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