2
0
forked from Wavyzz/dolibarr

Created a modular syslog handler system

I've split all log handlers into classes so that people can make
new log handler systems without making changes to the core.
Also, now every log handler can handle how and what will be logged.
This commit is contained in:
Marcos García
2012-10-16 02:01:37 +02:00
parent 5dff8dbc6d
commit 07dcb07fd2
8 changed files with 754 additions and 246 deletions

View File

@@ -33,10 +33,42 @@ $langs->load("other");
$error=0; $mesg='';
$action = GETPOST("action");
$syslog_file_on=(defined('SYSLOG_FILE_ON') && constant('SYSLOG_FILE_ON'))?1:0;
$syslog_syslog_on=(defined('SYSLOG_SYSLOG_ON') && constant('SYSLOG_SYSLOG_ON'))?1:0;
$syslog_firephp_on=(defined('SYSLOG_FIREPHP_ON') && constant('SYSLOG_FIREPHP_ON'))?1:0;
$syslog_chromephp_on=(defined('SYSLOG_CHROMEPHP_ON') && constant('SYSLOG_CHROMEPHP_ON'))?1:0;
$syslogModules = array();
$activeModules = array();
if (defined('SYSLOG_HANDLERS')) $activeModules = unserialize(SYSLOG_HANDLERS);
$dir = dol_buildpath('/core/modules/syslog/');
if (is_dir($dir))
{
$handle = opendir($dir);
if (is_resource($handle))
{
$var=true;
while (($file = readdir($handle))!==false)
{
if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file)-3, 3) == 'php')
{
$file = substr($file, 0, dol_strlen($file)-4);
require_once $dir.$file.'.php';
$module = new $file;
// Show modules according to features level
if ($module->getVersion() == $module::DEVELOPMENT && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
if ($module->getVersion() == $module::EXPERIMENTAL && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
$syslogModules[] = $file;
}
}
closedir($handle);
}
}
/*
@@ -48,71 +80,32 @@ if ($action == 'set')
{
$db->begin();
$res = dolibarr_del_const($db,"SYSLOG_FILE_ON",0);
$res = dolibarr_del_const($db,"SYSLOG_SYSLOG_ON",0);
$res = dolibarr_del_const($db,"SYSLOG_FIREPHP_ON",0);
$res = dolibarr_del_const($db,"SYSLOG_CHROMEPHP_ON",0);
$activeModules = array();
$selectedModules = (isset($_POST['SYSLOG_HANDLERS']) ? $_POST['SYSLOG_HANDLERS'] : array());
$syslog_file_on=0;
$syslog_syslog_on=0;
$syslog_firephp_on=0;
$syslog_chromephp_on=0;
if (! $error && GETPOST("filename"))
foreach ($selectedModules as $syslogHandler)
{
$filename=GETPOST("filename");
$filelog=GETPOST("filename");
$filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog);
$file=@fopen($filelog,"a+");
if ($file)
if (in_array($syslogHandler, $syslogModules))
{
fclose($file);
$module = new $syslogHandler;
dol_syslog("admin/syslog: file ".$filename);
$res = dolibarr_set_const($db,"SYSLOG_FILE",$filename,'chaine',0,'',0);
if (! $res > 0) $error++;
$syslog_file_on=GETPOST('SYSLOG_FILE_ON');
if (! $error) $res = dolibarr_set_const($db,"SYSLOG_FILE_ON",$syslog_file_on,'chaine',0,'',0);
if ($module->isActive())
{
$activeModules[] = $syslogHandler;
foreach ($module->configure() as $option)
{
if ($_POST[$option['constant']])
{
dolibarr_del_const($db, $option['constant'], 0);
dolibarr_set_const($db, $option['constant'], $_POST[$option['constant']], 'chaine');
}
}
}
else
{
$error++;
$mesg = "<font class=\"error\">".$langs->trans("ErrorFailedToOpenFile",$filename)."</font>";
}
}
if (! $error && GETPOST("facility"))
{
$facility=GETPOST("facility");
if (defined($_POST["facility"]))
{
// Only LOG_USER supported on Windows
if (! empty($_SERVER["WINDIR"])) $facility='LOG_USER';
dol_syslog("admin/syslog: facility ".$facility);
$res = dolibarr_set_const($db,"SYSLOG_FACILITY",$facility,'chaine',0,'',0);
if (! $res > 0) $error++;
$syslog_syslog_on=GETPOST('SYSLOG_SYSLOG_ON');
if (! $error) $res = dolibarr_set_const($db,"SYSLOG_SYSLOG_ON",$syslog_syslog_on,'chaine',0,'',0);
}
else
{
$error++;
$mesg = "<font class=\"error\">".$langs->trans("ErrorUnknownSyslogConstant",$facility)."</font>";
}
}
if (! $error && isset($_POST['SYSLOG_FIREPHP_ON'])) // If firephp no available, post is not present. We must keep isset here.
{
$syslog_firephp_on=GETPOST('SYSLOG_FIREPHP_ON');
if (! $error) $res = dolibarr_set_const($db,"SYSLOG_FIREPHP_ON",$syslog_firephp_on,'chaine',0,'',0);
}
if (! $error && isset($_POST['SYSLOG_CHROMEPHP_ON'])) // If chromephp no available, post is not present. We must keep isset here.
{
$syslog_chromephp_on=GETPOST('SYSLOG_CHROMEPHP_ON');
if (! $error) $res = dolibarr_set_const($db,"SYSLOG_CHROMEPHP_ON",$syslog_chromephp_on,'chaine',0,'',0);
}
dolibarr_set_const($db, 'SYSLOG_HANDLERS', serialize($activeModules), 'chaine');
if (! $error)
{
@@ -185,71 +178,102 @@ print '<td align="right" colspan="2"><input type="submit" class="button" '.$opti
print "</tr>\n";
$var=true;
// Output to file
$var=!$var;
print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_FILE_ON" '.$option.' value="1" '.($syslog_file_on?' checked="checked"':'').'> '.$langs->trans("SyslogSimpleFile").'</td>';
print '<td nowrap="nowrap">'.$langs->trans("SyslogFilename").': <input type="text" class="flat" name="filename" '.$option.' size="60" value="'.$defaultsyslogfile.'">';
print '</td>';
print "<td align=\"left\">".$form->textwithpicto('',$langs->trans("YouCanUseDOL_DATA_ROOT"));
print '</td></tr>';
// Output to syslog
$var=!$var;
print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_SYSLOG_ON" '.$option.' value="1" '.($syslog_syslog_on?' checked="checked"':'').'> '.$langs->trans("SyslogSyslog").'</td>';
print '<td nowrap="nowrap">'.$langs->trans("SyslogFacility").': <input type="text" class="flat" name="facility" '.$option.' value="'.$defaultsyslogfacility.'">';
print '</td>';
print "<td align=\"left\">".$form->textwithpicto('', $langs->trans('OnlyWindowsLOG_USER'));
print '</td></tr>';
// Output to Firebug
try
foreach ($syslogModules as $moduleName)
{
set_include_path('/usr/share/php/');
$res=@include_once 'FirePHPCore/FirePHP.class.php';
restore_include_path();
if ($res)
{
$module = new $moduleName;
$var=!$var;
print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_FIREPHP_ON" '.$option.' value="1" ';
if (! class_exists('FirePHP')) print ' disabled="disabled"';
else print ($syslog_firephp_on?' checked="checked"':"");
print '> '.$langs->trans("FirePHP").'</td>';
print '<tr '.$bc[$var].'>';
print '<td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked="checked"' : '').(!$module->isActive() ? 'disabled="disabled"' : '').'> ';
print $module->getName().'</td>';
print '<td nowrap="nowrap">';
print '</td>';
print "<td align=\"left\">".$form->textwithpicto('','FirePHP must be installed onto PHP and FirePHP plugin for Firefox must also be installed');
print '</td></tr>';
if ($module->configure())
{
foreach ($module->configure() as $option)
{
if (defined($option['constant'])) $value = constant($option['constant']);
else $value = (isset($option['default']) ? $option['default'] : '');
print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
}
}
catch(Exception $e)
{
// Do nothing
print '<!-- FirePHP no available into PHP -->'."\n";
}
print '</td>';
print '<td align="left">';
if ($module->getInfo())
{
print $form->textwithpicto('', $module->getInfo());
}
print '</td>';
print "</tr>\n";
}
// Output to Chrome
try
{
set_include_path('/usr/share/php/');
$res=@include_once 'ChromePhp.php';
restore_include_path();
if ($res)
{
$var=!$var;
print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_CHROMEPHP_ON" '.$option.' value="1" ';
if (! class_exists('ChromePHP')) print ' disabled="disabled"';
else print ($syslog_chromephp_on?' checked="checked"':"");
print '> '.$langs->trans("ChromePHP").'</td>';
print '<td nowrap="nowrap">';
print '</td>';
print "<td align=\"left\">".$form->textwithpicto('','ChromePHP must be installed onto PHP path and ChromePHP plugin for Chrome must also be installed');
print '</td></tr>';
}
}
catch(Exception $e)
{
// Do nothing
print '<!-- ChromePHP no available into PHP -->'."\n";
}
// // Output to file
// $var=!$var;
// print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_FILE_ON" '.$option.' value="1" '.($syslog_file_on?' checked="checked"':'').'> '.$langs->trans("SyslogSimpleFile").'</td>';
// print '<td nowrap="nowrap">'.$langs->trans("SyslogFilename").': <input type="text" class="flat" name="filename" '.$option.' size="60" value="'.$defaultsyslogfile.'">';
// print '</td>';
// print "<td align=\"left\">".$form->textwithpicto('',$langs->trans("YouCanUseDOL_DATA_ROOT"));
// print '</td></tr>';
// // Output to syslog
// $var=!$var;
// print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_SYSLOG_ON" '.$option.' value="1" '.($syslog_syslog_on?' checked="checked"':'').'> '.$langs->trans("SyslogSyslog").'</td>';
// print '<td nowrap="nowrap">'.$langs->trans("SyslogFacility").': <input type="text" class="flat" name="facility" '.$option.' value="'.$defaultsyslogfacility.'">';
// print '</td>';
// print "<td align=\"left\">".$form->textwithpicto('', $langs->trans('OnlyWindowsLOG_USER'));
// print '</td></tr>';
// // Output to Firebug
// try
// {
// set_include_path('/usr/share/php/');
// $res=@include_once 'FirePHPCore/FirePHP.class.php';
// restore_include_path();
// if ($res)
// {
// $var=!$var;
// print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_FIREPHP_ON" '.$option.' value="1" ';
// if (! class_exists('FirePHP')) print ' disabled="disabled"';
// else print ($syslog_firephp_on?' checked="checked"':"");
// print '> '.$langs->trans("FirePHP").'</td>';
// print '<td nowrap="nowrap">';
// print '</td>';
// print "<td align=\"left\">".$form->textwithpicto('','FirePHP must be installed onto PHP and FirePHP plugin for Firefox must also be installed');
// print '</td></tr>';
// }
// }
// catch(Exception $e)
// {
// // Do nothing
// print '<!-- FirePHP no available into PHP -->'."\n";
// }
// // Output to Chrome
// try
// {
// set_include_path('/usr/share/php/');
// $res=@include_once 'ChromePhp.php';
// restore_include_path();
// if ($res)
// {
// $var=!$var;
// print '<tr '.$bc[$var].'><td width="140"><input '.$bc[$var].' type="checkbox" name="SYSLOG_CHROMEPHP_ON" '.$option.' value="1" ';
// if (! class_exists('ChromePHP')) print ' disabled="disabled"';
// else print ($syslog_chromephp_on?' checked="checked"':"");
// print '> '.$langs->trans("ChromePHP").'</td>';
// print '<td nowrap="nowrap">';
// print '</td>';
// print "<td align=\"left\">".$form->textwithpicto('','ChromePHP must be installed onto PHP path and ChromePHP plugin for Chrome must also be installed');
// print '</td></tr>';
// }
// }
// catch(Exception $e)
// {
// // Do nothing
// print '<!-- ChromePHP no available into PHP -->'."\n";
// }
print "</table>\n";
print "</form>\n";

View File

@@ -463,68 +463,39 @@ function dol_escape_htmltag($stringtoescape,$keepb=0)
* On Linux LOG_ERR=3, LOG_WARNING=4, LOG_INFO=6, LOG_DEBUG=7
* @return void
*/
function dol_syslog($message, $level=LOG_INFO)
function dol_syslog($message, $level = LOG_INFO)
{
global $conf,$user,$langs,$_REQUEST;
global $conf, $user, $langs;
// If syslog module enabled
if (empty($conf->syslog->enabled)) return false;
if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false;
$logLevels = array(
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG
);
if (!in_array($level, $logLevels))
{
throw new Exception('Incorrect log level');
}
if ($level > $conf->global->SYSLOG_LEVEL) return false;
// If adding log inside HTML page is required
if (! empty($_REQUEST['logtohtml']) && ! empty($conf->global->MAIN_LOGTOHTML))
{
$conf->logbuffer[]=dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".$message;
$conf->logbuffer[] = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".$message;
}
// If syslog module enabled
if (! empty($conf->syslog->enabled))
{
//print $level.' - '.$conf->global->SYSLOG_LEVEL.' - '.$conf->syslog->enabled." \n";
if ($level > $conf->global->SYSLOG_LEVEL) return;
// Translate error message if this is an error message (rare) and langs is loaded
if ($level == LOG_ERR)
{
if (is_object($langs))
{
$langs->load("errors");
if ($message != $langs->trans($message)) $message = $langs->trans($message);
}
}
// Add page/script name to log message
$script=isset($_SERVER['PHP_SELF'])?basename($_SERVER['PHP_SELF'],'.php').' ':'';
$message=$script.$message;
// Add user to log message
$login='nologin';
if (is_object($user) && $user->id) $login=$user->login;
$message=sprintf("%-8s",$login)." ".$message;
// Check if log is to a file (SYSLOG_FILE_ON defined)
if (defined("SYSLOG_FILE_ON") && constant("SYSLOG_FILE_ON"))
{
$filelog=SYSLOG_FILE;
$filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog);
//print "filelog=".$filelog."\n";
if (defined("SYSLOG_FILE_NO_ERROR")) $file=@fopen($filelog,"a+");
else $file=fopen($filelog,"a+");
if ($file)
{
$ip='???'; // $ip contains information to identify computer that run the code
if (! empty($_SERVER["REMOTE_ADDR"])) $ip=$_SERVER["REMOTE_ADDR"]; // In most cases.
else if (! empty($_SERVER['SERVER_ADDR'])) $ip=$_SERVER['SERVER_ADDR']; // This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
else if (! empty($_SERVER['COMPUTERNAME'])) $ip=$_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME'])?'':'@'.$_SERVER['USERNAME']); // This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but usefull if OS defined it).
else if (! empty($_SERVER['LOGNAME'])) $ip='???@'.$_SERVER['LOGNAME']; // This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but usefull if OS defined it).
$liblevelarray=array(LOG_ERR=>'ERROR',LOG_WARNING=>'WARN',LOG_INFO=>'INFO',LOG_DEBUG=>'DEBUG');
$liblevel=$liblevelarray[$level];
if (! $liblevel) $liblevel='UNDEF';
$message=dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".sprintf("%-5s",$liblevel)." ".sprintf("%-15s",$ip)." ".$message;
fwrite($file,$message."\n");
fclose($file);
// This is for log file, we do not change permissions
// If enable html log tag enabled and url parameter log defined, we show output log on HTML comments
if (! empty($conf->global->MAIN_ENABLE_LOG_HTML) && ! empty($_GET["log"]))
{
@@ -532,78 +503,46 @@ function dol_syslog($message, $level=LOG_INFO)
print $message."\n";
print "Log end -->\n";
}
}
elseif (! defined("SYSLOG_FILE_NO_ERROR"))
$data = array(
'message' => $message,
'script' => (isset($_SERVER['PHP_SELF'])? basename($_SERVER['PHP_SELF'],'.php') : false),
'level' => $level,
'user' => ((is_object($user) && $user->id) ? $user->login : false),
'ip' => false
);
if (! empty($_SERVER["REMOTE_ADDR"])) $data['ip'] = $_SERVER['REMOTE_ADDR'];
// This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
else if (! empty($_SERVER['SERVER_ADDR'])) $data['ip'] = $_SERVER['SERVER_ADDR'];
// This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defined it).
else if (! empty($_SERVER['COMPUTERNAME'])) $data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME'])?'':'@'.$_SERVER['USERNAME']);
// This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but usefull if OS defined it).
else if (! empty($_SERVER['LOGNAME'])) $data['ip'] = '???@'.$_SERVER['LOGNAME'];
//We load SYSLOG handlers
if (defined('SYSLOG_HANDLERS')) $handlers = unserialize(SYSLOG_HANDLERS);
else $handlers = array();
foreach ($handlers as $handler)
{
// Do not use here a call to functions that make call to dol_syslog so making call to langs. A simple print is enough.
print "Error, failed to open file ".$filelog."\n";
}
$file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php';
if (!file_exists($file))
{
throw new Exception('Missing log handler');
}
// Check if log is to syslog (SYSLOG_SYSLOG_ON defined)
if (defined("SYSLOG_SYSLOG_ON") && constant("SYSLOG_SYSLOG_ON"))
require_once $file;
$class = new $handler();
if (!$class instanceof LogHandlerInterface)
{
if (function_exists('openlog')) // This function does not exists on some ISP (Ex: Free in France)
{
$facility = LOG_USER;
if (defined("SYSLOG_FACILITY") && constant("SYSLOG_FACILITY"))
{
// Exemple: SYSLOG_FACILITY vaut LOG_USER qui vaut 8. On a besoin de 8 dans $facility.
$facility = constant("SYSLOG_FACILITY");
throw new Exception('Log handler does not extend LogHandlerInterface');
}
openlog("dolibarr", LOG_PID | LOG_PERROR, (int) $facility); // (int) is required to avoid error parameter 3 expected to be long
if (! $level) syslog(LOG_ERR, $message);
else syslog($level, $message);
closelog();
}
}
// Check if log is to syslog (SYSLOG_FIREPHP_ON defined)
if (defined("SYSLOG_FIREPHP_ON") && constant("SYSLOG_FIREPHP_ON") && ! empty($_SERVER["SERVER_NAME"])) //! empty($_SERVER["SERVER_NAME"]) to be sure to enable this in Web mode only
{
try
{
// Warning FirePHPCore must be into PHP include path. It is not possible to use into require_once() a constant from
// database or config file because we must be able to log data before database or config file read.
$oldinclude=get_include_path();
set_include_path('/usr/share/php/');
include_once 'FirePHPCore/FirePHP.class.php';
set_include_path($oldinclude);
ob_start(); // To be sure headers are not flushed until all page is completely processed
$firephp = FirePHP::getInstance(true);
if ($level == LOG_ERR) $firephp->error($message);
elseif ($level == LOG_WARNING) $firephp->warn($message);
elseif ($level == LOG_INFO) $firephp->log($message);
else $firephp->log($message);
}
catch(Exception $e)
{
// Do not use dol_syslog here to avoid infinite loop
}
}
// Check if log is to syslog (SYSLOG_FIREPHP_ON defined)
if (defined("SYSLOG_CHROMEPHP_ON") && constant("SYSLOG_CHROMEPHP_ON") && ! empty($_SERVER["SERVER_NAME"])) //! empty($_SERVER["SERVER_NAME"]) to be sure to enable this in Web mode only
{
try
{
// Warning ChromePHP must be into PHP include path. It is not possible to use into require_once() a constant from
// database or config file because we must be able to log data before database or config file read.
$oldinclude=get_include_path();
set_include_path('/usr/share/php/');
include_once 'ChromePhp.php';
set_include_path($oldinclude);
ob_start(); // To be sure headers are not flushed until all page is completely processed
if ($level == LOG_ERR) ChromePhp::error($message);
elseif ($level == LOG_WARNING) ChromePhp::warn($message);
elseif ($level == LOG_INFO) ChromePhp::log($message);
else ChromePhp::log($message);
}
catch(Exception $e)
{
// Do not use dol_syslog here to avoid infinite loop
}
}
$class->export($data);
}
}

