LoggerExtraBundle

Symfony2 Bundle for Logging related functionality.

MIT License

Downloads
3.3K
Stars
4

LoggerExtraBundle

Symfony2 Bundle for Logging related functionality. This bundle can give you these features:

  • Add a unique RequestId to each message of the current request.
  • Add a unique SessionId to each message dependent on current session id.
  • Add arbitrary "key: value" pairs to each message of the current request.
  • Create a log entry on a MASTER Request.
  • Create a log entry on a Response.

There is also a Stack Middleware for logging Requests and Responses: silpion/stack-logger

Installation

Using Composer:

php composer.phar require silpion/logger-extra-bundle

Also add SilpionLoggerExtraBundle to your AppKernel:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Silpion\LoggerExtraBundle\SilpionLoggerExtraBundle(),
        );
        ...
    }
}

Configuration

By default, this bundle will not do anything! Every feature has to be enabled on its own.

Example configuration:

silpion_logger_extra:

    # If a random request_id should be added to the [extra] section of each log message.
    request_id:           true

    # Class of the used request_id provider.
    request_id_provider:  Silpion\LoggerExtraBundle\Logger\Provider\Request\UniqRequestIdProvider

    # If a salted SHA1 of the session_id should be added to the [extra] section of each log message.
    session_id:           true

    # Class of the used session_id provider.
    session_id_provider:  Silpion\LoggerExtraBundle\Logger\Provider\Session\SymfonySessionIdProvider

    # If the current PID of the PHP Interpreter should be added to the [extra] section of each log message.
    process_id:           true

    # If the session should be started, so the session_id will always be available.
    session_start:        false

    # A list of "key: value" entries that will be set in the [extra] section of each log message (Overwrites existing keys!).
    additions:
        server_id: 42

    logger:

        # Will create a log entry on each incoming request.
        on_request:           true

        # Will create a log entry on each outgoing response.
        on_response:          true

Available Providers

RequestIdProvider

Next to the UniqRequestIdProvider, which will generate a simple sha1 hash, there is also the EnrichedRequestIdProvider that will generate MongoDb ObjectId like hashes containing the current timestamp, machineId, processId. The EnrichedRequestIdProvider will generate request_id, that will be sortable by creation time.

Usage

If you want to use the current RequestId or SessionId somewhere in your application, see this code:

$requestId = $this->get('silpion_logger_extra')->getRequestId();
$sessionId = $this->get('silpion_logger_extra')->getSessionId();

References

Funny enough a example for adding a RequestId is in a Symfony2 Cookbook, but not available in a bundle till now.