Skip to content

Commit 52312ce

Browse files
author
Josh Lockhart
committed
Add timestamp log file writer
1 parent e9e0ca1 commit 52312ce

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Timestamp Log File Writer
4+
*
5+
* Use this custom log writer to output log messages
6+
* to a daily, weekly, monthly, or yearly log file. Log
7+
* files will inherently rotate based on the specified
8+
* log file name format and the current time.
9+
*
10+
* USAGE
11+
*
12+
* $app = new Slim(array(
13+
* 'log.writer' => new TimestampLogFileWriter()
14+
* ));
15+
*
16+
* SETTINGS
17+
*
18+
* You may customize this log writer by passing an array of
19+
* settings into the class constructor. Available options
20+
* are shown above the constructor method below.
21+
*
22+
* @author Josh Lockhart <[email protected]>
23+
* @copyright 2012 Josh Lockhart
24+
*
25+
* MIT LICENSE
26+
*
27+
* Permission is hereby granted, free of charge, to any person obtaining
28+
* a copy of this software and associated documentation files (the
29+
* "Software"), to deal in the Software without restriction, including
30+
* without limitation the rights to use, copy, modify, merge, publish,
31+
* distribute, sublicense, and/or sell copies of the Software, and to
32+
* permit persons to whom the Software is furnished to do so, subject to
33+
* the following conditions:
34+
*
35+
* The above copyright notice and this permission notice shall be
36+
* included in all copies or substantial portions of the Software.
37+
*
38+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
42+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
43+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
44+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45+
*/
46+
class TimestampLogFileWriter {
47+
/**
48+
* @var resource
49+
*/
50+
protected $resource;
51+
52+
/**
53+
* @var array
54+
*/
55+
protected $settings;
56+
57+
/**
58+
* Constructor
59+
*
60+
* Prepare this log writer. Available settings are:
61+
*
62+
* path:
63+
* (string) The relative or absolute filesystem path to a writable directory.
64+
*
65+
* name_format:
66+
* (string) The log file name format; parsed with `date()`.
67+
*
68+
* message_format:
69+
* (string) The log message format; available tokens are...
70+
* %label% Replaced with the log message level (e.g. FATAL, ERROR, WARN).
71+
* %date% Replaced with a ISO8601 date string for current timezone.
72+
* %message% Replaced with the log message, coerced to a string.
73+
*
74+
* @param array $settings
75+
* @return void
76+
*/
77+
public function __construct( $settings = array() ) {
78+
//Merge user settings
79+
$this->settings = array_merge(array(
80+
'path' => './logs',
81+
'name_format' => 'Y-m-d',
82+
'message_format' => '%label% - %date% - %message%'
83+
), $settings);
84+
85+
//Remove trailing slash from log path
86+
$this->settings['path'] = rtrim($this->settings['path'], DIRECTORY_SEPARATOR);
87+
88+
//Open resource handle to log file
89+
$this->resource = fopen($this->settings['path'] . DIRECTORY_SEPARATOR . date($this->settings['name_format']), 'a');
90+
}
91+
92+
/**
93+
* Write to log
94+
*
95+
* @param mixed $object
96+
* @param int $level
97+
* @return void
98+
*/
99+
public function write( $object, $level ) {
100+
//Determine label
101+
$label = 'DEBUG';
102+
switch ( $level ) {
103+
case Slim_Log::FATAL:
104+
$label = 'FATAL';
105+
break;
106+
case Slim_Log::ERROR:
107+
$label = 'ERROR';
108+
break;
109+
case Slim_Log::WARN:
110+
$label = 'WARN';
111+
break;
112+
case Slim_Log::INFO:
113+
$label = 'INFO';
114+
break;
115+
}
116+
117+
//Get formatted log message
118+
$message = str_replace(array(
119+
'%label%',
120+
'%date%',
121+
'%message%'
122+
), array(
123+
$label,
124+
date('c'),
125+
(string)$object
126+
), $this->settings['message_format']);
127+
128+
//Output to resource
129+
fwrite($this->resource, $message . PHP_EOL);
130+
}
131+
}

0 commit comments

Comments
 (0)