PHP code example of popphp / pop-log

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

    

popphp / pop-log example snippets


use Pop\Log\Logger;
use Pop\Log\Writer\File;

$log = new Logger(new File(__DIR__ . '/logs/app.log'));

$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Log\Logger;
use Pop\Log\Writer\File;

$log = new Logger(new File(__DIR__ . '/logs/app.csv'));

$context = [
    'name'      => 'my-log-entry',
    'timestamp' => date('Y-m-d H:i:s')
];

$log->info('Just a info message', $context);

use Pop\Log\Logger;
use Pop\Log\Writer\File;
use Pop\Log\Formatter;

$log = new Logger(new File(__DIR__ . '/logs/app.log', new Formatter\NdJson()));

use Pop\Log\Logger;
use Pop\Log\Writer\Mail;
use Pop\Mail\Mailer;
use Pop\Mail\Transport\Sendmail;

$emails  = ['[email protected]', '[email protected]'];
$options = [
    'subject' => 'Custom Log Entry:',
    'cc'      => '[email protected]'
];

$mailer = new Mailer(new Sendmail());
$log    = new Logger(new Mail($mailer, $emails, $options));

$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Db\Db;
use Pop\Log\Logger;
use Pop\Log\Writer\Database;

$db  = Db::connect('sqlite', __DIR__ . '/logs/.htapplog.sqlite');
$log = new Logger(new Database($db, 'system_logs'));

$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Log\Logger;
use Pop\Log\Writer;
use Pop\Http\Client;
use Pop\Http\Auth;

$client = new Client(
    'https://logs.mydomain.com/',
    ['method' => 'POST'],
    Auth::createKey('LOG_API_KEY')
);

$log = new Logger(new Writer\Http($client));
$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Log\Facility;
use Pop\Log\Logger;
use Pop\Log\Writer\Syslog;

$log = new Logger(new Syslog(Facility::LOCAL0, 'logs.mydomain.com', 514, 'my-app'));

$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Log\Logger;
use Pop\Log\Writer\Stream;

$log = new Logger(Stream::stdout());

$log->info('Just a info message');
$log->alert('Look Out! Something serious happened!');

use Pop\Log\Writer\Stream;
use Pop\Log\Formatter;

// An already-open resource (including STDOUT/STDERR) is never closed by the writer — the caller keeps
// ownership of it.
$stream = new Stream(STDOUT, new Formatter\Line());

// A string is opened here (fopen($stream, 'a')) and closed automatically when the writer is destroyed.
$memory = new Stream('php://memory');

use Pop\Log\Logger;
use Pop\Log\Writer\File;

$log = new Logger(new File(__DIR__ . '/logs/app.log'));
$log->info('User {user} logged in from {ip}', ['user' => 'nick', 'ip' => '1.2.3.4']);

$log->getLevel(Logger::ERROR); // 'ERROR'
Logger::getLogLevel(3);        // 'ERROR'

$context = [
    'name'      => 'my-log-entry',
    'timestamp' => date('Y-m-d H:i:s'),
    'format'    => 'json'
];

use Pop\Log\Logger;
use Pop\Log\Writer\File;

$log = new Logger(new File(__DIR__ . '/logs/app.log'));

$context = [
    'name'      => 'my-log-entry',
    'timestamp' => date('Y-m-d H:i:s')
];

$log->info('Just a info message', $context);

use Pop\Log\Logger;
use Pop\Log\Writer\File;

$log = new Logger(new File(__DIR__ . '/logs/app.log'));

$log->addProcessor(function (array $context): array {
    $context['request_id'] = bin2hex(random_bytes(8));
    return $context;
});

$log->addProcessor(function (array $context): array {
    $context['request_id'] = 'req-123';
    return $context;
});

$log->info('Handling request {request_id}'); // "Handling request req-123"

$log->addProcessors([
    function (array $context): array {
        $context['step'] = 'one';
        return $context;
    },
    function (array $context): array {
        $context['step'] .= '-two'; // sees 'one' from the previous processor
        return $context;
    }
]);

$log->addProcessor(function (array $context): array {
    $context['format'] ??= 'json';
    return $context;
});

use Pop\Log\Logger;
use Pop\Log\Writer\File;

$prodLog = new File(__DIR__ . '/logs/app_prod.log');
$devLog  = new File(__DIR__ . '/logs/app_dev.log');

$prodLog->setLogLimit(Logger::ERROR); // Log only ERROR and more severe
$devLog->setLogLimit(Logger::INFO);   // Log only INFO and more severe

$log = new Logger([$prodLog, $devLog]);

$log->alert('Look Out! Something serious happened!'); // Will write to both writers
$log->info('Just a info message');                    // Will write to only app_dev.log

$log = new Logger([$prodLog, $devLog]);
$log->setLogLimit(Logger::ERROR); // every writer on this Logger now only logs ERROR and more severe
text
Subject: Custom Log Entry: ALERT (alert)
2023-11-11 12:32:33    alert   ALERT   Look Out! Something serious happened!
text
2023-11-11 12:32:33    alert   ALERT   Look Out! Something serious happened!