add MysqlHandler for Monolog-Logger

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-12-25 08:48:28 +01:00
parent 684130871b
commit 792d25fdd8
3 changed files with 98 additions and 25 deletions

View File

@@ -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);
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Froxlor\System;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
class MysqlHandler extends AbstractProcessingHandler
{
protected $pdoStatement = null;
protected static $froxlorLevels = array(
Logger::DEBUG => 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')
]);
}
}

View File

@@ -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.');
}
}