Compare commits

...

3 Commits

Author SHA1 Message Date
Eric Espie
b87845c844 Merge branch 'support/3.0' into feature/faf_sqlite 2022-03-07 18:33:43 +01:00
Eric Espie
37dd887cf1 SQLite Log format 2022-03-07 12:00:04 +01:00
Eric Espie
b6cfb40f8f SQLite Log format 2022-03-07 11:56:42 +01:00

View File

@@ -455,6 +455,7 @@ class LogFileNameBuilderFactory
class FileLog
{
protected $oFileNameBuilder;
protected $bHasSQLite;
/**
* FileLog constructor.
@@ -467,6 +468,22 @@ class FileLog
public function __construct($sFileName = '')
{
$this->oFileNameBuilder = LogFileNameBuilderFactory::GetInstance($sFileName);
$sLogFilePath = $this->oFileNameBuilder->GetLogFilePath();
$this->bHasSQLite = class_exists('SQLite3');
if ($this->bHasSQLite) {
if (empty($sLogFilePath)) {
return;
}
$bCreate = false;
$sDBFilePath = "$sLogFilePath.db";
if (!is_file($sDBFilePath)) {
$bCreate = true;
}
$this->DB = new SQLite3($sDBFilePath);
if ($bCreate) {
$this->DB->exec('CREATE TABLE log (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, level TEXT, user TEXT, content LONGTEXT, context LONGTEXT, channel TEXT)');
}
}
}
public function Error($sText, $sChannel = '', $aContext = array())
@@ -502,6 +519,17 @@ class FileLog
protected function Write($sText, $sLevel = '', $sChannel = '', $aContext = array())
{
if ($this->bHasSQLite) {
$stmt = $this->DB->prepare('INSERT INTO log (date, level, user, content, context, channel) VALUES (:date, :level, :user, :content, :context, :channel)');
$stmt->bindValue(':date', date('Y-m-d H:i:s'));
$stmt->bindValue(':level', $sLevel);
$stmt->bindValue(':user', LogAPI::GetUserInfo());
$stmt->bindValue(':content', $sText);
$stmt->bindValue(':context', empty($aContext) ? '' : var_export($aContext, true));
$stmt->bindValue(':channel', $sChannel);
$stmt->execute();
}
$sTextPrefix = empty($sLevel) ? '' : (str_pad($sLevel, 7));
$sTextPrefix .= ' | ';
$sTextPrefix .= str_pad(LogAPI::GetUserInfo(), 5)." | ";