PHP code example of devnav2902 / utilitylog

1. Go to this page and download the library: Download devnav2902/utilitylog 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/ */

    

devnav2902 / utilitylog example snippets


UtilityLog::writeLog('error', 'Foo');
UtilityLog::writeLog('info', 'Bar');

// in try-catch block
try {
     throw new \Exception('Something happened!');
} catch (\Throwable $th) {
     UtilityLog::writeLog('error', 'Error in try-catch block', $th);
}

// contextual information
UtilityLog::writeLog('info', 'User {id} failed to login.', null, ['id' => $user->id]);



return [
    'customize_formatter' => "[%channel%][%level_name%] %datetime%\n%message% %extra%\n\n",
    'date_format' => 'Y-m-d H:i:s',
    'message_json_option' => JSON_PRETTY_PRINT,
    'allow_inline_linebreaks' => true,
    'ignore_empty_context_and_extra' => true,
    '

class CustomizeFormatter
{
    /**
     * Customize the given logger instance.
     */
    public function __invoke(Logger $logger): void
    {
        foreach ($logger->getHandlers() as $handler) {
            $lineFormatter = new LineFormatter(
                config('utilitylog.customize_formatter'),
                config('utilitylog.date_format'),
                config('utilitylog.allow_inline_linebreaks'),
                config('utilitylog.ignore_empty_context_and_extra'),
                config('utilitylog.