PHP code example of logvice / phplogger

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

    

logvice / phplogger example snippets




use LogVice\PHPLogger\Logger;
use LogVice\PHPLogger\Config;
use LogVice\PHPLogger\Output\TCPOutput;
use LogVice\PHPLogger\Output\UDPOutput;

// create a config instance
$config = new Config();
$config->setAppKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$config->setEnvironment('dev');
$config->setChannel('php');
$config->activateBacktrace();
$config->setOutputHandlers([
    new TCPOutput('127.0.0.1', '8080'),
    new UDPOutput('127.0.0.1', '514'),
]);
$config->setLogLevel(Logger::DEBUG);

// create a log instance
$log = new Logger($config);

// add records to the log
$log->debug('foo');
$log->info('bar');
$log->notice('foo');
$log->warning('bar');
$log->error('foo');
$log->critical('bar');
$log->alert('foo');
$log->emergency('bar');
$log->log(Logger::ERROR, 'foo');

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    // create a config instance
    $config = new LogVice\PHPLogger\Config();
    $config->setAppKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    $config->setEnvironment('dev');
    $config->setChannel('php');
    $config->setOutputHandlers([
        new LogVice\PHPLogger\Output\TCPOutput('127.0.0.1', '8080'),
        new LogVice\PHPLogger\Output\UDPOutput('127.0.0.1', '514'),
        new LogVice\PHPLogger\Output\FileOutput('path/to/logs', 'file-name', true)
    ]);
    $config->setLogLevel(Logger::ERROR);

    // create a log instance
    $logger = new LogVice\PHPLogger\Logger($config);

    $logger->handleError($errno, $errstr, $errfile, $errline);
});

set_exception_handler(function ($exception) {
    // create a config instance
    $config = new LogVice\PHPLogger\Config();
    $config->setAppKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    $config->setEnvironment('dev');
    $config->setChannel('php');
    $config->setOutputHandlers([
        new LogVice\PHPLogger\Output\TCPOutput('127.0.0.1', '8080'),
        new LogVice\PHPLogger\Output\UDPOutput('127.0.0.1', '514'),
        new LogVice\PHPLogger\Output\FileOutput('path/to/logs', 'file-name', true)
    ]);
    $config->setLogLevel(Logger::ERROR);

    // create a log instance
    $logger = new LogVice\PHPLogger\Logger($config);

    $logger->handleException($exception);
});

register_shutdown_function(function () {
    // create a config instance
    $config = new LogVice\PHPLogger\Config();
    $config->setAppKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    $config->setEnvironment('dev');
    $config->setChannel('php');
    $config->setOutputHandlers([
        new LogVice\PHPLogger\Output\TCPOutput('127.0.0.1', '8080'),
        new LogVice\PHPLogger\Output\UDPOutput('127.0.0.1', '514'),
        new LogVice\PHPLogger\Output\FileOutput('path/to/logs', 'file-name', true)
    ]);
    $config->setLogLevel(Logger::ERROR);

    // create a log instance
    $logger = new LogVice\PHPLogger\Logger($config);

    $logger->handleShutdownError();
});