/
mod_syslog_file.php
190 lines (167 loc) · 5.19 KB
/
mod_syslog_file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
*/
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
/**
* Class to manage logging to a file
*/
class mod_syslog_file extends LogHandler
{
public $code = 'file';
public $lastTime = 0;
/**
* Return name of logger
*
* @return string Name of logger
*/
public function getName()
{
global $langs;
return $langs->trans('File');
}
/**
* Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
*
* @return string
*/
public function getVersion()
{
return 'dolibarr';
}
/**
* Content of the info tooltip.
*
* @return false|string
*/
public function getInfo()
{
global $langs;
return $langs->trans('YouCanUseDOL_DATA_ROOT');
}
/**
* Is the logger active ?
*
* @return int 1 if logger enabled
*/
public function isActive()
{
return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_FILE') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
}
/**
* Return array of configuration data
*
* @return array Return array of configuration data
*/
public function configure()
{
global $langs;
return array(
array(
'name' => $langs->trans('SyslogFilename'),
'constant' => 'SYSLOG_FILE',
'default' => 'DOL_DATA_ROOT/dolibarr.log',
'css' => 'minwidth300 maxwidth500'
)
);
}
/**
* Return if configuration is valid
*
* @return bool true if ok
*/
public function checkConfiguration()
{
global $langs;
$filename = $this->getFilename();
if (file_exists($filename) && is_writable($filename)) {
dol_syslog('admin/syslog: file '.$filename);
return true;
} else {
$this->errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
return false;
}
}
/**
* Return the parsed logfile path
*
* @param string $suffixinfilename When output is a file, append this suffix into default log filename.
* @return string
*/
private function getFilename($suffixinfilename = '')
{
global $conf;
if (!getDolGlobalString('SYSLOG_FILE')) {
$tmp = DOL_DATA_ROOT.'/dolibarr.log';
} else {
$tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
}
if (getDolGlobalString('SYSLOG_FILE_ONEPERSESSION')) {
if (is_numeric(getDolGlobalString('SYSLOG_FILE_ONEPERSESSION'))) {
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)
$suffixinfilename .= '_'.session_name();
}
if (getDolGlobalInt('SYSLOG_FILE_ONEPERSESSION') == 2) { // file depend on instance session key name + ip so nearly per user
$suffixinfilename .= '_'.session_name().'_'.$_SERVER["REMOTE_ADDR"];
}
} else {
$suffixinfilename .= '_' . getDolGlobalString('SYSLOG_FILE_ONEPERSESSION');
}
}
return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
}
/**
* Export the message
*
* @param array $content Array containing the info about the message
* @param string $suffixinfilename When output is a file, append this suffix into default log filename.
* @return void
* @phan-suppress PhanPluginDuplicateArrayKey
*/
public function export($content, $suffixinfilename = '')
{
if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
return; // Global option to disable output of this handler
}
$logfile = $this->getFilename($suffixinfilename);
// Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
if (defined('SYSLOG_FILE_NO_ERROR')) {
$filefd = @fopen($logfile, 'a+');
} else {
$filefd = fopen($logfile, 'a+');
}
if (!$filefd) {
if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) {
global $dolibarr_main_prod;
// Do not break dolibarr usage if log fails
//throw new Exception('Failed to open log file '.basename($logfile));
print 'Failed to open log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
}
} else {
$logLevels = array(
LOG_EMERG => 'EMERG',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRIT',
LOG_ERR => 'ERR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG'
);
$delay = "";
if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
$now = microtime(true);
$delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
$this->lastTime = $now;
}
// @phan-suppress-next-line PhanParamSuspiciousOrder
$message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip']);
$message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
$message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
$message .= " ".($this->ident > 0 ? str_pad(((string) ''), ((int) $this->ident), ((string) ' ')) : '').$content['message'];
fwrite($filefd, $message."\n");
fclose($filefd);
dolChmod($logfile);
}
}
}