Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
syslog.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Log
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * @package Joomla.Platform
21  * @subpackage Log
22  * @since 11.1
23  */
25 {
26  /**
27  * @var array Translation array for JLogEntry priorities to SysLog priority names.
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  * Constructor.
42  *
43  * @param array &$options Log object options.
44  *
45  * @since 11.1
46  */
47  public function __construct(array &$options)
48  {
49  // Call the parent constructor.
50  parent::__construct($options);
51 
52  // Ensure that we have an identity string for the Syslog entries.
53  if (empty($this->options['sys_ident']))
54  {
55  $this->options['sys_ident'] = 'Joomla Platform';
56  }
57 
58  // If the option to add the process id to Syslog entries is set use it, otherwise default to true.
59  if (isset($this->options['sys_add_pid']))
60  {
61  $this->options['sys_add_pid'] = (bool) $this->options['sys_add_pid'];
62  }
63  else
64  {
65  $this->options['sys_add_pid'] = true;
66  }
67 
68  // If the option to also send Syslog entries to STDERR is set use it, otherwise default to false.
69  if (isset($this->options['sys_use_stderr']))
70  {
71  $this->options['sys_use_stderr'] = (bool) $this->options['sys_use_stderr'];
72  }
73  else
74  {
75  $this->options['sys_use_stderr'] = false;
76  }
77 
78  // Build the Syslog options from our log object options.
79  $sysOptions = 0;
80 
81  if ($this->options['sys_add_pid'])
82  {
83  $sysOptions = $sysOptions | LOG_PID;
84  }
85  if ($this->options['sys_use_stderr'])
86  {
87  $sysOptions = $sysOptions | LOG_PERROR;
88  }
89 
90  // Default logging facility is LOG_USER for Windows compatibility.
91  $sysFacility = LOG_USER;
92 
93  // If we have a facility passed in and we're not on Windows, reset it.
94  if (isset($this->options['sys_facility']) && !IS_WIN)
95  {
96  $sysFacility = $this->options['sys_facility'];
97  }
98 
99  // Open the Syslog connection.
100  openlog((string) $this->options['sys_ident'], $sysOptions, $sysFacility);
101  }
102 
103  /**
104  * Destructor.
105  *
106  * @since 11.1
107  */
108  public function __destruct()
109  {
110  closelog();
111  }
112 
113  /**
114  * Method to add an entry to the log.
115  *
116  * @param JLogEntry $entry The log entry object to add to the log.
117  *
118  * @return void
119  *
120  * @since 11.1
121  */
122  public function addEntry(JLogEntry $entry)
123  {
124  // Generate the value for the priority based on predefined constants.
125  $priority = constant(strtoupper('LOG_' . $this->priorities[$entry->priority]));
126 
127  // Send the entry to Syslog.
128  syslog($priority, '[' . $entry->category . '] ' . $entry->message);
129  }
130 }