add MysqlHandler for Monolog-Logger
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Froxlor;
|
|||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Handler\SyslogHandler;
|
use Monolog\Handler\SyslogHandler;
|
||||||
|
use Froxlor\System\MysqlHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FroxlorLogger
|
* Class FroxlorLogger
|
||||||
@@ -32,12 +33,20 @@ class FroxlorLogger
|
|||||||
*/
|
*/
|
||||||
private static $crondebug_flag = false;
|
private static $crondebug_flag = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* user info of logged in user
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $userinfo = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor.
|
* Class constructor.
|
||||||
*/
|
*/
|
||||||
protected function __construct()
|
protected function __construct($userinfo = array())
|
||||||
{
|
{
|
||||||
$this->initMonolog();
|
$this->initMonolog();
|
||||||
|
self::$userinfo = $userinfo;
|
||||||
self::$logtypes = array();
|
self::$logtypes = array();
|
||||||
|
|
||||||
if ((Settings::Get('logger.logtypes') == null || Settings::Get('logger.logtypes') == '') && (Settings::Get('logger.enabled') !== null && Settings::Get('logger.enabled'))) {
|
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));
|
self::$ml->pushHandler(new StreamHandler(Settings::Get('logger.logfile'), Logger::DEBUG));
|
||||||
break;
|
break;
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
// @fixme add MySQL-Handler
|
self::$ml->pushHandler(new MysqlHandler(Logger::DEBUG));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,13 +80,17 @@ class FroxlorLogger
|
|||||||
* return FroxlorLogger instance
|
* return FroxlorLogger instance
|
||||||
*
|
*
|
||||||
* @param array $userinfo
|
* @param array $userinfo
|
||||||
* unused
|
*
|
||||||
*
|
|
||||||
* @return FroxlorLogger
|
* @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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$logExtra = array(
|
||||||
|
'source' => $this->getActionTypeDesc($action),
|
||||||
|
'action' => $action,
|
||||||
|
'user' => self::$userinfo['loginname']
|
||||||
|
);
|
||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case LOG_DEBUG:
|
case LOG_DEBUG:
|
||||||
self::$ml->addDebug($text, array(
|
self::$ml->addDebug($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
break;
|
break;
|
||||||
case LOG_INFO:
|
case LOG_INFO:
|
||||||
self::$ml->addInfo($text, array(
|
self::$ml->addInfo($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
break;
|
break;
|
||||||
case LOG_NOTICE:
|
case LOG_NOTICE:
|
||||||
self::$ml->addNotice($text, array(
|
self::$ml->addNotice($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
break;
|
break;
|
||||||
case LOG_WARNING:
|
case LOG_WARNING:
|
||||||
self::$ml->addWarning($text, array(
|
self::$ml->addWarning($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
break;
|
break;
|
||||||
case LOG_ERR:
|
case LOG_ERR:
|
||||||
self::$ml->addError($text, array(
|
self::$ml->addError($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
self::$ml->addDebug($text, array(
|
self::$ml->addDebug($text, $logExtra);
|
||||||
'source' => $this->getActionTypeDesc($action)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
66
lib/Froxlor/System/MysqlHandler.php
Normal file
66
lib/Froxlor/System/MysqlHandler.php
Normal 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')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,6 +83,6 @@ if (function_exists('exec')) {
|
|||||||
if (AREA == 'admin') {
|
if (AREA == 'admin') {
|
||||||
\Froxlor\UI\Response::dynamic_error('You need to allow the exec() function in the froxlor-vhost php-config');
|
\Froxlor\UI\Response::dynamic_error('You need to allow the exec() function in the froxlor-vhost php-config');
|
||||||
} else {
|
} 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.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user