dolibarr  7.0.0-beta
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)) // file depend on session name that is same for all user, not per user value of the session id
113  {
114  $suffixinfilename = '_'.session_name();
115  }
116 
117  return $suffixinfilename?preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp):$tmp;
118  }
119 
127  public function export($content, $suffixinfilename='')
128  {
129  global $conf, $dolibarr_main_prod;
130 
131  if (! empty($conf->global->MAIN_SYSLOG_DISABLE_FILE)) return; // Global option to disable output of this handler
132 
133  $logfile = $this->getFilename($suffixinfilename);
134 
135  // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
136  if (defined('SYSLOG_FILE_NO_ERROR')) $filefd = @fopen($logfile, 'a+');
137  else $filefd = fopen($logfile, 'a+');
138 
139  if (! $filefd)
140  {
141  if (! defined('SYSLOG_FILE_NO_ERROR') || ! constant('SYSLOG_FILE_NO_ERROR'))
142  {
143  // Do not break dolibarr usage if log fails
144  //throw new Exception('Failed to open log file '.basename($logfile));
145  print 'Failed to open log file '.($dolibarr_main_prod?basename($logfile):$logfile);
146  }
147  }
148  else
149  {
150  $logLevels = array(
151  LOG_EMERG => 'EMERG',
152  LOG_ALERT => 'ALERT',
153  LOG_CRIT => 'CRIT',
154  LOG_ERR => 'ERR',
155  LOG_WARNING => 'WARNING',
156  LOG_NOTICE => 'NOTICE',
157  LOG_INFO => 'INFO',
158  LOG_DEBUG => 'DEBUG'
159  );
160 
161  $delay = "";
162  if (!empty($conf->global->MAIN_SYSLOG_SHOW_DELAY))
163  {
164  $now = microtime(true);
165  $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
166  $this->lastTime = $now;
167  }
168 
169  $message = dol_print_date(time(),"%Y-%m-%d %H:%M:%S").$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident>0?str_pad('',$this->ident,' '):'').$content['message'];
170  fwrite($filefd, $message."\n");
171  fclose($filefd);
172  @chmod($logfile, octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
173  }
174  }
175 }
getName()
Return name of logger.
LogHandlerInterface.
getVersion()
Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
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.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getInfo()
Content of the info tooltip.
print
Draft customers invoices.
Definition: index.php:91
isActive()
Is the module active ?
Parent class for log handlers.
Definition: logHandler.php:23
checkConfiguration()
Return if configuration is valid.