|
| 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 | +} |
0 commit comments