PSR-3 & Monolog

Chau Pham

Logging is one of the most ubiquitous tasks encountered in PHP. We use logs to track error messages, record important events, and debug problems with our code. In any PHP project, the code is likely to be full of calls to a logging library which handles these actions for us.

PSR-3

PSR-3 is a proposed standard describing a common logging interface for PHP frameworks.

Logging with PSR-3 to Improve Reusability

The logger interface defined by PSR-3 allows us to write reusable code that isn’t dependent on any particular logging implementation.

PSR-3

The LoggerInterface exposes 8 methods to write logs

  • Emergency – the system is unusable
  • Alert – immediate action is required
  • Critical – critical conditions
  • Error – errors that do not require immediate attention but should be monitored
  • Warning – unusual or undesirable occurrences that are not errors
  • Notice – normal but significant events
  • Info – interesting events
  • Debug – detailed information for debugging purposes

PSR-3

Helper classes and interfaces

  • Psr\Log\AbstractLogger
  • Psr\Log\LoggerTrait
  • Psr\Log\NullLogger
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerAwareTrait
  • Psr\Log\LogLevel

Monolog

Jordi Boggiano - https://github.com/seldaek/monolog

 

Monolog is a PSR-3-compatible logging library for PHP

 

Monolog sends your logs to files, sockets, inboxes, databases and various web services.

Special handlers allow you to build advanced logging strategies.

Monolog

Usage

 

 

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');

Monolog

Log Levels

  • DEBUG (100)

  • INFO (200)

  • NOTICE (250)

  • WARNING (300)

  • ERROR (400)

  • CRITICAL (500)

  • ALERT (550)

  • EMERGENCY (600)

Monolog

Handlers

  • Log to files and syslog

    • StreamHandler: Logs records into any PHP stream, use this for log files.
    • RotatingFileHandler: Logs records to a file and creates one logfile per day. It will also delete files older than $maxFiles. You should use logrotate for high profile setups though, this is just meant as a quick and dirty solution.
    • SyslogHandler: Logs records to the syslog.
    • ErrorLogHandler: Logs records to PHP's error_log() function.

Monolog

Handlers

  • Send alerts and emails

    • NativeMailerHandler: Sends emails using PHP's mail() function.
    • SwiftMailerHandler: Sends emails using a Swift_Mailer instance.
    • PushoverHandler: Sends mobile notifications via the Pushover API.
    • HipChatHandler: Logs records to a HipChat chat room using its API.
    • FlowdockHandler: Logs records to a Flowdock account.
    • SlackHandler: Logs records to a Slack account.
    • MandrillHandler: Sends emails via the Mandrill API using a Swift_Message instance.
    • FleepHookHandler: Logs records to a Fleep conversation using Webhooks.

Monolog

Handlers

  • Log specific servers and networked logging

    • SocketHandler: Logs records to sockets, use this for UNIX and TCP sockets.
    • ZendMonitorHandler: Logs records to the Zend Monitor present in Zend Server.
    • AmqpHandler: Logs records to an amqp compatible server. Requires the php-amqp extension (1.0+).
    • GelfHandler, CubeHandler, RavenHandler, NewRelicHandler, LogglyHandler, RollbarHandler, SyslogUdpHandler, LogEntriesHandler.

Monolog

Handlers

  • Logging in development

    • FirePHPHandler: Handler for FirePHP, providing inline console messages within FireBug.
    • ChromePHPHandler: Handler for ChromePHP, providing inline console messages within Chrome.
    • BrowserConsoleHandler: Handler to send logs to browser's Javascript console with no browser extension required. Most browsers supporting console API are supported.

Monolog

Handlers

  • Log to databases

    • RedisHandler: Logs records to a redis server.
    • MongoDBHandler: Handler to write records in MongoDB via a Mongo extension connection.
    • CouchDBHandler: Logs records to a CouchDB server.
    • DoctrineCouchDBHandler: Logs records to a CouchDB server via the Doctrine CouchDB ODM.
    • ElasticSearchHandler: Logs records to an Elastic Search server.
    • DynamoDbHandler: Logs records to a DynamoDB table with the AWS SDK.

Monolog

Handlers

  • Wrappers / Special Handlers

    • FingersCrossedHandler, NullHandler, BufferHandler, GroupHandler, FilterHandler, TestHandler, WhatFailureGroupHandler
  • Formatters
    • LineFormatter, HtmlFormatter, NormalizerFormatter, ScalarFormatter, JsonFormatter, WildfireFormatter, ChromePHPFormatter, GelfMessageFormatter, ...

Monolog

Handlers

  • Processors

    • IntrospectionProcessor, WebProcessor, MemoryUsageProcessor, MemoryPeakUsageProcessor, ProcessIdProcessor, UidProcessor, GitProcessor, TagProcessor.
  • Utilities
    • Registry, ErrorHandler, ErrorLevelActivationStrategy, ChannelLevelActivationStrategy.

Thank you!

PSR-3 & Monolog

By Chau Pham

PSR-3 & Monolog

  • 1,267