dolibarr 23.0.3
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{
13 // @phan-suppress-next-line PhanPluginDuplicateArrayKey
14 const DOL_LOG_LEVELS = array(
15 LOG_EMERG => 'EMERG',
16 LOG_ALERT => 'ALERT',
17 LOG_CRIT => 'CRIT',
18 LOG_ERR => 'ERR',
19 LOG_WARNING => 'WARNING',
20 LOG_NOTICE => 'NOTICE',
21 LOG_INFO => 'INFO',
22 LOG_DEBUG => 'DEBUG'
23 );
24
28 public $code = 'file';
32 public $lastTime = 0;
33
39 public function getName()
40 {
41 global $langs;
42
43 return $langs->trans('File');
44 }
45
51 public function getVersion()
52 {
53 return 'dolibarr';
54 }
55
61 public function getInfo()
62 {
63 global $langs;
64
65 return $langs->trans('YouCanUseDOL_DATA_ROOT');
66 }
67
73 public function isActive()
74 {
75 return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_FILE') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
76 }
77
83 public function configure()
84 {
85 global $langs;
86
87 return array(
88 array(
89 'name' => $langs->trans('SyslogFilename'),
90 'constant' => 'SYSLOG_FILE',
91 'default' => 'DOL_DATA_ROOT/dolibarr.log',
92 'css' => 'minwidth300 maxwidth500'
93 )
94 );
95 }
96
102 public function checkConfiguration()
103 {
104 global $langs;
105
106 $filename = $this->getFilename();
107
108 if (file_exists($filename) && is_writable($filename)) {
109 dol_syslog('admin/syslog: file '.$filename);
110 return true;
111 } else {
112 $this->errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
113 return false;
114 }
115 }
116
123 private function getFilename($suffixinfilename = '')
124 {
125 global $conf;
126
127 if (!getDolGlobalString('SYSLOG_FILE')) {
128 $tmp = DOL_DATA_ROOT.'/dolibarr.log';
129 } else {
130 $tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
131 }
132
133 // Deprecated, use instead the constant 'SYSLOG_FILE_ADDSUFFIX' and/or 'SYSLOG_FILE_ONEPERIP'
134 if (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
135 if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
136 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)
137 $suffixinfilename .= '_'.session_name();
138 }
139 if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
140 $suffixinfilename .= '_'.session_name().'_'.getUserRemoteIP();
141 }
142 } else {
143 $suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
144 }
145 }
146
147 if (defined('SYSLOG_FILE_ADDSUFFIX')) {
148 $suffixinfilename .= '_' . constant('SYSLOG_FILE_ADDSUFFIX');
149 }
150 if (defined('SYSLOG_FILE_ADDIP')) {
151 $suffixinfilename .= '_'.getUserRemoteIP();
152 }
153
154 return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
155 }
156
165 public function export($content, $suffixinfilename = '')
166 {
167 if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
168 return; // Global option to disable output of this handler
169 }
170
171
172 // Prepare log message
173
174 $delay = "";
175 if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
176 $now = microtime(true);
177 $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
178 $this->lastTime = $now;
179 }
180 $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", self::DOL_LOG_LEVELS[$content['level']])." ".sprintf("%-15s", $content['ip']);
181 $message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
182 $message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
183 // @phan-suppress-next-line PhanParamSuspiciousOrder
184 $message .= " ".($this->ident > 0 ? str_pad('', ((int) $this->ident), ' ') : '').$content['message'];
185 $message .= "\n";
186
187
188 // Write log message
189
190 $logfile = $this->getFilename($suffixinfilename);
191
192 $result = false;
193 if (defined('SYSLOG_FILE_NO_ERROR')) {
194 $filefd = @fopen($logfile, "a");
195 } else {
196 $filefd = fopen($logfile, "a");
197 }
198
199 if ($filefd !== false) {
200 $result = fwrite($filefd, $message);
201 fclose($filefd);
202 dolChmod($logfile);
203 }
204 if ($result === false && (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR'))) {
205 global $dolibarr_main_prod;
206 // Do not break dolibarr usage if log fails
207 //throw new Exception('Failed to open log file '.basename($logfile));
208 print 'Failed to write to log file '.($dolibarr_main_prod ? basename($logfile) : $logfile)."\n";
209 }
210 }
211}
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.
dol_now($mode='gmt')
Return date for now.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
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.