View File

@@ -0,0 +1,57 @@
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandlerInterface.php';
class LogHandler
{
const STABLE = 'stable';
const DEVELOPMENT = 'development';
const EXPERIMENTAL = 'experimental';
/**
* Content of the info tooltip.
* @return false|string
*/
public function getInfo()
{
return false;
}
/**
* Version of the module
* @return string
*/
public function getVersion()
{
return self::EXPERIMENTAL;
}
/**
* ¿Is the module active?
* @return boolean
*/
public function isActive()
{
return false;
}
/**
* Configuration variables of the module
* @return array
*/
public function configure()
{
return array();
}
/**
* Function that checks if the configuration is valid.
* It will be called after setting the configuration.
* The function returns an array with error messages
* @return array
*/
public function checkConfiguration()
{
return array();
}
}

View File

@@ -0,0 +1,12 @@
<?php
interface LogHandlerInterface
{
public function getName();
public function getVersion();
public function getInfo();
public function configure();
public function checkConfiguration();
public function isActive();
public function export($content);
}

View File

@@ -0,0 +1,122 @@
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
class mod_syslog_chromephp extends LogHandler implements LogHandlerInterface
{
/**
* {@inheritDoc}
*/
public function getName()
{
return 'ChromePHP';
}
/**
* {@inheritDoc}
*/
public function getVersion()
{
return self::STABLE;
}
/**
* {@inheritDoc}
*/
public function getInfo()
{
global $langs;
return $langs->trans('ChromePHPIncludePathWarning');
}
/**
* {@inheritDoc}
*/
public function isActive()
{
try
{
set_include_path('/usr/share/php/');
$res = @include_once 'ChromePhp.class.php';
restore_include_path();
if ($res)
{
return true;
}
}
catch(Exception $e)
{
print '<!-- FirePHP no available into PHP -->'."\n";
}
return false;
}
/**
* {@inheritDoc}
*/
public function configure()
{
global $langs;
return array(
array(
'name' => $langs->trans('IncludePath'),
'constant' => 'SYSLOG_CHROMEPHP_INCLUDEPATH',
'default' => '/usr/share/php',
'attr' => 'size="40"'
)
);
}
/**
* {@inheritDoc}
*/
public function checkConfiguration()
{
global $langs;
$errors = array();
$oldinclude = get_include_path();
set_include_path(SYSLOG_CHROMEPHP_INCLUDEPATH);
if (!file_exists('ChromePhp.php'))
{
$errors[] = $langs->trans("ErrorFailedToOpenFile", 'ChromePhp.php');
}
set_include_path($oldinclude);
return $errors;
}
/**
* {@inheritDoc}
*/
public function export($content)
{
//We check the configuration to avoid showing PHP warnings
if (count($this->checkConfiguration())) return false;
try
{
// Warning ChromePHP must be into PHP include path. It is not possible to use into require_once() a constant from
// database or config file because we must be able to log data before database or config file read.
$oldinclude=get_include_path();
set_include_path(SYSLOG_CHROMEPHP_INCLUDEPATH);
include_once 'ChromePhp.php';
set_include_path($oldinclude);
ob_start(); // To be sure headers are not flushed until all page is completely processed
if ($level == LOG_ERR) ChromePhp::error($message);
elseif ($level == LOG_WARNING) ChromePhp::warn($message);
elseif ($level == LOG_INFO) ChromePhp::log($message);
else ChromePhp::log($message);
}
catch (Exception $e)
{
// Do not use dol_syslog here to avoid infinite loop
}
}
}

