germania-kg/logger

Our Company's default logging solution: Pimple Service Provider for Logging with Monolog

3.1.2 2019-02-14 15:19 UTC

README

Default logging solution for Germania KG's websites: Pimple Service Provider for Logging with Monolog.

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/logger

Setup

<?php
use Germania\Logger\LoggerServiceProvider;

// Have your Pimple or Slim3 Container at hand
$dic = new \Pimple\Container;
$dic->register( new LoggerServiceProvider( "My App" );
               
// Alternatively, pass custom server data environment,
// disable IP address anonymization               
$dic->register( new LoggerServiceProvider(
  "My App",
  $_SERVER,
  false
));

Services provided

// This Monolog Logger instance is your PSR-3 Logger
$dic['Logger']
  
// Monolog handlers array; 
// Empty per default; will be filled by one or more 
// of the specialised Service Providers below.
$dic['Monolog.Handlers']
  
// Monolog Processors array;
// Default: just Monolog's "WebProcessor" with "ip", "method" and "url"
$dic['Monolog.Processors']
 

Specialised Service Providers

Log to logfile

<?php
use Germania\Logger\FileLoggerServiceProvider;
use Monolog\Logger;
  
$max_files_count = 30;
$dic->register( new FileLoggerServiceProvider(
  "var/log/app.log",
  $max_files_count,
  Logger::DEBUG
));

Log to StdErr (stream)

<?php
use Germania\Logger\StreamLoggerServiceProvider;
use Monolog\Logger;

$dic->register( new StreamLoggerServiceProvider );

// Custom values, here the defaults:
$dic->register( new StreamLoggerServiceProvider("php://stderr", Logger::WARNING) );

Log using SwiftMailer

This service requires service definitions for SwiftMailer and SwiftMailer.HtmlMessage. Germania KG's germania-kg/mailer will provide those.

<?php
use Germania\Logger\SwiftMailerLoggerServiceProvider;
use Monolog\Logger;

$dic->register( 
  new SwiftMailerLoggerServiceProvider( Logger::WARNING ) 
);

Log using CLImate Logger

This requires CLImate, available with Composer: league/climate

<?php
use Germania\Logger\ClimateLoggerServiceProvider;
use Monolog\Logger;

$dic->register( 
  new ClimateLoggerServiceProvider( Logger::INFO ) 
);

Log to Slack channel

For more information, see these links:

https://blog.tschelhas.de/symfony/slack-als-logger-fuer-symfony-nutzen/

https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SlackHandler.php

<?php
use Germania\Logger\SlackLoggerServiceProvider;
use Monolog\Logger;

$dic->register( new SlackLoggerServiceProvider(
  $slack_token,
  $slack_channel,
  $slack_username,
  Logger::CRITICAL
));

Example

<?php
use Germania\Logger\LoggerServiceProvider;
use Germania\Logger\FileLoggerServiceProvider;
use Monolog\Logger;

// 
// 1. Basic setup
//
$log_name     = "My App";
$anonymize_ip = true;
$server_data  = $_SERVER;

$dic->register( new LoggerServiceProvider(
  $log_name,
  $server_data,
  $anonymize_ip
));


//
// 2. The 'Logger' service won't do anything right here.
// Adding a specialized Service Provider is needed:
//
$max_files_count = 30;
$dic->register( new FileLoggerServiceProvider(
  "var/log/app.log",
  $max_files_count,
  Logger::DEBUG
));


// 
// 3. Now you can grab your PSR-3 Logger:
//
$logger = $dic['Logger'];
$logger->info("Hooray!");

Development

$ git clone https://github.com/GermaniaKG/Logger.git
$ cd Logger
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit