anewt-developers team mailing list archive
-
anewt-developers team
-
Mailing list archive
-
Message #00244
[Branch ~uws/anewt/anewt.uws] Rev 1771: [logging] Display colors in log output on stderr
------------------------------------------------------------
revno: 1771
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Sat 2010-03-20 23:02:02 +0100
message:
[logging] Display colors in log output on stderr
The default log handler now shows colorized log messages
when running in an interactive terminal session.
Fixes bug lp:537700.
modified:
logging/logging.test.php
logging/loghandlers.lib.php
--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws
Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'logging/logging.test.php'
--- logging/logging.test.php 2008-10-24 21:21:20 +0000
+++ logging/logging.test.php 2010-03-20 22:02:02 +0000
@@ -5,7 +5,7 @@
anewt_include('logging');
AnewtLog::init(false);
-AnewtLog::add_handler(new AnewtLogHandlerDefault());
+AnewtLog::add_handler(new AnewtLogHandlerDefault(), ANEWT_LOG_LEVEL_DEBUG);
AnewtLog::add_handler(new AnewtLogHandlerFile('test.log'));
AnewtLog::add_handler(new AnewtLogHandlerFile('test-debug.log'), ANEWT_LOG_LEVEL_DEBUG);
@@ -22,5 +22,8 @@
AnewtLog::reset_domain();
AnewtLog::warning('This is a warning message: %d: %s', array(2, 'test2'));
AnewtLog::warning('This is warning with format characters but no values, %s %s %s');
+AnewtLog::message('This is a message');
+AnewtLog::info('This is an info message');
+AnewtLog::debug('This is a debug message');
?>
=== modified file 'logging/loghandlers.lib.php'
--- logging/loghandlers.lib.php 2009-07-20 20:39:48 +0000
+++ logging/loghandlers.lib.php 2010-03-20 22:02:02 +0000
@@ -46,7 +46,7 @@
/**
* \public
*
- * Format a log message. This method should be used in AnewtLogHandlerBase
+ * Format a log message. This method can be used in AnewtLogHandlerBase
* subclasses to format the log messages.
*
* \param $domain
@@ -61,7 +61,7 @@
* \return
* Formatted log string.
*/
- protected function format_log_message($domain=null, $level, $message)
+ protected function format_log_message($domain=null, $level, $message, $ansi_color_escapes=false)
{
assert('is_int($level)');
assert('is_string($message)');
@@ -69,14 +69,17 @@
$name = AnewtLog::loglevel_to_string($level);
/* Without logging domain */
- if (is_null($domain)) {
+ if (is_null($domain))
+ {
$output = sprintf(
- '%s: %s',
+ $ansi_color_escapes ? "\x1b[30;43m\x1b[2K%s: %s" : '%s: %s',
$name,
$message);
+ }
/* With logging domain */
- } else {
+ else
+ {
assert('is_string($domain)');
$output = sprintf(
'(%s) %s: %s',
@@ -99,11 +102,50 @@
* Log handler that uses the default error output mechanism.
*
* For Apache web servers the message will be written to the ErrorLog file (eg.
- * error.log or error_log). For command line applications the message will be
- * written to stderr.
+ * error.log or error_log).
+ *
+ * For command line applications the message will be written to the standard
+ * error output (stderr). In interactive terminals, the output will be colored
+ * using ANSI escape codes.
*/
final class AnewtLogHandlerDefault extends AnewtLogHandlerBase
{
+ private $stderr_is_tty;
+ private $color_escapes;
+ private $color_reset_escape = '';
+
+ function __construct()
+ {
+ /* Prepare color code escapes if on an interactive tty */
+
+ $this->stderr_is_tty = function_exists('posix_isatty') && posix_isatty(STDERR);
+
+ if ($this->stderr_is_tty)
+ {
+ $this->color_reset_escape = "\x1b[0m";
+
+ $this->color_escapes = array(
+ ANEWT_LOG_LEVEL_ERROR => "\x1b[0;31m", /* Red */
+ ANEWT_LOG_LEVEL_CRITICAL => "\x1b[0;31m", /* Red */
+ ANEWT_LOG_LEVEL_WARNING => "\x1b[0;31m", /* Red */
+ ANEWT_LOG_LEVEL_MESSAGE => "\x1b[0;32m", /* Green */
+ ANEWT_LOG_LEVEL_INFO => "\x1b[0;33m", /* Yellow */
+ ANEWT_LOG_LEVEL_DEBUG => "\x1b[0;33m", /* Yellow */
+ );
+ }
+ else
+ {
+ $this->color_escapes = array(
+ ANEWT_LOG_LEVEL_ERROR => '',
+ ANEWT_LOG_LEVEL_CRITICAL => '',
+ ANEWT_LOG_LEVEL_WARNING => '',
+ ANEWT_LOG_LEVEL_MESSAGE => '',
+ ANEWT_LOG_LEVEL_INFO => '',
+ ANEWT_LOG_LEVEL_DEBUG => '',
+ );
+ }
+ }
+
/**
* Log a message.
*
@@ -115,7 +157,20 @@
*/
function log($domain, $level, $message)
{
- $output = $this->format_log_message($domain, $level, $message);
+ $output = array();
+
+ $output[] = $this->color_escapes[$level];
+
+ if ($domain)
+ $output[] = sprintf('(%s) ', $domain);
+
+ $output[] = '';
+ $output[] = AnewtLog::loglevel_to_string($level);
+ $output[] = ': ';
+ $output[] = $this->color_reset_escape;
+ $output[] = $message;
+
+ $output = join('', $output);
error_log($output);
}
}