Skip to content

Commit

Permalink
add MysqlHandler for Monolog-Logger
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kaufmann <[email protected]>
  • Loading branch information
d00p committed Dec 25, 2018
1 parent 6841308 commit 792d25f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 25 deletions.
55 changes: 31 additions & 24 deletions lib/Froxlor/FroxlorLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
use Froxlor\System\MysqlHandler;

/**
* Class FroxlorLogger
Expand Down Expand Up @@ -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'))) {
Expand All @@ -61,7 +70,7 @@ protected function __construct()
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;
}
}
Expand All @@ -71,13 +80,17 @@ protected function __construct()
* 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);
}

/**
Expand Down Expand Up @@ -121,36 +134,30 @@ public function logAction($action = USR_ACTION, $type = LOG_NOTICE, $text = null
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);
}
}

Expand Down
66 changes: 66 additions & 0 deletions lib/Froxlor/System/MysqlHandler.php
Original file line number Diff line number Diff line change
@@ -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')
]);
}
}
2 changes: 1 addition & 1 deletion logfiles_viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@
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.');
}
}

0 comments on commit 792d25f

Please sign in to comment.