dolibarr  20.0.0-alpha
mod_syslog_file.php
1 <?php
2 /* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
3  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4  */
5 
6 require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
7 
12 {
13  public $code = 'file';
14  public $lastTime = 0;
15 
21  public function getName()
22  {
23  global $langs;
24 
25  return $langs->trans('File');
26  }
27 
33  public function getVersion()
34  {
35  return 'dolibarr';
36  }
37 
43  public function getInfo()
44  {
45  global $langs;
46 
47  return $langs->trans('YouCanUseDOL_DATA_ROOT');
48  }
49 
55  public function isActive()
56  {
57  return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_FILE') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
58  }
59 
65  public function configure()
66  {
67  global $langs;
68 
69  return array(
70  array(
71  'name' => $langs->trans('SyslogFilename'),
72  'constant' => 'SYSLOG_FILE',
73  'default' => 'DOL_DATA_ROOT/dolibarr.log',
74  'css' => 'minwidth300 maxwidth500'
75  )
76  );
77  }
78 
84  public function checkConfiguration()
85  {
86  global $langs;
87 
88  $filename = $this->getFilename();
89 
90  if (file_exists($filename) && is_writable($filename)) {
91  dol_syslog('admin/syslog: file '.$filename);
92  return true;
93  } else {
94  $this->errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
95  return false;
96  }
97  }
98 
105  private function getFilename($suffixinfilename = '')
106  {
107  global $conf;
108 
109  if (!getDolGlobalString('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 (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
116  if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
117  if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 1) { // file depend on instance session key name (Note that session name is same for the instance so for all users and is not a per user value)
118  $suffixinfilename .= '_'.session_name();
119  }
120  if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
121  $suffixinfilename .= '_'.session_name().'_'.$_SERVER["REMOTE_ADDR"];
122  }
123  } else {
124  $suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
125  }
126  }
127 
128  return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
129  }
130 
139  public function export($content, $suffixinfilename = '')
140  {
141  if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
142  return; // Global option to disable output of this handler
143  }
144 
145  $logfile = $this->getFilename($suffixinfilename);
146 
147  // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
148  if (defined('SYSLOG_FILE_NO_ERROR')) {
149  $filefd = @fopen($logfile, 'a+');
150  } else {
151  $filefd = fopen($logfile, 'a+');
152  }
153 
154  if (!$filefd) {
155  if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) {
156  global $dolibarr_main_prod;
157  // Do not break dolibarr usage if log fails
158  //throw new Exception('Failed to open log file '.basename($logfile));
159  print 'Failed to open log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
160  }
161  } else {
162  $logLevels = array(
163  LOG_EMERG => 'EMERG',
164  LOG_ALERT => 'ALERT',
165  LOG_CRIT => 'CRIT',
166  LOG_ERR => 'ERR',
167  LOG_WARNING => 'WARNING',
168  LOG_NOTICE => 'NOTICE',
169  LOG_INFO => 'INFO',
170  LOG_DEBUG => 'DEBUG'
171  );
172 
173  $delay = "";
174  if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
175  $now = microtime(true);
176  $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
177  $this->lastTime = $now;
178  }
179 
180  // @phan-suppress-next-line PhanParamSuspiciousOrder
181  $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip']);
182  $message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
183  $message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
184  $message .= " ".($this->ident > 0 ? str_pad(((string) ''), ((int) $this->ident), ((string) ' ')) : '').$content['message'];
185  fwrite($filefd, $message."\n");
186  fclose($filefd);
187  dolChmod($logfile);
188  }
189  }
190 }
Parent class for log handlers.
Definition: logHandler.php:25
Class to manage logging to a file.
isActive()
Is the logger active ?
getInfo()
Content of the info tooltip.
getName()
Return name of logger.
getVersion()
Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
checkConfiguration()
Return if configuration is valid.
export($content, $suffixinfilename='')
Export the message.
getFilename($suffixinfilename='')
Return the parsed logfile path.
configure()
Return array of configuration data.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.