1: <?php
2: /**
3: * This class extends the base Horde_Log_Logger class to ensure that the log
4: * entries are consistently generated across the applications and framework
5: * libraries.
6: *
7: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
8: *
9: * See the enclosed file COPYING for license information (LGPL). If you
10: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
11: *
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @category Horde
14: * @package Core
15: */
16: class Horde_Core_Log_Logger extends Horde_Log_Logger
17: {
18: /**
19: * Logs a message to the global Horde log backend.
20: *
21: * @param mixed $event Either a string (log string), an array
22: * (containing 'level', 'message', and 'timestamp'
23: * entries) or an object with a getMessage()
24: * method (e.g. PEAR_Error, Exception,
25: * ErrorException).
26: * @param mixed $priority The priority of the message. Integers
27: * correspond to Horde_Log constants. String
28: * values are auto translated to Horde_Log
29: * constants.
30: * @param array $options Additional options:
31: * <pre>
32: * 'file' - (string) The filename to use in the log message.
33: * 'line' - (integer) The file line to use in the log message.
34: * 'trace' - (integer) The trace level of the original log location.
35: * </pre>
36: */
37: public function log($event, $priority = null, $options = array())
38: {
39: /* If an array is passed in, assume that the caller knew what they
40: * were doing and pass it directly to the log backend. */
41: if (is_array($event)) {
42: return parent::log($event, constant('Horde_Log::' . $priority));
43: }
44:
45: if ($event instanceof Exception) {
46: if (is_null($priority)) {
47: $priority = Horde_Log::ERR;
48: }
49: $text = $event->getMessage();
50: if (!empty($event->details)) {
51: $text .= ' ' . $event->details;
52: }
53: $trace = array(
54: 'file' => $event->getFile(),
55: 'line' => $event->getLine()
56: );
57: } else {
58: if ($event instanceof PEAR_Error) {
59: if (is_null($priority)) {
60: $priority = Horde_Log::ERR;
61: }
62: $userinfo = $event->getUserInfo();
63: $text = $event->getMessage();
64: if (!empty($userinfo)) {
65: if (is_array($userinfo)) {
66: $userinfo = @implode(', ', $userinfo);
67: }
68: $text .= ': ' . $userinfo;
69: }
70: } elseif (is_object($event)) {
71: $text = strval($event);
72: if (!is_string($text)) {
73: $text = is_callable(array($event, 'getMessage'))
74: ? $event->getMessage()
75: : '';
76: }
77: } else {
78: $text = $event;
79: }
80:
81: $trace = debug_backtrace();
82: if (isset($options['trace'])) {
83: $frame = $options['trace'] - 1;
84: } elseif (count($trace) > 1 &&
85: $trace[1]['class'] == 'Horde_Log_Logger' &&
86: $trace[1]['function'] == '__call') {
87: $frame = 2;
88: } else {
89: $frame = 0;
90: }
91: $trace = $trace[$frame];
92: }
93:
94: if (is_null($priority)) {
95: $priority = Horde_Log::INFO;
96: } elseif (is_string($priority)) {
97: $priority = defined('Horde_Log::' . $priority)
98: ? constant('Horde_Log::' . $priority)
99: : Horde_Log::INFO;
100: }
101:
102: $file = isset($options['file'])
103: ? $options['file']
104: : $trace['file'];
105: $line = isset($options['line'])
106: ? $options['line']
107: : $trace['line'];
108:
109: $app = isset($GLOBALS['registry'])
110: ? $GLOBALS['registry']->getApp()
111: : 'horde';
112:
113: $message = ($app ? '[' . $app . '] ' : '') .
114: $text .
115: ' [pid ' . getmypid() . ' on line ' . $line . ' of "' . $file . '"]';
116:
117: /* Make sure to log in the system's locale and timezone. */
118: // TODO: Needed?
119: $locale = setlocale(LC_TIME, 0);
120: setlocale(LC_TIME, 'C');
121: $tz = getenv('TZ');
122: @putenv('TZ');
123:
124: $eventob = array(
125: 'level' => $priority,
126: 'message' => $message,
127: );
128:
129: if (!empty($GLOBALS['conf']['log']['time_format'])) {
130: $eventob['timestamp'] = date($GLOBALS['conf']['log']['time_format']);
131: }
132:
133: parent::log($eventob);
134:
135: /* If logging an exception, log the backtrace too. */
136: if (($event instanceof Exception) &&
137: class_exists('Horde_Support_Backtrace')) {
138: parent::log((string)new Horde_Support_Backtrace($event), Horde_Log::DEBUG);
139: }
140:
141: /* Restore original locale and timezone. */
142: setlocale(LC_TIME, $locale);
143: if ($tz) {
144: @putenv('TZ=' . $tz);
145: }
146: }
147:
148: }
149: