1 |
<?php |
2 |
// $Horde: horde/lib/Log/file.php,v 1.4 2000/06/28 21:36:13 jon Exp $ |
3 |
|
4 |
/** |
5 |
* The Log_file class is a concrete implementation of the Log:: |
6 |
* abstract class which writes message to a text file. |
7 |
* |
8 |
* @author Jon Parise <jon@csh.rit.edu> |
9 |
* @version $Revision: 1.1 $ |
10 |
* @since Horde 1.3 |
11 |
*/ |
12 |
class Log_file extends Log { |
13 |
|
14 |
// {{{ properties |
15 |
|
16 |
/** String holding the filename of the logfile. */ |
17 |
var $filename = ''; |
18 |
|
19 |
/** Integer holding the file handle. */ |
20 |
var $fp = ''; |
21 |
|
22 |
// }}} |
23 |
|
24 |
|
25 |
// {{{ constructor |
26 |
/** |
27 |
* Constructs a new logfile object. |
28 |
* |
29 |
* @param $log_name The filename of the logfile. |
30 |
* @param $ident (optional) The identity string. |
31 |
* @param $conf (optional) The configuration array. |
32 |
*/ |
33 |
function Log_file ($log_name, $ident = '', $conf = false) { |
34 |
$this->filename = $log_name; |
35 |
$this->ident = $ident; |
36 |
} |
37 |
// }}} |
38 |
|
39 |
|
40 |
// {{{ open() |
41 |
/** |
42 |
* Opens the logfile for appending, if it has not already been opened. |
43 |
* If the file doesn't already exist, attempt to create it. This is |
44 |
* implicitly called by log(), if necessary. |
45 |
*/ |
46 |
function open () { |
47 |
if (!$this->opened) { |
48 |
$this->fp = fopen($this->filename, 'a'); |
49 |
$this->opened = true; |
50 |
} |
51 |
} |
52 |
// }}} |
53 |
|
54 |
// {{{ close() |
55 |
/** |
56 |
* Closes the logfile, if it is open. |
57 |
*/ |
58 |
function close () { |
59 |
if ($this->opened) { |
60 |
fclose($this->fp); |
61 |
$this->opened = false; |
62 |
} |
63 |
} |
64 |
// }}} |
65 |
|
66 |
// {{{ log() |
67 |
/** |
68 |
* Writes $message to the currently open logfile. Calls open(), if |
69 |
* necessary. Also, passes the message along to any Log_observer |
70 |
* instances that are observing this Log. |
71 |
* |
72 |
* @param $message The textual message to be logged. |
73 |
* @param $priority (optional) The priority of the message. Valid |
74 |
* values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, |
75 |
* LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and |
76 |
* LOG_DEBUG. The default is LOG_INFO. |
77 |
*/ |
78 |
function log ($message, $priority = LOG_INFO) { |
79 |
if (!$this->opened) |
80 |
$this->open(); |
81 |
|
82 |
$this->ident && $ident = $this->ident . " "; |
83 |
|
84 |
$entry = sprintf("%s %s[%s] %s\n", |
85 |
strftime("%Y-%m-%d %H:%M:%S"), |
86 |
$ident, |
87 |
Log::priorityToString($priority), |
88 |
$message |
89 |
); |
90 |
|
91 |
if ($this->fp) |
92 |
fwrite($this->fp, $entry); |
93 |
|
94 |
$this->notifyAll(array('priority' => $priority, 'message' => $message)); |
95 |
} |
96 |
// }}} |
97 |
|
98 |
} |
99 |
|
100 |
?> |