Skip to content

A Loggly Monolog Handler with some extra common context processors

License

Notifications You must be signed in to change notification settings

golincode/monologgly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loggly Helpers

This extends the basic loggly handler provided by monolog, adding the following information as context:

  • the environment
  • whether the log happened on a request, or CLI call
  • information about the http request (url, method)
  • information about the origin of the log call (file and line, class and method (if relevant))
  • some simplified exception information (if the log comes from a "normal" exception log - ie. logging, or __toString-ing the exception)

Note: there is currently a bug with the class and method origin / backtrace information.

Installation

composer require golin/monologgly

Exceptions

To log exception information, simply add the exception to the context:

$logger->debug('Oh No!', ['exception' => $e]);

The exception object will be removed before final logging, and replaced with an array interpretation of it. It will still be available to all other processors added (as long as they are added before the exception information processor (which they will be using the provided service provider)).

Setup with Laravel

Config

Add the following to config/app.php:

    'loggly-token' => env('LOGGLY_TOKEN'),

and add your loggly token to your .env file. Ommitting this will mean that the loggly monlog handler will not be loaded (and nothing will be sent to loggly) - basically, it's safe to not have this key when developing locally.

Provider

Add the following file, as LogglyServiceProvider.php, and put it in your application's service providers config in config/app.php.

Update the $name property with your application's name.

<?php

namespace App\Providers;

use Golin\MonoLoggly\LogglyServiceProvider as BaseProvider;
use Monolog\Monolog;

class LogglyServiceProvider extends BaseProvider {

    /**
     * The log name. This should uniquely identify the log.
     *
     * @var string
     */
    protected $name;

    /**
     * The minimum log level.
     *
     * @var int
     */
    protected $level = Monolog::DEBUG;

    /**
     * A place to construct any other processors that will be added to 
     * the loggly handler.
     *
     * @return array   An array of callables objects
     */
    protected function processors()
    {
        return [];
    }

}