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+
89+ /**
90+ * Write to log
91+ *
92+ * @param mixed $object
93+ * @param int $level
94+ * @return void
95+ */
96+ public function write ( $ object , $ level ) {
97+ //Determine label
98+ $ label = 'DEBUG ' ;
99+ switch ( $ level ) {
100+ case 0 :
101+ $ label = 'FATAL ' ;
102+ break ;
103+ case 1 :
104+ $ label = 'ERROR ' ;
105+ break ;
106+ case 2 :
107+ $ label = 'WARN ' ;
108+ break ;
109+ case 3 :
110+ $ label = 'INFO ' ;
111+ break ;
112+ }
113+
114+ //Get formatted log message
115+ $ message = str_replace (array (
116+ '%label% ' ,
117+ '%date% ' ,
118+ '%message% '
119+ ), array (
120+ $ label ,
121+ date ('c ' ),
122+ (string )$ object
123+ ), $ this ->settings ['message_format ' ]);
124+
125+ //Open resource handle to log file
126+ if (! $ this ->resource ) {
127+ $ this ->resource = fopen ($ this ->settings ['path ' ] . DIRECTORY_SEPARATOR . date ($ this ->settings ['name_format ' ]), 'a ' );
128+ }
129+
130+ //Output to resource
131+ fwrite ($ this ->resource , $ message . PHP_EOL );
132+ }
133+ }
0 commit comments