Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More settings/placeholder for DateTimeFileLogger #95

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 70 additions & 9 deletions Log/DateTimeFileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,31 @@ class DateTimeFileWriter
* (string) The log file name format; parsed with `date()`.
*
* extension:
* (string) The file extention to append to the filename`.
* (string) The file extention to append to the filename`.
*
* date_message_format:
* (string) The date format to use in the %date% parameter in message_format; parsed with `date()`.
*
* message_format:
* (string) The log message format; available tokens are...
* %label% Replaced with the log message level (e.g. FATAL, ERROR, WARN).
* %date% Replaced with a ISO8601 date string for current timezone.
* %message% Replaced with the log message, coerced to a string.
* %label% Replaced with the log message level (e.g. FATAL, ERROR, WARN).
* %date% Replaced with a date string for current timezone (default is ISO8601).
* %message% Replaced with the log message, coerced to a string.
* %root_uri% Replaced with the root URI of the request.
* %resurce_uri% Replaced with the resource URI of the request.
* %content_type% Replaced with the content type of the request.
* %media_type% Replaced with the media type of the request.
* %content_charset% Replaced with the content charset of the request.
* %content_length% Replaced with the content length of the request.
* %host% Replaced with the host of the request.
* %host_with_port% Replaced with the host with the port of the request.
* %port% Replaced with the port of the request.
* %scheme% Replaced with the scheme of the request.
* %path% Replaced with the path of the request.
* %url% Replaced with the url of the request.
* %ip_address% Replaced with the ip address of the request.
* %referer% Replaced with the referer of the request.
* %user_agent% Replaced with the user agent of the request.
*
* @param array $settings
* @return void
Expand All @@ -87,6 +105,7 @@ public function __construct($settings = array())
'path' => './logs',
'name_format' => 'Y-m-d',
'extension' => 'log',
'date_message_format' => 'c',
'message_format' => '%label% - %date% - %message%'
), $settings);

Expand All @@ -106,29 +125,71 @@ public function write($object, $level)
//Determine label
$label = 'DEBUG';
switch ($level) {
case \Slim\Log::FATAL:
$label = 'FATAL';
case \Slim\Log::EMERGENCY:
$label = 'EMERGENCY';
break;
case \Slim\Log::ALERT:
$label = 'ALERT';
break;
case \Slim\Log::CRITICAL:
$label = 'CRITICAL';
break;
case \Slim\Log::ERROR:
$label = 'ERROR';
break;
case \Slim\Log::WARN:
$label = 'WARN';
break;
case \Slim\Log::NOTICE:
$label = 'NOTICE';
break;
case \Slim\Log::INFO:
$label = 'INFO';
break;
}

//Get the request
$request = \Slim\Slim::getInstance()->request;

//Get formatted log message
$message = str_replace(array(
'%label%',
'%date%',
'%message%'
'%message%',
'%root_uri%',
'%resurce_uri%',
'%content_type%',
'%media_type%',
'%content_charset%',
'%content_length%',
'%host%',
'%host_with_port%',
'%port%',
'%scheme%',
'%path%',
'%url%',
'%ip_address%',
'%referer%',
'%user_agent%'
), array(
$label,
date('c'),
(string)$object
date($this->settings['date_message_format']),
(string)$object,
$request->getRootUri(),
$request->getResourceUri(),
$request->getContentType(),
$request->getMediaType(),
$request->getContentCharset(),
$request->getContentLength(),
$request->getHost(),
$request->getHostWithPort(),
$request->getPort(),
$request->getScheme(),
$request->getPath(),
$request->getUrl(),
$request->getIp(),
$request->getReferer(),
$request->getUserAgent()
), $this->settings['message_format']);

//Open resource handle to log file
Expand Down
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ demonstrates how to use the custom `DateTimeLogWriter` to write rolling log file
'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
'path' => './logs',
'name_format' => 'Y-m-d',
'message_format' => '%label% - %date% - %message%'
'message_format' => '%label% - %date% - %message%',
'date_message_format' => 'Y-m-d'
))
));

Expand Down