Continuing this exploration of logging solutions used in various projects, let's look at logging in Kohana 3.
While Monolog and log4php share a mostly common logging model of a frontal Logger object instantiated as many times as needed to supply different logging channels, in which log events are Processed/Filtered then written out by Handlers/Writers, Kohana builds upon a simpler model, which can be summarized by three patterns:
- Singleton: there is only one instance of the Kohana
Log
- Observer:
Log_Writer
instances are attached (and detached) to(/from) the logger instance and handle events they are interested in based on their own configuration. Much like a Drupal hook, all writer instances receive eachLog
event - Delegation: the
Log
exposes awrite()
to trigger the buffered writing, but does not implement it itself, but delegates to theLog_Writer
objects to perform it. Buffered logging control is aLog
property, not aLog_Writer
property.
The logging API proper is a two-step affair:
add()
events as needed. No per-level helpers as in the ZF2 or Symfony logging interfaces- at the end of the page cycle, have shutdown invoke
Log::instance()->write()
to flush the data out if buffered logging has not been deactivated
An interesting bit is that unlike Symfony's LoggerInterface
, the main entry point for logging is much like Drupal's watchdog()
in that it uses structured message events separating the message template (watchdog parameter 2) from the message arguments (watchdog parameter 3), instead of having to stuff the arguments in the $context
object: it's Log::add(<level>, <message template>, <message arguments>, <extra>)
. However, by using the Singleton pattern to ensure a single Log
object, Kohana has no native support for multiple logging channels, which would not be a good fit for Drupal needs.
Like most logging solutions, Kohana expects event level to be one of the 8 RFC5424 levels.
Unlike Drupal, Kohana chose not to bundle any DB Writer, keeping to Unix standard streams and Syslog. Additional Writers can be found in the contrib space, notably on Github:
I've tried searching all over
I've tried searching all over but can't seem to get just a simple straight forward answer.
I want to write log messages (INFO, ERROR, etc.) to the Kohana log file /application/logs/YYYY/MM/DD.php.
How do I do it? Thanks.
Logging with Kohana 3.3.
Logging is a two-step process (three with the boot in which you set up the log destination):
$log = Log::instance();
, and keep it at handIf your question was actually about choosing the name of the logging file, this is in
kohana/bootstrap.php
, where you configure the environment. The default code is like this:And this gives you, as per
Log_File::__construct()
log files named with just the day number, in the directory named YYYY/DD as you described. You can overwrite this by creating a newLog_Writer
class, probably inheriting fromLog_File
, and defining a new name format.