Overview

Source Code Quality

This is a simple, flexible PSR-3 logging implementation for PHP.

Installation

Install via composer.

composer require vespula/log

Tip

New in version 1.0. You used to have to use the ErrorLog adapter to send log data to email or a file. Now, they have their own adapters. They still use PHP's error_log() function, but it's more explicit now.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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);

$log = new Log($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);

Notes

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:

1
[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.

1
2
3
4
<?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.

1
2
3
4
5
<?php
$adapter->setDateFormat('Y-m-d H:i:s');

// using a DateTime constant
$adapter->setDateFormat(\DateTime::W3C);

Getting the Adapters (>=1.1.0)

You can get all the adapters, or a single adapter using the getAdapters() and getAdapter() methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?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($adapter);

// get array of adapters
$adapters = $log->getAdapters();

$chrome_adapter = new ChromeAdapter();

$log->addAdapter($chrome_adapter);

// get single adapter by array key
$chrome_adapter = $log->getAdapter(1); // added seconds so key is 1