PHP code example of migears / log

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

    

migears / log example snippets


use MiGears\Log\Logger;
use Psr\Log\LogLevel;

// File logger
$log = Logger::toFile('/var/log/app.log', LogLevel::DEBUG, 'app');

$log->info('User {user} logged in', ['user' => 'Alice']);
$log->warning('Disk space low: {percent}%', ['percent' => 85]);
$log->error('Database connection failed', ['host' => 'db1']);

$log = Logger::toStream(STDERR, LogLevel::WARNING, 'cli');
$log->error('Something went wrong');

$log = Logger::null();
$log->debug('This will be silently discarded');

// Send logs to any destination via a callable
$log = new Logger(function (string $line): void {
    // e.g. send to syslog, Slack, database...
    syslog(LOG_INFO, $line);
}, LogLevel::INFO, 'app');

use Psr\Log\LogLevel;

// From lowest to highest priority:
LogLevel::DEBUG      // 0
LogLevel::INFO       // 1
LogLevel::NOTICE     // 2
LogLevel::WARNING    // 3
LogLevel::ERROR      // 4
LogLevel::CRITICAL   // 5
LogLevel::ALERT      // 6
LogLevel::EMERGENCY  // 7

$log->info('Hello, {name}! You have {count} messages.', [
    'name' => 'Bob',
    'count' => 5,
]);
// Output: [2024-01-01 12:00:00] [app] INFO: Hello, Bob! You have 5 messages.

use MiGears\Web\MiRest;
use MiGears\Log\Logger;

$rest = new MiRest(__DIR__ . '/resources', 'App\\Resources');

// Register logger as a service
$rest->set('logger', function () {
    return Logger::toFile(__DIR__ . '/logs/app.log', 'debug', 'app');
});

// In a resource:
class Users extends AbstractResource
{
    public function POST(Request $request): Response
    {
        $this->resolve('logger')->info('User created', ['id' => $userId]);
        return Response::json(['status' => 'ok']);
    }
}

use MiGears\Log\Logger;
use Psr\Log\LogLevel;

// 文件日志
$log = Logger::toFile('/var/log/app.log', LogLevel::DEBUG, 'app');

$log->info('用户 {user} 已登录', ['user' => 'Alice']);
$log->warning('磁盘空间不足:{percent}%', ['percent' => 85]);
$log->error('数据库连接失败', ['host' => 'db1']);

$log = Logger::toStream(STDERR, LogLevel::WARNING, 'cli');
$log->error('出错了');

$log = Logger::null();
$log->debug('这条会被静默丢弃');

// 通过可调用函数将日志发送到任何目的地
$log = new Logger(function (string $line): void {
    // 例如发送到 syslog、Slack、数据库...
    syslog(LOG_INFO, $line);
}, LogLevel::INFO, 'app');

use Psr\Log\LogLevel;

// 从低到高优先级:
LogLevel::DEBUG      // 0
LogLevel::INFO       // 1
LogLevel::NOTICE     // 2
LogLevel::WARNING    // 3
LogLevel::ERROR      // 4
LogLevel::CRITICAL   // 5
LogLevel::ALERT      // 6
LogLevel::EMERGENCY  // 7

$log->info('你好,{name}!你有 {count} 条消息。', [
    'name' => 'Bob',
    'count' => 5,
]);
// 输出:[2024-01-01 12:00:00] [app] INFO: 你好,Bob!你有 5 条消息。

use MiGears\Web\MiRest;
use MiGears\Log\Logger;

$rest = new MiRest(__DIR__ . '/resources', 'App\\Resources');

// 将日志器注册为服务
$rest->set('logger', function () {
    return Logger::toFile(__DIR__ . '/logs/app.log', 'debug', 'app');
});

// 在资源类中:
class Users extends AbstractResource
{
    public function POST(Request $request): Response
    {
        $this->resolve('logger')->info('用户已创建', ['id' => $userId]);
        return Response::json(['status' => 'ok']);
    }
}

[2024-01-15 10:30:45] [app] INFO: User Alice logged in
[2024-01-15 10:31:02] [app] WARNING: Rate limit approaching for user 123
[2024-01-15 10:31:10] [db] ERROR: Connection timeout on host db2

[2024-01-15 10:30:45] [app] INFO: 用户 Alice 已登录
[2024-01-15 10:31:02] [app] WARNING: 用户 123 即将达到速率限制
[2024-01-15 10:31:10] [db] ERROR: 主机 db2 连接超时