Skip to content

webbmaffian/php-logger-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Logger Client

Prerequisites

  • ED25519 private SSL key
  • Signed certificate
  • CA certificate

Initialization

Simple

<?php
use Webbmaffian\Logger\Logger;
use Webbmaffian\Logger\TlsClient;

// Named arguments work since PHP 8.0
$client = new TlsClient(
    host: '127.0.0.1',
    port: 4610,
    caCertPath: 'ca.pem',
    certPath: 'cert.pem',
    privKeyPath: 'key.pem',
);

$logger = new Logger($client);

With fallback

<?php
use Webbmaffian\Logger\Logger;
use Webbmaffian\Logger\TlsClient;
use Webbmaffian\Logger\Directory;

// Named arguments work since PHP 8.0
$client = new TlsClient(
    host: '127.0.0.1',
    port: 4610,
    caCertPath: 'ca.pem',
    certPath: 'cert.pem',
    privKeyPath: 'key.pem',
);

// Use a directroy as fallback. When the fallback occurs, the directory will be filled with up to 1 file per minute.
$fallback = new Directory('logs');
$logger = new Logger($client, $fallback);

// Whenever suitable, transfer the log entries from the fallback client to the main client. If there are no entries
// in the fallback client, nothing will happen.
$logger->transferFromFallback();

Logging

Simple

$logger->info('Hello world');

With 2 tags

$logger->info('Hello world', 'tag1', 'tag2');

With 1 interpolated tag + 1 normal tag

$logger->info('Foo %s bar', 'tag1', 'tag2');

With meta data

$logger->info('Order %d failed', $orderNumber, [
    'something' => 'foobar',
    'somethingElse' => 1234,
]);

Get a globally unique log entry ID

$entryId = $logger->err('Oopsie daisy');

// Give the ID to the user so that the development team can look it up
echo $entryId;

Throwing

Log entries

<?php
use Webbmaffian\Logging\Error;

try {

    // Severity 'error'
    throw new Error('Order %d failed', $orderNumber, [
        'something' => 'foobar',
        'somethingElse' => 1234,
    ]);
} catch(Error $e) {

    // The whole log entry is contained in the exception, so just send
    $logger->send($e);
}

Other exceptions

try {
    throw new Exception('Something happened');
} catch(Exception $e) {

    // Other exceptions have no severity level - log the usual way
    $logger->err($e);
}

Get a globally unique log entry ID

<?php
use Webbmaffian\Logging\Error;

try {

    // Severity 'error'
    throw new Error('Order %d failed', $orderNumber, [
        'something' => 'foobar',
        'somethingElse' => 1234,
    ]);
} catch(Error $e) {

    // The exception itself contains the entry ID
    $entryId = $e->getId();

    // When sending an entry, the ID is also returned
    $entryId = $logger->send($e);

    // Give the ID to the user so that the development team can look it up
    echo $entryId;
}

Severity levels

Level Name Description Log Throw
0 Emergency System is unusable - everybody panic $logger->emerg() new Emergency()
1 Alert Action must be taken immediately
E.g: corrupted system database, backup failures, etc.
$logger->alert() new Alert()
2 Critical Critical condition that prevents a specific task
E.g: fatal error, broken pages, etc.
$logger->crit() new Critical()
3 Error Non-critical errors, but that must be fixed.
E.g: failed API requests, etc.
$logger->err() new Error()
4 Warning Warnings about unexpected conditions that might lead to errors further on
E.g: "this shouldn't be possible, but let's check it any way"
$logger->warn() new Warning()
5 Notice Normal but significant conditions
E.g: undefined variables, and other problems that were maneuvered but should be prevented
$logger->notice() new Notice()
6 Info Informational events of normal operations
E.g: taken actions, form validation, blocked requests, user errors, etc.
$logger->info() -
7 Debug Helpful information for troubleshooting
E.g: anything verbose
$logger->debug() -

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages