1: <?php
2: /**
3: * Horde Log package
4: *
5: * This package is based on Zend_Log from the Zend Framework
6: * (http://framework.zend.com). Both that package and this
7: * one were written by Mike Naberezny and Chuck Hagenbuch.
8: *
9: * @author Mike Naberezny <mike@maintainable.com>
10: * @author Chuck Hagenbuch <chuck@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/bsd BSD
13: * @package Log
14: * @subpackage Handlers
15: */
16:
17: /**
18: * @author Mike Naberezny <mike@maintainable.com>
19: * @author Chuck Hagenbuch <chuck@horde.org>
20: * @category Horde
21: * @license http://www.horde.org/licenses/bsd BSD
22: * @package Log
23: * @subpackage Handlers
24: */
25: class Horde_Log_Handler_Stream extends Horde_Log_Handler_Base
26: {
27: /**
28: * Formats the log message before writing.
29: *
30: * @var Horde_Log_Formatter
31: */
32: protected $_formatter;
33:
34: /**
35: * Holds the PHP stream to log to.
36: *
37: * @var null|stream
38: */
39: protected $_stream = null;
40:
41: /**
42: * The open mode.
43: *
44: * @var string
45: */
46: protected $_mode;
47:
48: /**
49: * The stream to open.
50: *
51: * @var string
52: */
53: protected $_streamOrUrl;
54:
55: /**
56: * Class Constructor
57: *
58: * @param mixed $streamOrUrl Stream or URL to open as a
59: * stream.
60: * @param string $mode Mode, only applicable if a URL
61: * is given.
62: * @param Horde_Log_Formatter $formatter Log formatter.
63: *
64: * @throws Horde_Log_Exception
65: */
66: public function __construct($streamOrUrl, $mode = 'a+',
67: Horde_Log_Formatter $formatter = null)
68: {
69: $this->_formatter = is_null($formatter)
70: ? new Horde_Log_Formatter_Simple()
71: : $formatter;
72: $this->_mode = $mode;
73: $this->_streamOrUrl = $streamOrUrl;
74:
75: if (is_resource($streamOrUrl)) {
76: if (get_resource_type($streamOrUrl) != 'stream') {
77: throw new Horde_Log_Exception(__CLASS__ . ': Resource is not a stream');
78: }
79:
80: if ($mode && $mode != 'a+') {
81: throw new Horde_Log_Exception(__CLASS__ . ': Mode cannot be changed on existing streams');
82: }
83:
84: $this->_stream = $streamOrUrl;
85: } else {
86: $this->__wakeup();
87: }
88: }
89:
90: /**
91: * Wakup function - reattaches stream.
92: *
93: * @throws Horde_Log_Exception
94: */
95: public function __wakeup()
96: {
97: if (!($this->_stream = @fopen($this->_streamOrUrl, $this->_mode, false))) {
98: throw new Horde_Log_Exception(__CLASS__ . ': "' . $this->_streamOrUrl . '" cannot be opened with mode "' . $this->_mode . '"');
99: }
100: }
101:
102: /**
103: * Write a message to the log.
104: *
105: * @param array $event Log event.
106: *
107: * @return boolean True.
108: * @throws Horde_Log_Exception
109: */
110: public function write($event)
111: {
112: if (!empty($this->_options['ident'])) {
113: $event['message'] = $this->_options['ident'] . ' ' . $event['message'];
114: }
115: $line = $this->_formatter->format($event);
116:
117: if (!@fwrite($this->_stream, $line)) {
118: throw new Horde_Log_Exception(__CLASS__ . ': Unable to write to stream');
119: }
120:
121: return true;
122: }
123:
124: }
125: