dolibarr 20.0.2
mod_syslog_syslog.php
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 */
5
6require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
7
12{
13 public $code = 'syslog';
14
20 public function getName()
21 {
22 return 'Syslog';
23 }
24
30 public function getVersion()
31 {
32 return 'dolibarr';
33 }
34
40 public function getInfo()
41 {
42 global $langs;
43
44 return $langs->trans('OnlyWindowsLOG_USER');
45 }
46
52 public function isActive()
53 {
54 // This function does not exists on some ISP (Ex: Free in France)
55 if (!function_exists('openlog')) {
56 return 0;
57 }
58
59 return !getDolGlobalString('SYSLOG_DISABLE_LOGHANDLER_SYSLOG') ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_SYSLOG to 1 to disable this loghandler
60 }
61
67 public function configure()
68 {
69 global $langs;
70
71 return array(
72 array(
73 'constant' => 'SYSLOG_FACILITY',
74 'name' => $langs->trans('SyslogFacility'),
75 'default' => 'LOG_USER'
76 )
77 );
78 }
79
85 public function checkConfiguration()
86 {
87 global $langs;
88
89 $facility = constant(getDolGlobalString('SYSLOG_FACILITY'));
90
91 if ($facility) {
92 // Only LOG_USER supported on Windows
93 if (!empty($_SERVER["WINDIR"])) {
94 $facility = constant('LOG_USER');
95 }
96
97 dol_syslog("admin/syslog: facility ".$facility);
98 return true;
99 } else {
100 $this->errors[] = $langs->trans("ErrorUnknownSyslogConstant", $facility);
101 return false;
102 }
103 }
104
112 public function export($content, $suffixinfilename = '')
113 {
114 global $conf;
115
116 if (getDolGlobalString('MAIN_SYSLOG_DISABLE_SYSLOG')) {
117 return; // Global option to disable output of this handler
118 }
119
120 if (getDolGlobalString('SYSLOG_FACILITY')) { // Example LOG_USER
121 $facility = constant($conf->global->SYSLOG_FACILITY);
122 } else {
123 $facility = constant('LOG_USER');
124 }
125
126 // (int) is required to avoid error parameter 3 expected to be long
127 openlog('dolibarr', LOG_PID | LOG_PERROR, (int) $facility);
128
129 $message = sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
130 $message .= " ".$content['message'];
131
132 syslog($content['level'], $message);
133 closelog();
134 }
135}
Parent class for log handlers.
Class to manage logging to syslog.
export($content, $suffixinfilename='')
Export the message.
configure()
Return array of configuration data.
isActive()
Is the logger active ?
getVersion()
Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
checkConfiguration()
Return if configuration is valid.
getInfo()
Content of the info tooltip.
getName()
Return name of logger.
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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.