Skip to content

Instantly share code, notes, and snippets.

@kiall
Created January 24, 2012 16:39
Show Gist options
  • Save kiall/1671022 to your computer and use it in GitHub Desktop.
Save kiall/1671022 to your computer and use it in GitHub Desktop.
diff --git a/classes/kohana/kohana/exception.php b/classes/kohana/kohana/exception.php
index 947d932..d66aeb6 100644
--- a/classes/kohana/kohana/exception.php
+++ b/classes/kohana/kohana/exception.php
@@ -144,8 +144,7 @@ class Kohana_Kohana_Exception extends Exception {
$error = Kohana_Exception::text($e);
// Add this exception to the log
- Kohana::$log->add(Log::ERROR, $error);
- Kohana::$log->add(Log::STRACE, $error."\n--\n" . $e->getTraceAsString());
+ Kohana::$log->add(Log::ERROR, $error, NULL, array('exception' => $e));
// Make sure the logs are written
Kohana::$log->write();
diff --git a/classes/kohana/log.php b/classes/kohana/log.php
index 6c20df0..5f04eae 100644
--- a/classes/kohana/log.php
+++ b/classes/kohana/log.php
@@ -21,17 +21,6 @@ class Kohana_Log {
const NOTICE = LOG_NOTICE; // 5
const INFO = LOG_INFO; // 6
const DEBUG = LOG_DEBUG; // 7
- const STRACE = 8;
-
- /**
- * @var string timestamp format for log entries
- */
- public static $timestamp = 'Y-m-d H:i:s';
-
- /**
- * @var string timezone for log entries
- */
- public static $timezone;
/**
* @var boolean immediately write when logs are added
@@ -130,7 +119,7 @@ class Kohana_Log {
* @param array $values values to replace in the message
* @return Log
*/
- public function add($level, $message, array $values = NULL)
+ public function add($level, $message, array $values = NULL, array $additional = NULL)
{
if ($values)
{
@@ -138,12 +127,28 @@ class Kohana_Log {
$message = strtr($message, $values);
}
- // Create a new message and timestamp it
+ // Grab a copy of the trace
+ if (isset($additional['exception']))
+ {
+ $trace = $additional['exception']->getTrace();
+ }
+ else
+ {
+ $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+ }
+
+ // Create a new message
$this->_messages[] = array
(
- 'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
- 'level' => $level,
- 'body' => $message,
+ 'time' => time(),
+ 'level' => $level,
+ 'body' => $message,
+ 'trace' => $trace,
+ 'file' => $trace[0]['file'],
+ 'line' => $trace[0]['line'],
+ 'class' => $trace[0]['class'],
+ 'function' => $trace[0]['function'],
+ 'additional' => $additional,
);
if (Log::$write_on_add)
diff --git a/classes/kohana/log/file.php b/classes/kohana/log/file.php
index d54bcd2..be4fcc6 100644
--- a/classes/kohana/log/file.php
+++ b/classes/kohana/log/file.php
@@ -16,6 +16,16 @@ class Kohana_Log_File extends Log_Writer {
protected $_directory;
/**
+ * @var string timestamp format for log entries
+ */
+ public static $timestamp = 'Y-m-d H:i:s';
+
+ /**
+ * @var string timezone for log entries
+ */
+ public static $timezone;
+
+ /**
* Creates a new file logger. Checks that the directory exists and
* is writable.
*
@@ -88,8 +98,26 @@ class Kohana_Log_File extends Log_Writer {
{
// Write each message into the log file
// Format: time --- level: body
- file_put_contents($filename, PHP_EOL.$message['time'].' --- '.$this->_log_levels[$message['level']].': '.$message['body'], FILE_APPEND);
+ file_put_contents($filename, PHP_EOL.Log_File::format_entry($message), FILE_APPEND);
+ }
+ }
+
+ /**
+ * Formats a log entry.
+ *
+ * @param array $message
+ * @return string
+ */
+ public function format_entry(array $message)
+ {
+ $string = Date::formatted_time($message['time'], Log_File::$timestamp, Log_File::$timezone).' --- '.$this->_log_levels[$message['level']].': '.$message['body'];
+
+ if (isset($message['additional']['exception']))
+ {
+ $string .= PHP_EOL.Date::formatted_time($message['time'], Log_File::$timestamp, Log_File::$timezone).' --- STRACE: '.$message['additional']['exception']->getTraceAsString();
}
+
+ return $string;
}
} // End Kohana_Log_File
\ No newline at end of file
diff --git a/classes/kohana/log/syslog.php b/classes/kohana/log/syslog.php
index 6ec0217..81da585 100644
--- a/classes/kohana/log/syslog.php
+++ b/classes/kohana/log/syslog.php
@@ -16,14 +16,9 @@ class Kohana_Log_Syslog extends Log_Writer {
protected $_ident;
/**
- * @var array log levels
+ * @var int Level to use for stack traces
*/
- protected $_syslog_levels = array('ERROR' => LOG_ERR,
- 'CRITICAL' => LOG_CRIT,
- 'STRACE' => LOG_ALERT,
- 'ALERT' => LOG_WARNING,
- 'INFO' => LOG_INFO,
- 'DEBUG' => LOG_DEBUG);
+ public static $strace_level = LOG_ALERT;
/**
* Creates a new syslog logger.
@@ -53,6 +48,12 @@ class Kohana_Log_Syslog extends Log_Writer {
foreach ($messages as $message)
{
syslog($message['level'], $message['body']);
+
+ if (isset($message['additional']['exception']))
+ {
+ syslog(Log_Syslog::$strace_level, $message['additional']['exception']->getTraceAsString());
+ }
+
}
}
diff --git a/classes/kohana/log/writer.php b/classes/kohana/log/writer.php
index fdb01f0..d4df920 100644
--- a/classes/kohana/log/writer.php
+++ b/classes/kohana/log/writer.php
@@ -23,7 +23,6 @@ abstract class Kohana_Log_Writer {
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG',
- 8 => 'STRACE',
);
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment