From ec09c8b357be5614984a43a7fcffafceffccf2de Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Thu, 6 Mar 2014 14:32:20 +0100 Subject: [PATCH 1/4] DateTimeFileWriter.php: Add a parameter DateTimeFileWriter.php: Add the date_message_format parameter. With this parameter you can use a custom date format in the logger. The default ISO8601 is a little narrow. --- Log/DateTimeFileWriter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Log/DateTimeFileWriter.php b/Log/DateTimeFileWriter.php index 9f5e587..fc42518 100755 --- a/Log/DateTimeFileWriter.php +++ b/Log/DateTimeFileWriter.php @@ -69,12 +69,15 @@ 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. + * %date% Replaced with a date string for current timezone (default is ISO8601). * %message% Replaced with the log message, coerced to a string. * * @param array $settings @@ -87,6 +90,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); @@ -127,7 +131,7 @@ public function write($object, $level) '%message%' ), array( $label, - date('c'), + date($this->settings['date_message_format']), (string)$object ), $this->settings['message_format']); From 0efccfbe02df820432214e7a74247e5c000b4460 Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Thu, 6 Mar 2014 14:43:43 +0100 Subject: [PATCH 2/4] Add date_message_format to the example --- README.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index db82a12..f3ce791 100644 --- a/README.markdown +++ b/README.markdown @@ -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' )) )); From 3d54e7e278fcb02bb2efc3d762f011b7f0d2aa4e Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Thu, 6 Mar 2014 14:58:11 +0100 Subject: [PATCH 3/4] DateTimeFileWriter: add all the Log Levels Now it supports all the log levels. --- Log/DateTimeFileWriter.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Log/DateTimeFileWriter.php b/Log/DateTimeFileWriter.php index fc42518..5981946 100755 --- a/Log/DateTimeFileWriter.php +++ b/Log/DateTimeFileWriter.php @@ -110,8 +110,14 @@ 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'; @@ -119,6 +125,9 @@ public function write($object, $level) case \Slim\Log::WARN: $label = 'WARN'; break; + case \Slim\Log::NOTICE: + $label = 'NOTICE'; + break; case \Slim\Log::INFO: $label = 'INFO'; break; From 9a0705e43dc9ecb27e3b829b0a5b01f2574d06f1 Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Thu, 6 Mar 2014 16:25:38 +0100 Subject: [PATCH 4/4] Adding all the parameters of the request Now it's available: * %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. --- Log/DateTimeFileWriter.php | 58 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/Log/DateTimeFileWriter.php b/Log/DateTimeFileWriter.php index 5981946..204ee1d 100755 --- a/Log/DateTimeFileWriter.php +++ b/Log/DateTimeFileWriter.php @@ -76,9 +76,24 @@ class DateTimeFileWriter * * 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 date string for current timezone (default is ISO8601). - * %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 @@ -132,16 +147,49 @@ public function write($object, $level) $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($this->settings['date_message_format']), - (string)$object + (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