From 792d25fdd8541711209c0db0f63676af1bc50932 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Tue, 25 Dec 2018 08:48:28 +0100 Subject: [PATCH] add MysqlHandler for Monolog-Logger Signed-off-by: Michael Kaufmann --- lib/Froxlor/FroxlorLogger.php | 55 +++++++++++++----------- lib/Froxlor/System/MysqlHandler.php | 66 +++++++++++++++++++++++++++++ logfiles_viewer.php | 2 +- 3 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 lib/Froxlor/System/MysqlHandler.php diff --git a/lib/Froxlor/FroxlorLogger.php b/lib/Froxlor/FroxlorLogger.php index 16d30f23..10c604e2 100644 --- a/lib/Froxlor/FroxlorLogger.php +++ b/lib/Froxlor/FroxlorLogger.php @@ -4,6 +4,7 @@ namespace Froxlor; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Handler\SyslogHandler; +use Froxlor\System\MysqlHandler; /** * Class FroxlorLogger @@ -32,12 +33,20 @@ class FroxlorLogger */ private static $crondebug_flag = false; + /** + * user info of logged in user + * + * @var array + */ + private static $userinfo = array(); + /** * Class constructor. */ - protected function __construct() + protected function __construct($userinfo = array()) { $this->initMonolog(); + self::$userinfo = $userinfo; self::$logtypes = array(); if ((Settings::Get('logger.logtypes') == null || Settings::Get('logger.logtypes') == '') && (Settings::Get('logger.enabled') !== null && Settings::Get('logger.enabled'))) { @@ -61,7 +70,7 @@ class FroxlorLogger self::$ml->pushHandler(new StreamHandler(Settings::Get('logger.logfile'), Logger::DEBUG)); break; case 'mysql': - // @fixme add MySQL-Handler + self::$ml->pushHandler(new MysqlHandler(Logger::DEBUG)); break; } } @@ -71,13 +80,17 @@ class FroxlorLogger * return FroxlorLogger instance * * @param array $userinfo - * unused - * + * * @return FroxlorLogger */ - public static function getInstanceOf($userinfo = null) + public static function getInstanceOf($userinfo = array()) { - return new FroxlorLogger(); + if (empty($userinfo)) { + $userinfo = array( + 'loginname' => 'system' + ); + } + return new FroxlorLogger($userinfo); } /** @@ -121,36 +134,30 @@ class FroxlorLogger return; } + $logExtra = array( + 'source' => $this->getActionTypeDesc($action), + 'action' => $action, + 'user' => self::$userinfo['loginname'] + ); + switch ($type) { case LOG_DEBUG: - self::$ml->addDebug($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addDebug($text, $logExtra); break; case LOG_INFO: - self::$ml->addInfo($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addInfo($text, $logExtra); break; case LOG_NOTICE: - self::$ml->addNotice($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addNotice($text, $logExtra); break; case LOG_WARNING: - self::$ml->addWarning($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addWarning($text, $logExtra); break; case LOG_ERR: - self::$ml->addError($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addError($text, $logExtra); break; default: - self::$ml->addDebug($text, array( - 'source' => $this->getActionTypeDesc($action) - )); + self::$ml->addDebug($text, $logExtra); } } diff --git a/lib/Froxlor/System/MysqlHandler.php b/lib/Froxlor/System/MysqlHandler.php new file mode 100644 index 00000000..c9dfc8ee --- /dev/null +++ b/lib/Froxlor/System/MysqlHandler.php @@ -0,0 +1,66 @@ + LOG_DEBUG, + Logger::INFO => LOG_INFO, + Logger::NOTICE => LOG_NOTICE, + Logger::WARNING => LOG_WARNING, + Logger::ERROR => LOG_ERR, + Logger::CRITICAL => LOG_ERR, + Logger::ALERT => LOG_ERR, + Logger::EMERGENCY => LOG_ERR + ); + + /** + * Constructor + * + * @param bool|int $level + * Debug level which this handler should store + * @param bool $bubble + */ + public function __construct($level = Logger::DEBUG, $bubble = true) + { + parent::__construct($level, $bubble); + } + + /** + * Insert the data to the logger table + * + * @param array $data + * @return bool + */ + protected function insert(array $data) + { + if ($this->pdoStatement === null) { + $sql = "INSERT INTO `panel_syslog` SET `text` = :message, `user` = :contextUser, `action` = :contextAction, `type` = :level, `date` = :datetime"; + $this->pdoStatement = \Froxlor\Database\Database::prepare($sql); + } + return $this->pdoStatement->execute($data); + } + + /** + * Writes the record down to the log + * + * @param array $record + * @return void + */ + protected function write(array $record) + { + $this->insert([ + ':message' => $record['message'], + ':contextUser' => (isset($record['context']['loginname']) ? $record['context']['loginname'] : 'unknown'), + ':contextAction' => (isset($record['context']['action']) ? $record['context']['action'] : '0'), + ':level' => self::$froxlorLevels[$record['level']], + ':datetime' => $record['datetime']->format('U') + ]); + } +} \ No newline at end of file diff --git a/logfiles_viewer.php b/logfiles_viewer.php index d4d4e2c2..eb7cc4e0 100644 --- a/logfiles_viewer.php +++ b/logfiles_viewer.php @@ -83,6 +83,6 @@ if (function_exists('exec')) { if (AREA == 'admin') { \Froxlor\UI\Response::dynamic_error('You need to allow the exec() function in the froxlor-vhost php-config'); } else { - \Froxlor\UI\Response::dynamic_error('Required function exec() is not allowed. Pllease contact the system administrator.'); + \Froxlor\UI\Response::dynamic_error('Required function exec() is not allowed. Please contact the system administrator.'); } }