This package is abandoned and no longer maintained. No replacement package was suggested.

PHP Logger, PSR-3, what else ?

3.0.0 2017-09-19 11:48 UTC

README

Loggr is PSR-3 Loger implementation.

  • It's just a loggr, so I have nothing more, funny or not, to say.
  • The cake is a lie.

Features

  • Any number of agents can log at once.
  • Different agents can be configured to log between min and max levels
  • Messages are automaticly formated with the context
  • File Agent : Write logs into files. Can be configured to auto-rotate files, logs different levels in differents files
  • MySQL Agent : Log into a mysql db. Configurable to map to any existing table. Require a PDO connection object
  • HTML Agent : Just like the File, but add some HTML formating to review logs in a browser
  • Console Agent : For command line logs, with colors !
  • Nil Agent : Do nothing. It's usefull if you need to pass a PSR-3 log object to a library but don't actually want to log anything

Requirements

  • PHP 7.0+ (Should work with PHP 5.4+, but not used/tested anymore)
  • A wrtiteable directory for the log files, if you log into files.
  • PDO if you use MySQL

Install

Composer

{
    "minimum-stability": "dev",
    "require": {
        "alex-robert/loggr": "~3"
    }
}

Manually

Download sources and unzip in your project directory (but who does that, really ?!)

Quick Manual

Use the Loggr class

$Log = new \Loggr\Loggr();

//Add some log Agents
$Log->add_agent(new \Loggr\Agent\File());
$Log->debug('This a debug message', ['Some', 'Context']);

Static logging

//Add some log Agents
\Loggr\Log::add_agent(new \Loggr\Agent\File());
\Loggr\Log::add_agent(new \Loggr\Agent\MySQL());

//This will be logged to a File and in Mysql
\Loggr\Log::alert('Things are going verry badly here');
//yes, it's important to give specific context to your logs, like "very" and "badly" here ;p

File Formatter exemple

$Fl = new \Loggr\Agent\File();
$Fl->set_path('/tmp/logs/');
$Fl->get_formatter()
        ->set_format(\Loggr\Level::DEBUG, "{level} ::: {time} - {message} {context} \n")
        ->set_format('default', "{level} :: {time} - {message} \n")
        ->set_time_format('Y-m-d h:i:s')
        ->set_microtime(true)
        ->set_context_format('json');

$Fl->info('Hello {whom}', ['whom'=>'World']);

MySQL Loggr exemple

$Ml = new \Loggr\Agent\MySQL();
$Ml->set_connection(new PDO('mysql:host=localhost;dbname=test', 'logger'));
$Ml->set_table_name('logs');

//Bind context and basics ('message', 'level', 'context', 'time') keys to DB columns
$Ml->bind_column_names([
    'time'    => 'created_at',
    'level'   => 'lvl',
    'foo'     => 'bar'
]);

//Will bind "description" key (in the context) to description column
$Ml->bind_column('description');

$Ml->debug('Test', ['description' => 'This is a test.', 'bar' => 'Just inside the context', 'foo' => 'Goes in bar column']);

Log levels

$Fl = new \Loggr\Agent\File();
$Fl->set_min_level(\Loggr\Level::INFO);

//Logged
$Fl->info('Hello world');
$Fl->alert('I have a very important message for you, world');

//Not logged
$Fl->debug('Not logged, level INFO > DEBUG');

Channel Logging

A channel is a stack of log agents. It implements the LoggerInterface, and you can add multiple agents to it, so you can log on multiple support at once with a channel. It is usefull to separate log purposes/environements across a project, like having a channel named api to track api calls, another for internals_errors or even a dev channel, with it's specific log levels. Channels works with static logging or with the Loggr class.

Channel are by default explicit : it means, when you use a Loggr class, you have to specify in which channel to log to. If no channel is specified, logs will be passed to any agents added with the add_agent method. Log levels can be defined channel-wise and agent-wise. The agent levels are the last applied.

Licence

MIT