ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
syslog.php
Go to the documentation of this file.
1 <?php
23 class Log_syslog extends Log
24 {
30  var $_name = LOG_SYSLOG;
31 
38  var $_inherit = false;
39 
49  function Log_syslog($name, $ident = '', $conf = array(),
50  $level = PEAR_LOG_DEBUG)
51  {
52  /* Ensure we have a valid integer value for $name. */
53  if (empty($name) || !is_int($name)) {
54  $name = LOG_SYSLOG;
55  }
56 
57  if (isset($conf['inherit'])) {
58  $this->_inherit = $conf['inherit'];
59  $this->_opened = $this->_inherit;
60  }
61 
62  $this->_id = md5(microtime());
63  $this->_name = $name;
64  $this->_ident = $ident;
65  $this->_mask = Log::UPTO($level);
66  }
67 
73  function open()
74  {
75  if (!$this->_opened) {
76  $this->_opened = openlog($this->_ident, LOG_PID, $this->_name);
77  }
78 
79  return $this->_opened;
80  }
81 
86  function close()
87  {
88  if ($this->_opened && !$this->_inherit) {
89  closelog();
90  $this->_opened = false;
91  }
92 
93  return true;
94  }
95 
109  function log($message, $priority = null)
110  {
111  /* If a priority hasn't been specified, use the default value. */
112  if ($priority === null) {
113  $priority = $this->_priority;
114  }
115 
116  /* Abort early if the priority is above the maximum logging level. */
117  if (!$this->_isMasked($priority)) {
118  return false;
119  }
120 
121  /* If the connection isn't open and can't be opened, return failure. */
122  if (!$this->_opened && !$this->open()) {
123  return false;
124  }
125 
126  /* Extract the string representation of the message. */
127  $message = $this->_extractMessage($message);
128 
129  /* Build a syslog priority value based on our current configuration. */
130  $priority = $this->_toSyslog($priority);
131  if ($this->_inherit) {
132  $priority |= $this->_name;
133  }
134 
135  if (!syslog($priority, $message)) {
136  return false;
137  }
138 
139  $this->_announce(array('priority' => $priority, 'message' => $message));
140 
141  return true;
142  }
143 
158  function _toSyslog($priority)
159  {
160  static $priorities = array(
161  PEAR_LOG_EMERG => LOG_EMERG,
162  PEAR_LOG_ALERT => LOG_ALERT,
163  PEAR_LOG_CRIT => LOG_CRIT,
164  PEAR_LOG_ERR => LOG_ERR,
165  PEAR_LOG_WARNING => LOG_WARNING,
166  PEAR_LOG_NOTICE => LOG_NOTICE,
167  PEAR_LOG_INFO => LOG_INFO,
168  PEAR_LOG_DEBUG => LOG_DEBUG
169  );
170 
171  /* If we're passed an unknown priority, default to LOG_INFO. */
172  if (!is_int($priority) || !in_array($priority, $priorities)) {
173  return LOG_INFO;
174  }
175 
176  return $priorities[$priority];
177  }
178 
179 }