dolibarr 21.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
6require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
7
12{
16 public $code = 'file';
20 public $lastTime = 0;
21
27 public function getName()
28 {
29 global $langs;
30
31 return $langs->trans('File');
32 }
33
39 public function getVersion()
40 {
41 return 'dolibarr';
42 }
43
49 public function getInfo()
50 {
51 global $langs;
52
53 return $langs->trans('YouCanUseDOL_DATA_ROOT');
54 }
55
61 public function isActive()
62 {
63 return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_FILE') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
64 }
65
71 public function configure()
72 {
73 global $langs;
74
75 return array(
76 array(
77 'name' => $langs->trans('SyslogFilename'),
78 'constant' => 'SYSLOG_FILE',
79 'default' => 'DOL_DATA_ROOT/dolibarr.log',
80 'css' => 'minwidth300 maxwidth500'
81 )
82 );
83 }
84
90 public function checkConfiguration()
91 {
92 global $langs;
93
94 $filename = $this->getFilename();
95
96 if (file_exists($filename) && is_writable($filename)) {
97 dol_syslog('admin/syslog: file '.$filename);
98 return true;
99 } else {
100 $this->errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
101 return false;
102 }
103 }
104
111 private function getFilename($suffixinfilename = '')
112 {
113 global $conf;
114
115 if (!getDolGlobalString('SYSLOG_FILE')) {
116 $tmp = DOL_DATA_ROOT.'/dolibarr.log';
117 } else {
118 $tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
119 }
120
121 if (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
122 if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
123 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)
124 $suffixinfilename .= '_'.session_name();
125 }
126 if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
127 $suffixinfilename .= '_'.session_name().'_'.$_SERVER["REMOTE_ADDR"];
128 }
129 } else {
130 $suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
131 }
132 }
133
134 return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
135 }
136
137
138 // @phan-suppress-next-line PhanPluginDuplicateArrayKey
139 const DOL_LOG_LEVELS = array(
140 LOG_EMERG => 'EMERG',
141 LOG_ALERT => 'ALERT',
142 LOG_CRIT => 'CRIT',
143 LOG_ERR => 'ERR',
144 LOG_WARNING => 'WARNING',
145 LOG_NOTICE => 'NOTICE',
146 LOG_INFO => 'INFO',
147 LOG_DEBUG => 'DEBUG'
148 );
149
158 public function export($content, $suffixinfilename = '')
159 {
160 if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
161 return; // Global option to disable output of this handler
162 }
163
164
165 // Prepare log message
166
167 $delay = "";
168 if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
169 $now = microtime(true);
170 $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
171 $this->lastTime = $now;
172 }
173 $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", self::DOL_LOG_LEVELS[$content['level']])." ".sprintf("%-15s", $content['ip']);
174 $message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
175 $message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
176 // @phan-suppress-next-line PhanParamSuspiciousOrder
177 $message .= " ".($this->ident > 0 ? str_pad('', ((int) $this->ident), ' ') : '').$content['message'];
178 $message .= "\n";
179
180
181 // Write log message
182
183 $logfile = $this->getFilename($suffixinfilename);
184
185 $result = false;
186 if (defined('SYSLOG_FILE_NO_ERROR')) {
187 $filefd = @fopen($logfile, "a");
188 } else {
189 $filefd = fopen($logfile, "a");
190 }
191
192 if ($filefd !== false) {
193 $result = fwrite($filefd, $message);
194 fclose($filefd);
195 dolChmod($logfile);
196 }
197 if ($result === false && (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR'))) {
198 global $dolibarr_main_prod;
199 // Do not break dolibarr usage if log fails
200 //throw new Exception('Failed to open log file '.basename($logfile));
201 print 'Failed to write to log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
202 }
203 }
204}
Parent class for log handlers.
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.