PHP code example of ride / lib-log

1. Go to this page and download the library: Download ride/lib-log library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ride / lib-log example snippets




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