dolibarr  9.0.0
mod_syslog_file.php
1 <?php
2 
3 require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
4 
9 {
10  var $code = 'file';
11  var $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  {
92  dol_syslog('admin/syslog: file '.$filename);
93  }
94  else $errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
95 
96  return $errors;
97  }
98 
105  private function getFilename($suffixinfilename='')
106  {
107  global $conf;
108 
109  if (empty($conf->global->SYSLOG_FILE)) $tmp=DOL_DATA_ROOT.'/dolibarr.log';
110  else $tmp=str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
111 
112  if (! empty($conf->global->SYSLOG_FILE_ONEPERSESSION))
113  {
114  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)
115  {
116  $suffixinfilename .= '_'.session_name();
117  }
118  if ($conf->global->SYSLOG_FILE_ONEPERSESSION == 2) // file depend on session value sor per user
119  {
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)) return; // Global option to disable output of this handler
139 
140  $logfile = $this->getFilename($suffixinfilename);
141 
142  // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
143  if (defined('SYSLOG_FILE_NO_ERROR')) $filefd = @fopen($logfile, 'a+');
144  else $filefd = fopen($logfile, 'a+');
145 
146  if (! $filefd)
147  {
148  if (! defined('SYSLOG_FILE_NO_ERROR') || ! constant('SYSLOG_FILE_NO_ERROR'))
149  {
150  // Do not break dolibarr usage if log fails
151  //throw new Exception('Failed to open log file '.basename($logfile));
152  print 'Failed to open log file '.($dolibarr_main_prod?basename($logfile):$logfile);
153  }
154  }
155  else
156  {
157  $logLevels = array(
158  LOG_EMERG => 'EMERG',
159  LOG_ALERT => 'ALERT',
160  LOG_CRIT => 'CRIT',
161  LOG_ERR => 'ERR',
162  LOG_WARNING => 'WARNING',
163  LOG_NOTICE => 'NOTICE',
164  LOG_INFO => 'INFO',
165  LOG_DEBUG => 'DEBUG'
166  );
167 
168  $delay = "";
169  if (!empty($conf->global->MAIN_SYSLOG_SHOW_DELAY))
170  {
171  $now = microtime(true);
172  $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
173  $this->lastTime = $now;
174  }
175 
176  $message = strftime("%Y-%m-%d %H:%M:%S", time()).$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 }
getName()
Return name of logger.
print
Draft customers invoices.
Definition: index.php:91
LogHandlerInterface.
getVersion()
Version of the module (&#39;x.y.z&#39; or &#39;dolibarr&#39; or &#39;experimental&#39; or &#39;development&#39;)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage logging to a file.
getFilename($suffixinfilename='')
Return the parsed logfile path.
export($content, $suffixinfilename='')
Export the message.
configure()
Return array of configuration data.
getInfo()
Content of the info tooltip.
isActive()
Is the module active ?
Parent class for log handlers.
Definition: logHandler.php:23
checkConfiguration()
Return if configuration is valid.