dolibarr  16.0.5
mod_syslog_file.php
1 <?php
2 
3 require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
4 
9 {
10  public $code = 'file';
11  public $lastTime = 0;
12 
18  public function getName()
19  {
20  global $langs;
21 
22  return $langs->trans('File');
23  }
24 
30  public function getVersion()
31  {
32  return 'dolibarr';
33  }
34 
40  public function getInfo()
41  {
42  global $langs;
43 
44  return $langs->trans('YouCanUseDOL_DATA_ROOT');
45  }
46 
52  public function isActive()
53  {
54  global $conf;
55  return empty($conf->global->SYSLOG_DISABLE_LOGHANDLER_FILE) ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
56  }
57 
63  public function configure()
64  {
65  global $langs;
66 
67  return array(
68  array(
69  'name' => $langs->trans('SyslogFilename'),
70  'constant' => 'SYSLOG_FILE',
71  'default' => 'DOL_DATA_ROOT/dolibarr.log',
72  'attr' => 'size="60"'
73  )
74  );
75  }
76 
82  public function checkConfiguration()
83  {
84  global $langs;
85 
86  $errors = array();
87 
88  $filename = $this->getFilename();
89 
90  if (file_exists($filename) && is_writable($filename)) {
91  dol_syslog('admin/syslog: file '.$filename);
92  } else {
93  $errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
94  }
95 
96  return $errors;
97  }
98 
105  private function getFilename($suffixinfilename = '')
106  {
107  global $conf;
108 
109  if (empty($conf->global->SYSLOG_FILE)) {
110  $tmp = DOL_DATA_ROOT.'/dolibarr.log';
111  } else {
112  $tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
113  }
114 
115  if (!empty($conf->global->SYSLOG_FILE_ONEPERSESSION)) {
116  if ($conf->global->SYSLOG_FILE_ONEPERSESSION == 1) { // file depend on session key name (Note that session name is same for all users and is not a per user value)
117  $suffixinfilename .= '_'.session_name();
118  }
119  if ($conf->global->SYSLOG_FILE_ONEPERSESSION == 2) { // file depend on session value sor per user
120  $suffixinfilename .= '_'.session_name().'_'.$_SERVER["REMOTE_ADDR"];
121  }
122  }
123 
124  return $suffixinfilename ?preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
125  }
126 
134  public function export($content, $suffixinfilename = '')
135  {
136  global $conf, $dolibarr_main_prod;
137 
138  if (!empty($conf->global->MAIN_SYSLOG_DISABLE_FILE)) {
139  return; // Global option to disable output of this handler
140  }
141 
142  $logfile = $this->getFilename($suffixinfilename);
143 
144  // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
145  if (defined('SYSLOG_FILE_NO_ERROR')) {
146  $filefd = @fopen($logfile, 'a+');
147  } else {
148  $filefd = fopen($logfile, 'a+');
149  }
150 
151  if (!$filefd) {
152  if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) {
153  // Do not break dolibarr usage if log fails
154  //throw new Exception('Failed to open log file '.basename($logfile));
155  print 'Failed to open log file '.($dolibarr_main_prod ?basename($logfile) : $logfile);
156  }
157  } else {
158  $logLevels = array(
159  LOG_EMERG => 'EMERG',
160  LOG_ALERT => 'ALERT',
161  LOG_CRIT => 'CRIT',
162  LOG_ERR => 'ERR',
163  LOG_WARNING => 'WARNING',
164  LOG_NOTICE => 'NOTICE',
165  LOG_INFO => 'INFO',
166  LOG_DEBUG => 'DEBUG'
167  );
168 
169  $delay = "";
170  if (!empty($conf->global->MAIN_SYSLOG_SHOW_DELAY)) {
171  $now = microtime(true);
172  $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
173  $this->lastTime = $now;
174  }
175 
176  $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident > 0 ?str_pad('', $this->ident, ' ') : '').$content['message'];
177  fwrite($filefd, $message."\n");
178  fclose($filefd);
179  @chmod($logfile, octdec(empty($conf->global->MAIN_UMASK) ? '0664' : $conf->global->MAIN_UMASK));
180  }
181  }
182 }
mod_syslog_file\getFilename
getFilename($suffixinfilename='')
Return the parsed logfile path.
Definition: mod_syslog_file.php:105
LogHandler
Parent class for log handlers.
Definition: logHandler.php:23
mod_syslog_file\getInfo
getInfo()
Content of the info tooltip.
Definition: mod_syslog_file.php:40
mod_syslog_file
Class to manage logging to a file.
Definition: mod_syslog_file.php:8
mod_syslog_file\export
export($content, $suffixinfilename='')
Export the message.
Definition: mod_syslog_file.php:134
mod_syslog_file\configure
configure()
Return array of configuration data.
Definition: mod_syslog_file.php:63
mod_syslog_file\getName
getName()
Return name of logger.
Definition: mod_syslog_file.php:18
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
LogHandlerInterface
LogHandlerInterface.
Definition: logHandlerInterface.php:27
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mod_syslog_file\checkConfiguration
checkConfiguration()
Return if configuration is valid.
Definition: mod_syslog_file.php:82
mod_syslog_file\isActive
isActive()
Is the module active ?
Definition: mod_syslog_file.php:52
mod_syslog_file\getVersion
getVersion()
Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
Definition: mod_syslog_file.php:30
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845