1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Log
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Joomla! Syslog Log class
14 *
15 * This class is designed to call the PHP Syslog function call which is then sent to the
16 * system wide log system. For Linux/Unix based systems this is the syslog subsystem, for
17 * the Windows based implementations this can be found in the Event Log. For Windows,
18 * permissions may prevent PHP from properly outputting messages.
19 *
20 * @since 11.1
21 */
22 class JLogLoggerSyslog extends JLogLogger
23 {
24 /**
25 * Translation array for JLogEntry priorities to SysLog priority names.
26 *
27 * @var array
28 * @since 11.1
29 */
30 protected $priorities = array(
31 JLog::EMERGENCY => 'EMERG',
32 JLog::ALERT => 'ALERT',
33 JLog::CRITICAL => 'CRIT',
34 JLog::ERROR => 'ERR',
35 JLog::WARNING => 'WARNING',
36 JLog::NOTICE => 'NOTICE',
37 JLog::INFO => 'INFO',
38 JLog::DEBUG => 'DEBUG',
39 );
40
41 /**
42 * Constructor.
43 *
44 * @param array &$options Log object options.
45 *
46 * @since 11.1
47 */
48 public function __construct(array &$options)
49 {
50 // Call the parent constructor.
51 parent::__construct($options);
52
53 // Ensure that we have an identity string for the Syslog entries.
54 if (empty($this->options['sys_ident']))
55 {
56 $this->options['sys_ident'] = 'Joomla Platform';
57 }
58
59 // If the option to add the process id to Syslog entries is set use it, otherwise default to true.
60 if (isset($this->options['sys_add_pid']))
61 {
62 $this->options['sys_add_pid'] = (bool) $this->options['sys_add_pid'];
63 }
64 else
65 {
66 $this->options['sys_add_pid'] = true;
67 }
68
69 // If the option to also send Syslog entries to STDERR is set use it, otherwise default to false.
70 if (isset($this->options['sys_use_stderr']))
71 {
72 $this->options['sys_use_stderr'] = (bool) $this->options['sys_use_stderr'];
73 }
74 else
75 {
76 $this->options['sys_use_stderr'] = false;
77 }
78
79 // Build the Syslog options from our log object options.
80 $sysOptions = 0;
81
82 if ($this->options['sys_add_pid'])
83 {
84 $sysOptions = $sysOptions | LOG_PID;
85 }
86
87 if ($this->options['sys_use_stderr'])
88 {
89 $sysOptions = $sysOptions | LOG_PERROR;
90 }
91
92 // Default logging facility is LOG_USER for Windows compatibility.
93 $sysFacility = LOG_USER;
94
95 // If we have a facility passed in and we're not on Windows, reset it.
96 if (isset($this->options['sys_facility']) && !IS_WIN)
97 {
98 $sysFacility = $this->options['sys_facility'];
99 }
100
101 // Open the Syslog connection.
102 openlog((string) $this->options['sys_ident'], $sysOptions, $sysFacility);
103 }
104
105 /**
106 * Destructor.
107 *
108 * @since 11.1
109 */
110 public function __destruct()
111 {
112 closelog();
113 }
114
115 /**
116 * Method to add an entry to the log.
117 *
118 * @param JLogEntry $entry The log entry object to add to the log.
119 *
120 * @return void
121 *
122 * @since 11.1
123 */
124 public function addEntry(JLogEntry $entry)
125 {
126 // Generate the value for the priority based on predefined constants.
127 $priority = constant(strtoupper('LOG_' . $this->priorities[$entry->priority]));
128
129 // Send the entry to Syslog.
130 syslog($priority, '[' . $entry->category . '] ' . $entry->message);
131 }
132 }
133