Overview
This is a simple, flexible PSR-3 logging implementation for PHP.
Source is at https://bitbucket.org/jelofson/vespula.log
Table of Contents
Installation
Install via composer.
composer require vespula/log
Basic Usage
All you need to get logging working is a Log object and an Adapter. There are currently 7 adapters:
File, Email, Sql, ErrorLog, Chrome, PHPDebugBar, and None. The ErrorLog adapter uses PHP’s error_log()
function to log messages with varying targets. See below.
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\File as FileAdapter;
use Psr\Log\LogLevel;
$destination = '/tmp/mylogfile.txt'; // must be writable by web server
$adapter = new FileAdapter($destination);
// First parameter is a string key to identify the adapter.
$log = new Log('file', $adapter);
// Now log something. PSR-3 has 8 log levels (alert, critical, debug, emergency,
// error, warning, info, notice)
// Use the log level directly...
$log->info('This is my message');
// Or, use the catch-all `log()` method...
$log->log(LogLevel::INFO, 'My info message');
// Alternatively, you can pass a context array to the log methods.
// The context data will replace same-named placeholders.
$context = [
'username'=>'juser'
];
// The placeholder must use the braces {} and the placeholder must match the key
// in the array.
$log->info('User {username} logged in', $context);
Note
You can pass non-scalar values to the context array. The logger will try to use print_r() to display the value. Arrays work, but JSON probably works better? It’s up to you.
Formatting Options
The formatting options are limited, but you can adjust the timestamp format and the overall message format. The default format is as follows: (a tab separates each element).
[{timestamp}]\t[{level}]\t{message}
Which would look something like this:
[2015-11-23T11:37:01-07:00] [INFO] User logged in: someuserid
You can adjust the output format by calling the setMessageFormat()
on the adapter.
<?php
// Use braces to wrap the placeholders
$format = '{level}|{timestamp}|{message}';
$adapter->setMessageFormat($format);
The timestamp is formatted using PHP’s DateTime::format()
method. The default format is ‘c’.
Eg. DateTime::format('c')
. You can change the timestamp format by calling the setDateFormat()
method on the adapter.
Please see the DateTime::format() documentation. The DateTime object has many constants available for formatting.
<?php
$adapter->setDateFormat('Y-m-d H:i:s');
// using a DateTime constant
$adapter->setDateFormat(\DateTime::W3C);
Getting the Adapters
You can get all the adapters, or a single adapter using the getAdapters()
and getAdapter()
methods.
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\File as FileAdapter;
use Vespula\Log\Adapter\Chrome as ChromeAdapter;
$destination = '/tmp/mylogfile.txt'; // must be writable by web server
$adapter = new FileAdapter($destination);
$log = new Log('file', $adapter);
// get array of adapters
$adapters = $log->getAdapters();
$chrome_adapter = new ChromeAdapter();
$log->addAdapter('chrome', $chrome_adapter);
// get single adapter by key
$chrome_adapter = $log->getAdapter('chrome');
// get the first adapter without key
$file_adapter = $log->getAdapter(); // The file adapter was added first in the constructor