View File

@@ -0,0 +1,121 @@
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
class mod_syslog_file extends LogHandler implements LogHandlerInterface
{
/**
* {@inheritDoc}
*/
public function getName()
{
global $langs;
return $langs->trans('File');
}
/**
* {@inheritDoc}
*/
public function getVersion()
{
return self::STABLE;
}
/**
* {@inheritDoc}
*/
public function getInfo()
{
global $langs;
return $langs->trans('YouCanUseDOL_DATA_ROOT');
}
/**
* {@inheritDoc}
*/
public function isActive()
{
return true;
}
/**
* {@inheritDoc}
*/
public function configure()
{
global $langs;
return array(
array(
'name' => $langs->trans('SyslogFilename'),
'constant' => 'SYSLOG_FILE',
'default' => 'DOL_DATA_ROOT/dolibarr.log',
'attr' => 'size="60"'
)
);
}
/**
* {@inheritDoc}
*/
public function checkConfiguration()
{
global $langs;
$errors = array();
$filename = $this->getFilename();
if (file_exists($filename) && is_writable($filename))
{
dol_syslog('admin/syslog: file '.$filename);
}
else $errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
return $errors;
}
/**
* Return the parsed logfile path
* @return string
*/
private function getFilename()
{
return str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, SYSLOG_FILE);
}
/**
* Export the message
* @param array $content Array containing the info about the message
*/
public function export($content)
{
$logfile = $this->getFilename();
if (defined("SYSLOG_FILE_NO_ERROR")) $filefd = @fopen($logfile, 'a+');
else $filefd = fopen($logfile, 'a+');
if (!$filefd && ! defined("SYSLOG_FILE_NO_ERROR"))
{
throw new Exception('Failed to open log file '.$logfile);
}
$logLevels = array(
LOG_EMERG => 'EMERG',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRIT',
LOG_ERR => 'ERR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG'
);
$message = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".sprintf("%-5s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".$content['message'];
fwrite($filefd, $message."\n");
fclose($filefd);
}
}

