Skip to content

Commit ad3a2e6

Browse files
committed
Merge branch 'release/0.1.0'
2 parents 5ab48dc + 061a0d9 commit ad3a2e6

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

DateTimeFileWriter.php

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* DateTime 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\Slim(array(
13+
* 'log.writer' => new \Slim\Logger\DateTimeFileWriter()
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+
* @version 0.1.0
25+
*
26+
* MIT LICENSE
27+
*
28+
* Permission is hereby granted, free of charge, to any person obtaining
29+
* a copy of this software and associated documentation files (the
30+
* "Software"), to deal in the Software without restriction, including
31+
* without limitation the rights to use, copy, modify, merge, publish,
32+
* distribute, sublicense, and/or sell copies of the Software, and to
33+
* permit persons to whom the Software is furnished to do so, subject to
34+
* the following conditions:
35+
*
36+
* The above copyright notice and this permission notice shall be
37+
* included in all copies or substantial portions of the Software.
38+
*
39+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
43+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
44+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46+
*/
47+
namespace Slim\Logger;
48+
49+
class DateTimeFileWriter
50+
{
51+
/**
52+
* @const string
53+
*/
54+
const VERSION = '0.1.0';
55+
56+
/**
57+
* @var resource
58+
*/
59+
protected $resource;
60+
61+
/**
62+
* @var array
63+
*/
64+
protected $settings;
65+
66+
/**
67+
* Constructor
68+
*
69+
* Prepare this log writer. Available settings are:
70+
*
71+
* path:
72+
* (string) The relative or absolute filesystem path to a writable directory.
73+
*
74+
* name_format:
75+
* (string) The log file name format; parsed with `date()`.
76+
*
77+
* extension:
78+
* (string) The file extention to append to the filename`.
79+
*
80+
* message_format:
81+
* (string) The log message format; available tokens are...
82+
* %label% Replaced with the log message level (e.g. FATAL, ERROR, WARN).
83+
* %date% Replaced with a ISO8601 date string for current timezone.
84+
* %message% Replaced with the log message, coerced to a string.
85+
*
86+
* @param array $settings
87+
* @return void
88+
*/
89+
public function __construct($settings = array())
90+
{
91+
//Merge user settings
92+
$this->settings = array_merge(array(
93+
'path' => './logs',
94+
'name_format' => 'Y-m-d',
95+
'extension' => 'log',
96+
'message_format' => '%label% - %date% - %message%'
97+
), $settings);
98+
99+
//Remove trailing slash from log path
100+
$this->settings['path'] = rtrim($this->settings['path'], DIRECTORY_SEPARATOR);
101+
}
102+
103+
/**
104+
* Write to log
105+
*
106+
* @param mixed $object
107+
* @param int $level
108+
* @return void
109+
*/
110+
public function write($object, $level)
111+
{
112+
//Determine label
113+
$label = 'DEBUG';
114+
switch ($level) {
115+
case \Slim\Log::FATAL:
116+
$label = 'FATAL';
117+
break;
118+
case \Slim\Log::ERROR:
119+
$label = 'ERROR';
120+
break;
121+
case \Slim\Log::WARN:
122+
$label = 'WARN';
123+
break;
124+
case \Slim\Log::INFO:
125+
$label = 'INFO';
126+
break;
127+
}
128+
129+
//Get formatted log message
130+
$message = str_replace(array(
131+
'%label%',
132+
'%date%',
133+
'%message%'
134+
), array(
135+
$label,
136+
date('c'),
137+
(string)$object
138+
), $this->settings['message_format']);
139+
140+
//Open resource handle to log file
141+
if (!$this->resource) {
142+
$filename = date($this->settings['name_format']);
143+
if (! empty($this->settings['extension'])) {
144+
$filename .= '.' . $this->settings['extension'];
145+
}
146+
147+
$this->resource = fopen($this->settings['path'] . DIRECTORY_SEPARATOR . $filename, 'a');
148+
}
149+
150+
//Output to resource
151+
fwrite($this->resource, $message . PHP_EOL);
152+
}
153+
}

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,46 @@ Slim-Logger
22
===========
33

44
A stand-alone logger class for use with the Slim Framework
5+
6+
## How to Install
7+
8+
#### using [Composer](http://getcomposer.org/)
9+
10+
Create a composer.json file in your project root:
11+
12+
```json
13+
{
14+
"require": {
15+
"slim/logger": "0.1.*"
16+
}
17+
}
18+
```
19+
20+
Then run the following composer command:
21+
22+
```bash
23+
$ php composer.phar install
24+
```
25+
26+
### How to use
27+
28+
```php
29+
<?php
30+
require 'vendor/autoload.php';
31+
32+
$app = new \Slim\Slim(array(
33+
'log.writer' => new \Slim\Logger\DateTimeFileWriter()
34+
));
35+
```
36+
37+
## Author
38+
39+
[Josh Lockhart](https://github.com/codeguy)
40+
41+
### Contributor
42+
43+
[Andrew Smith](https://github.com/silentworks)
44+
45+
## License
46+
47+
MIT Public License

composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "slim/logger",
3+
"description": "Slim Framework logger",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Josh Lockhart",
8+
"email": "[email protected]",
9+
"homepage": "http://www.joshlockhart.com/"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3.0",
14+
"slim/slim": ">=2.4.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Slim\\Logger\\": ""
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)