root/trunk/src/main/php/net/stubbles/util/log/stubFileLogAppender.php

Revision 1281, 3.5 kB (checked in by mikey, 6 months ago)

code nazi :)

Line 
1 <?php
2 /**
3  * A log appenders that writes log data to files.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  util_log
8  */
9 stubClassLoader::load('net::stubbles::util::log::stubLogAppender');
10 /**
11  * A log appenders that writes log data to files.
12  *
13  * This log appender writes the log data into a logfile using the error_log()
14  * function of PHP. The logfile name will be [target]-[Y-m-d].log where target
15  * is the return value of stubLogData::getTarget().
16  *
17  * @package     stubbles
18  * @subpackage  util_log
19  * @uses        http://php.net/error_log
20  */
21 class stubFileLogAppender extends stubBaseObject implements stubLogAppender
22 {
23     /**
24      * the directory to write the logfiles into
25      *
26      * @var  string
27      */
28     protected $logDir = '';
29     /**
30      * mode for new directories
31      *
32      * @var  int
33      */
34     protected $mode   = 0700;
35
36     /**
37      * constructor
38      *
39      * @param  string  $logDir  optional  the directory to write the logfiles into
40      */
41     public function __construct($logDir = null)
42     {
43         if (null == $logDir) {
44             $logDir = stubConfig::getLogPath();
45         }
46         
47         $this->setLogDir($logDir);
48     }
49
50     /**
51      * sets the configuration data
52      *
53      * @param  array  $config
54      */
55     public function setConfig(array $config)
56     {
57         if (isset($config['logDir']) == true) {
58             $this->logDir = $config['logDir'];
59         }
60         
61         if (isset($config['mode']) === true) {
62             $this->mode = $config['mode'];
63         }
64     }
65
66     /**
67      * returns the configuration
68      *
69      * @return  array
70      */
71     public function getConfig()
72     {
73         return array('logDir' => $this->logDir,
74                      'mode'   => $this->mode
75                );
76     }
77
78     /**
79      * set the logpath
80      *
81      * The log director may have placeholders: {Y} will be replaced with the
82      * current year, {M} will be replaced with the current month, e.g.
83      * /path/to/logs/{Y}/{M} would become /path/to/logs/2007/01.
84      *
85      * @param  string  $logDir
86      */
87     public function setLogDir($logDir)
88     {
89         $this->logDir = $logDir;
90     }
91
92     /**
93      * returns the logpath
94      *
95      * @return  string
96      */
97     public function getLogDir()
98     {
99         return $this->logDir;
100     }
101
102     /**
103      * sets the mode for new log directories
104      *
105      * @param  int  $mode
106      */
107     public function setMode($mode)
108     {
109         $this->mode = $mode;
110     }
111
112     /**
113      * returns the mode for new log directories
114      *
115      * @return  int
116      */
117     public function getMode()
118     {
119         return $this->mode;
120     }
121
122     /**
123      * builds the log directory
124      *
125      * @return  string
126      */
127     protected function buildLogDir()
128     {
129         return str_replace('{Y}', date('Y'), str_replace('{M}', date('m'), $this->logDir));
130     }
131
132     /**
133      * append the log data to the log target
134      *
135      * The basename of the logfile will be [target]-[Y-m-d].log where target
136      * is the return value of stubLogData::getTarget().
137      *
138      * @param  stubLogData  $logData
139      */
140     public function append(stubLogData $logData)
141     {
142         $logDir  = $this->buildLogDir();
143         if (file_exists($logDir) == false) {
144             mkdir($logDir, $this->mode, true);
145         }
146         
147         error_log($logData->get() . "\n", 3, $logDir . '/' . $logData->getTarget() . '-' . date('Y-m-d') . '.log');
148     }
149
150     /**
151      * finalize the log target
152      */
153     public function finalize()
154     {
155         // nothing to do, therefore intentionelly left blank
156     }
157 }
158 ?>
Note: See TracBrowser for help on using the browser.