View File

@@ -0,0 +1,124 @@
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
class mod_syslog_firephp extends LogHandler implements LogHandlerInterface
{
/**
* {@inheritDoc}
*/
public function getName()
{
return 'FirePHP';
}
/**
* {@inheritDoc}
*/
public function getVersion()
{
return self::STABLE;
}
/**
* {@inheritDoc}
*/
public function getInfo()
{
global $langs;
return $langs->trans('FirePHPIncludePathWarning');
}
/**
* {@inheritDoc}
*/
public function isActive()
{
try
{
set_include_path('/usr/share/php/');
$res = @include_once 'FirePHPCore/FirePHP.class.php';
restore_include_path();
if ($res)
{
return true;
}
}
catch(Exception $e)
{
print '<!-- FirePHP no available into PHP -->'."\n";
}
return false;
}
/**
* {@inheritDoc}
*/
public function configure()
{
global $langs;
return array(
array(
'name' => $langs->trans('IncludePath'),
'constant' => 'SYSLOG_FIREPHP_INCLUDEPATH',
'default' => '/usr/share/php',
'attr' => 'size="40"'
)
);
}
/**
* {@inheritDoc}
*/
public function checkConfiguration()
{
global $langs;
$errors = array();
$oldinclude = get_include_path();
set_include_path(SYSLOG_FIREPHP_INCLUDEPATH);
if (!file_exists('FirePHPCore/FirePHP.class.php'))
{
$errors[] = $langs->trans("ErrorFailedToOpenFile", 'FirePhp.php');
}
set_include_path($oldinclude);
return $errors;
}
/**
* {@inheritDoc}
*/
public function export($content)
{
//We check the configuration to avoid showing PHP warnings
if (count($this->checkConfiguration())) return false;
try
{
// Warning FirePHPCore must be into PHP include path. It is not possible to use into require_once() a constant from
// database or config file because we must be able to log data before database or config file read.
$oldinclude=get_include_path();
set_include_path('/usr/share/php/');
include_once 'FirePHPCore/FirePHP.class.php';
set_include_path($oldinclude);
ob_start(); // To be sure headers are not flushed until all page is completely processed
$firephp = FirePHP::getInstance(true);
if ($level == LOG_ERR) $firephp->error($message);
elseif ($level == LOG_WARNING) $firephp->warn($message);
elseif ($level == LOG_INFO) $firephp->log($message);
else $firephp->log($message);
}
catch (Exception $e)
{
// Do not use dol_syslog here to avoid infinite loop
return false;
}
}
}

