ride/lib-log

Log library of the Ride framework

1.2.0 2023-08-23 13:35 UTC

This package is auto-updated.

Last update: 2024-10-23 16:01:44 UTC


README

Log library of the PHP Ride framework.

Logging is used to keep an history of events or to debug an application.

What's In This Library

LogMessage

A log message defines what's being done or what happened.

It consists a:

  • level: error, warning, information or debug
  • title: title of the message
  • description: detailed information about the message
  • date: date and time of the event
  • microtime: microseconds in the application run
  • id: id of the log session
  • source: source library or module which logged the message
  • client: Id of the client (eg. an IP address)

LogSession

A LogSession is a collection of log messages which belong together. For example, all logged messages from handling the same HTTP request.

Log

The log object is the facade to the library which offers an easy interface to log messages. It uses the observer pattern to dispatch those logged messages to the listeners of the log.

LogListener

A log listener performs the actual logging of the message. The most common thing to do is write a log message to a file. An implementation to do just that has been provided.

BrowseableLogListener

The browseable log listener is an extension of the regular log listener. It adds functionality to retrieve and inspect log messages back from the log.

Code Sample

Check this code sample to see the possibilities of this library:

<?php

use ride\library\decorator\LogMessageDecorator;
use ride\library\log\listener\BrowseableFileLogListener;
use ride\library\log\Log;

// obtain the client and generate a log session id
$client = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
$logSessionId = 'abc123';

// create a listener
$listener = new BrowseableFileLogListener('/path/to/log.file'); // make sure it's writable
$listener->setFileTruncateSize(512); // in kilobytes
$listener->setLogMessageDecorator(new LogMessageDecorator()); // formats the log messages

// create the log object
$log = new Log();
$log->setId($logSessionId);
$log->setClient($client);
$log->addLogListener($listener);

// do some logging
$log->logDebug('Debug message');
$log->logInformation('Information message', 'with a description', 'source');
$log->logWarning('Warning message', 'with a description', 'my-module');
$log->logError('Debug message', 'with a description');
$log->logException(new Exception('A exception'));

// browse the log
$logSessions = $listener->getLogSessions(array('limit' => 10, 'page' => 2), $pages);
$logSession = $listener->getLogSession($logSessionId);
$logMessages = $logSession->getLogMessages();
$logMessages = $logSession->getLogMessagesBySource('my-module');
$logMessages = $logSession->getLogMessagesByQuery('message');

Installation

You can use Composer to install this library.

composer require ride/lib-log