germania-kg/logger

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

4.1.0 2020-09-17 14:57 UTC

README

68747470733a2f2f7374617469632e6765726d616e69612d6b672e636f6d2f6c6f676f732f67612d6c6f676f2d323031362d7765622e7376677a

Germania KG · Logger

Default logging solution for our websites: Pimple Service Provider for Logging with Monolog 1 and 2.

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

Installation with Composer

The major release 4 requires PHP 7.2. The older release 3 supports Monolog 2.

$ 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:

$logger = $dic['Logger'];

This Monolog handlers array is empty per default; it will be filled by one or more of the specialised Service Providers below.

$handlers = $dic['Monolog.Handlers'];

This Monolog Processors array contains per default just Monolog's WebProcessor with ip, method and url extra context variables.

$processors = $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.

$ composer require germania-kg/mailer
<?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

$ composer require league/climate
<?php
use Germania\Logger\ClimateLoggerServiceProvider;
use Monolog\Logger;

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

Log to Microsoft Teams

This requires CMDISP's monolog-microsoft-teams package, available via Composer: cmdisp/monolog-microsoft-teams.

composer require cmdisp/monolog-microsoft-teams "^1.1"
<?php
use Germania\Logger\TeamsLoggerServiceProvider;
use Monolog\Logger;

$incoming_webhook_url="https://outlook.office.com/webhook/many-many-letters";

$dic->register( new TeamsLoggerServiceProvider(
  $incoming_webhook_url,
  Logger::NOTICE
));

Deprecated: HtmlFormattedTeamsLogHandler

The Germania\Logger\HtmlFormattedTeamsLogHandler was an extension of the CMDISP\MonologMicrosoftTeams\TeamsLogHandler class and provided better log message formatting. As of the v1.1 release of CMDISP's monolog-microsoft-teams package, this extension is not needed any longer and will be removed.

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
));

Usage 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 and Unit tests

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

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