popphp/pop-log

Pop Log Component for Pop PHP Framework

3.0.0 2017-02-22 17:36 UTC

README

Build Status Coverage Status

OVERVIEW

pop-log is a logging component that provides a way of logging events following the standard BSD syslog protocol outlined in RFC-3164. Support is built-in for writing log messages to a file or database table or deploying them via email. The eight available log message severity values are:

  • EMERG (0)
  • ALERT (1)
  • CRIT (2)
  • ERR (3)
  • WARN (4)
  • NOTICE (5)
  • INFO (6)
  • DEBUG (7)

and are available via their respective methods:

  • $log->emergency($message);
  • $log->alert($message);
  • $log->critical($message);
  • $log->error($message);
  • $log->warning($message);
  • $log->notice($message);
  • $log->info($message);
  • $log->debug($message);

pop-log is a component of the Pop PHP Framework.

INSTALL

Install pop-log using Composer.

composer require popphp/pop-log

BASIC USAGE

Using a log file

Setting up and using a log file is pretty simple. Plain text is the default, but there is also support for CSV, TSV and XML formats:

use Pop\Log\Logger;
use Pop\Log\Writer;

$log = new Logger(new Writer\File(__DIR__ . '/logs/app.log'));

$log->info('Just a info message.');
$log->alert('Look Out! Something serious happened!');

Then, your 'app.log' file will contain:

2015-07-11 12:32:32    6    INFO    Just a info message.
2015-07-11 12:32:33    1    ALERT   Look Out! Something serious happened!

Using email

Here's an example using email, which requires you to install popphp/pop-mail:

use Pop\Log\Logger;
use Pop\Log\Writer;
use Pop\Mail;

$mailer = new Mail\Mailer(new Mail\Transport\Sendmail());
$log    = new Logger(new Writer\Mail($mailer, [
    'sysadmin@mydomain.com', 'logs@mydomain.com'
]));

$log->info('Just a info message.');
$log->alert('Look Out! Something serious happened!');

Then the emails listed above will receive a series of emails like this:

Subject: Log Entry: INFO (6)
2015-07-11 12:32:32    6    INFO    Just a info message.

and

Subject: Log Entry: ALERT (1)
2015-07-11 12:32:33    1    ALERT   Look Out! Something serious happened!

Using a database table

Writing a log to a table in a database requires you to install popphp/pop-db:

use Pop\Db\Db;
use Pop\Log\Logger;
use Pop\Log\Writer;

$db  = Db::connent('sqlite', __DIR__ . '/logs/.htapplog.sqlite');
$log = new Logger(new Writer\Db($db, 'system_logs'));

$log->info('Just a info message.');
$log->alert('Look Out! Something serious happened!');

In this case, the logs are written to a database table that has the columns id, timestamp, level, name and message. So, after the example above, your database table would look like this:

Id Timestamp Level Name Message
1 2015-07-11 12:32:32 6 INFO Just a info message.
2 2015-07-11 12:32:33 1 ALERT Look Out! Something serious happened!