dolibarr 21.0.0-beta
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 // Deprecated, use instead the constant 'SYSLOG_FILE_ADDSUFFIX' and/or 'SYSLOG_FILE_ONEPERIP'
122 if (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
123 if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
124 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)
125 $suffixinfilename .= '_'.session_name();
126 }
127 if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
128 $suffixinfilename .= '_'.session_name().'_'.getUserRemoteIP();
129 }
130 } else {
131 $suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
132 }
133 }
134
135 if (defined('SYSLOG_FILE_ADDSUFFIX')) {
136 $suffixinfilename .= '_' . constant('SYSLOG_FILE_ADDSUFFIX');
137 }
138 if (defined('SYSLOG_FILE_ADDIP')) {
139 $suffixinfilename .= '_'.getUserRemoteIP();
140 }
141
142 return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
143 }
144
145
146 // @phan-suppress-next-line PhanPluginDuplicateArrayKey
147 const DOL_LOG_LEVELS = array(
148 LOG_EMERG => 'EMERG',
149 LOG_ALERT => 'ALERT',
150 LOG_CRIT => 'CRIT',
151 LOG_ERR => 'ERR',
152 LOG_WARNING => 'WARNING',
153 LOG_NOTICE => 'NOTICE',
154 LOG_INFO => 'INFO',
155 LOG_DEBUG => 'DEBUG'
156 );
157
166 public function export($content, $suffixinfilename = '')
167 {
168 if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
169 return; // Global option to disable output of this handler
170 }
171
172
173 // Prepare log message
174
175 $delay = "";
176 if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
177 $now = microtime(true);
178 $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
179 $this->lastTime = $now;
180 }
181 $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", self::DOL_LOG_LEVELS[$content['level']])." ".sprintf("%-15s", $content['ip']);
182 $message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
183 $message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
184 // @phan-suppress-next-line PhanParamSuspiciousOrder
185 $message .= " ".($this->ident > 0 ? str_pad('', ((int) $this->ident), ' ') : '').$content['message'];
186 $message .= "\n";
187
188
189 // Write log message
190
191 $logfile = $this->getFilename($suffixinfilename);
192
193 $result = false;
194 if (defined('SYSLOG_FILE_NO_ERROR')) {
195 $filefd = @fopen($logfile, "a");
196 } else {
197 $filefd = fopen($logfile, "a");
198 }
199
200 if ($filefd !== false) {
201 $result = fwrite($filefd, $message);
202 fclose($filefd);
203 dolChmod($logfile);
204 }
205 if ($result === false && (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR'))) {
206 global $dolibarr_main_prod;
207 // Do not break dolibarr usage if log fails
208 //throw new Exception('Failed to open log file '.basename($logfile));
209 print 'Failed to write to log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
210 }
211 }
212}
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.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79