Logging
Overview
A Skinny application provides a log object that writes data to a specific output. The actual writing of data is delegated to a log writer.
How to log data
To log data in a Skinny application, get a reference to the log object:
<?php
$log = $app->log;
The log object provides the following PSR-3 interface
$app->log->debug(mixed $object);
$app->log->info(mixed $object);
$app->log->notice(mixed $object);
$app->log->warning(mixed $object);
$app->log->error(mixed $object);
$app->log->critical(mixed $object);
$app->log->alert(mixed $object);
$app->log->emergency(mixed $object);
Each log object method accepts one argument of mixed type. The argument is usually a string, but the argument can be anything. The log object will pass the argument to its log writer. It is the log writer’s responsibility to write arbitrary input to the appropriate destination.
Activate
The Skinny application’s log object provides the following public methods to enable or disable logging during runtime.
<?php
//Enable logging
$app->log->setEnabled(true);
//Disable logging
$app->log->setEnabled(false);
You may enable or disable the log object during application instantiation like this:
<?php
$app = \Skinny\Skinny::newInstance([
'log.enabled' => true,
]);
If logging is disabled, the log object will ignore all logged messages until it is enabled.
Levels
Heads Up! Use the \Skinny\Log constants when setting the log level instead of using raw integers. The Skinny application’s log object will respect or ignore logged messages based on its log level setting. When you invoke the log objects’s methods, you are inherently assigning a level to the logged message. The available log levels are:
- \Skinny\Log::EMERGENCY
- Level 1
- \Skinny\Log::ALERT
- Level?g 2
- \Skinny\Log::CRITICAL
- Level?g 3
- \Skinny\Log::ERROR
- Level?g 4
- \Skinny\Log::WARN
- Level?g 5
- \Skinny\Log::NOTICE
- Level?g 6
- \Skinny\Log::INFO
- Level?g 7
- \Skinny\Log::DEBUG
- Level 8
Only messages that have a level less than the current log object’s level will be logged. For example, if the log object’s level is \Skinny\Log::WARN (5), the log object will ignore \Skinny\Log::DEBUG and \Skinny\Log::INFO messages but will accept \Skinny\Log::WARN, \Skinny\Log::ERROR, and \Skinny\Log::CRITICAL messages.
How to set the log level
<?php
$app->log->setLevel(\Skinny\Log::WARN);
You can set the log object’s level during application instantiation, too:
<?php
$app = \Skinny\Skinny::newInstance([
'log.level' => \Skinny\Log::WARN
]);
Writers
The Skinny application’s log object has a log writer. The log writer is responsible for sending a logged message to the appropriate output (e.g. STDERR, a log file, a remote web service, Twitter, or a database). Out of the box, the Skinny application’s log object has a log writer of class \Skinny\LogFileWriter; this log writer directs output to the resource handle referenced by the application environment’s skinny.errors key (by default, this is “php://stderr”). You may also define and use a custom log writer.
How to use a custom log writer
A custom log writer must implement the following public interface:
<?php
public function write(mixed $message);
You must tell the Skinny application’s log object to use your custom log writer. You can do so in your application’s settings during instantiation like this:
<?php
$app = \Skinny\Skinny::newInstance([
'log.writer' => new MyLogWriter(),
]);
You may also set a custom log writer with middleware like this:
<?php
class CustomLogWriterMiddleware extends \Skinny\Middleware
{
public function call()
{
//Set the new log writer
$this->app->log->setWriter(new \MyLogWriter());
//Call next middleware
$this->next->call();
}
}
You can set the log writer similarly in an application hook or route callback like this:
<?php
$app->hook('skinny.before', function () use ($app) {
$app->log->setWriter(new \MyLogWriter());
});
If you only need to redirect error output to a different resource handle, use the Skinny application’s default log writer; it writes log messages to a resource handle. All you need to do is set the skinny.errors environment variable to a valid resource handle.