dolibarr 25.0.0-alpha
mod_syslog_file.php
1<?php
2/* Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 */
5
6// File ignore as individual 'suppress' is not extremely effective.
7// @phan-file-suppress PhanPluginDuplicateArrayKey
8
9require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
10
15{
16 // @phan-suppress-next-line PhanPluginDuplicateArrayKey
17 const DOL_LOG_LEVELS = array(
18 LOG_EMERG => 'EMERG',
19 LOG_ALERT => 'ALERT',
20 LOG_CRIT => 'CRIT',
21 LOG_ERR => 'ERR',
22 LOG_WARNING => 'WARNING',
23 LOG_NOTICE => 'NOTICE',
24 LOG_INFO => 'INFO',
25 LOG_DEBUG => 'DEBUG'
26 );
27
31 public $code = 'file';
35 public $lastTime = 0;
36
42 public function getName()
43 {
44 global $langs;
45
46 return $langs->trans('File');
47 }
48
54 public function getVersion()
55 {
56 return 'dolibarr';
57 }
58
64 public function getInfo()
65 {
66 global $langs;
67
68 return $langs->trans('YouCanUseDOL_DATA_ROOT');
69 }
70
76 public function isActive()
77 {
78 return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_FILE') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
79 }
80
86 public function configure()
87 {
88 global $langs;
89
90 return array(
91 array(
92 'name' => $langs->trans('SyslogFilename'),
93 'constant' => 'SYSLOG_FILE',
94 'default' => 'DOL_DATA_ROOT/dolibarr.log',
95 'css' => 'minwidth300 maxwidth500'
96 )
97 );
98 }
99
105 public function checkConfiguration()
106 {
107 global $langs;
108
109 $filename = $this->getFilename();
110
111 if (file_exists($filename) && is_writable($filename)) {
112 dol_syslog('admin/syslog: file '.$filename);
113 return true;
114 } else {
115 $this->errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
116 return false;
117 }
118 }
119
126 private function getFilename($suffixinfilename = '')
127 {
128 global $conf;
129
130 if (!getDolGlobalString('SYSLOG_FILE')) {
131 $tmp = DOL_DATA_ROOT.'/dolibarr.log';
132 } else {
133 $tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
134 }
135
136 // Deprecated, use instead the constant 'SYSLOG_FILE_ADDSUFFIX' and/or 'SYSLOG_FILE_ONEPERIP'
137 if (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
138 if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
139 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)
140 $suffixinfilename .= '_'.session_name();
141 }
142 if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
143 $suffixinfilename .= '_'.session_name().'_'.getUserRemoteIP();
144 }
145 } else {
146 $suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
147 }
148 }
149
150 if (defined('SYSLOG_FILE_ADDSUFFIX')) {
151 $suffixinfilename .= '_' . constant('SYSLOG_FILE_ADDSUFFIX');
152 }
153 if (defined('SYSLOG_FILE_ADDIP')) {
154 $suffixinfilename .= '_'.getUserRemoteIP();
155 }
156
157 return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
158 }
159
167 public function export($content, $suffixinfilename = '')
168 {
169 if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
170 return; // Global option to disable output of this handler
171 }
172
173
174 // Prepare log message
175
176 $delay = "";
177 if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
178 $now = microtime(true);
179 $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
180 $this->lastTime = $now;
181 }
182 $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", self::DOL_LOG_LEVELS[$content['level']])." ".sprintf("%-15s", $content['ip']);
183 $message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
184 $message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
185 // @phan-suppress-next-line PhanParamSuspiciousOrder
186 $message .= " ".($this->ident > 0 ? str_pad('', ((int) $this->ident), ' ') : '').$content['message'];
187 $message .= "\n";
188
189
190 // Write log message
191
192 $logfile = $this->getFilename($suffixinfilename);
193
194 $result = false;
195 if (defined('SYSLOG_FILE_NO_ERROR') || !empty($suffixinfilename)) {
196 $filefd = @fopen($logfile, "a");
197 } else {
198 $filefd = fopen($logfile, "a");
199 }
200
201 if ($filefd !== false) {
202 $result = fwrite($filefd, $message);
203 fclose($filefd);
204 dolChmod($logfile);
205 }
206 if ($result === false && empty($suffixinfilename) && (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR'))) {
207 global $dolibarr_main_prod;
208 // Do not break dolibarr usage if log fails
209 //throw new Exception('Failed to open log file '.basename($logfile));
210 print 'Failed to write to log file '.($dolibarr_main_prod ? basename($logfile) : $logfile)."\n";
211 }
212 }
213}
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.