-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
mod_syslog_file.php
175 lines (149 loc) · 4.32 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
<?php
require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
/**
* Class to manage logging to a file
*/
class mod_syslog_file extends LogHandler implements LogHandlerInterface
{
var $code = 'file';
var $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 module active ?
*
* @return int
*/
public function isActive()
{
global $conf;
return empty($conf->global->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',
'attr' => 'size="60"'
)
);
}
/**
* Return if configuration is valid
*
* @return array Array of errors. Empty array if ok.
*/
public function checkConfiguration()
{
global $langs;
$errors = array();
$filename = $this->getFilename();
if (file_exists($filename) && is_writable($filename))
{
dol_syslog('admin/syslog: file '.$filename);
}
else $errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
return $errors;
}
/**
* 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 (empty($conf->global->SYSLOG_FILE)) $tmp=DOL_DATA_ROOT.'/dolibarr.log';
else $tmp=str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
if (! empty($conf->global->SYSLOG_FILE_ONEPERSESSION)) // file depend on session name that is same for all user, not per user value of the session id
{
$suffixinfilename = '_'.session_name();
}
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
*/
public function export($content, $suffixinfilename='')
{
global $conf, $dolibarr_main_prod;
if (! empty($conf->global->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'))
{
// 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 (!empty($conf->global->MAIN_SYSLOG_SHOW_DELAY))
{
$now = microtime(true);
$delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
$this->lastTime = $now;
}
$message = dol_print_date(time(),"%Y-%m-%d %H:%M:%S").$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident>0?str_pad('',$this->ident,' '):'').$content['message'];
fwrite($filefd, $message."\n");
fclose($filefd);
@chmod($logfile, octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
}
}
}