View File

@@ -0,0 +1,109 @@
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
class mod_syslog_syslog extends LogHandler implements LogHandlerInterface
{
/**
* {@inheritDoc}
*/
public function getName()
{
return 'Syslog';
}
/**
* {@inheritDoc}
*/
public function getVersion()
{
return self::STABLE;
}
/**
* {@inheritDoc}
*/
public function getInfo()
{
global $langs;
return $langs->trans('OnlyWindowsLOG_USER');
}
/**
* {@inheritDoc}
*/
public function isActive()
{
return true;
}
/**
* {@inheritDoc}
*/
public function configure()
{
global $langs;
return array(
array(
'constant' => 'SYSLOG_FACILITY',
'name' => $langs->trans('SyslogFacility'),
'default' => 'LOG_USER'
)
);
}
/**
* {@inheritDoc}
*/
public function checkConfiguration()
{
global $langs;
$errors = array();
$facility = SYSLOG_FACILITY;
if ($facility)
{
// Only LOG_USER supported on Windows
if (! empty($_SERVER["WINDIR"])) $facility='LOG_USER';
dol_syslog("admin/syslog: facility ".$facility);
}
else
{
$errors[] = $langs->trans("ErrorUnknownSyslogConstant", $facility);
}
return $errors;
}
/**
* Export the message
* @param array $content Array containing the info about the message
*/
public function export($content)
{
// This function does not exists on some ISP (Ex: Free in France)
if (!function_exists('openlog'))
{
throw new Exception('Function openlog is not available in this server');
}
if (defined("SYSLOG_FACILITY") && constant("SYSLOG_FACILITY"))
{
if (constant(constant('SYSLOG_FACILITY')))
{
$facility = constant(constant("SYSLOG_FACILITY"));
}
else $facility = LOG_USER;
}
else $facility = LOG_USER;
// (int) is required to avoid error parameter 3 expected to be long
openlog('dolibarr', LOG_PID | LOG_PERROR, (int) $facility);
syslog($content['level'], $content['message']);
closelog();
}
}