Monolog 从多个脚本记录到同一个文件是否安全?

Is Monolog safe for logging to same file from multiple scripts?

与使用带有 LOCK_EX 标志的 file_put_contents 进行日志记录相比,使用 Monolog 是否会消除潜在的瓶颈?

我有以下面向 public 的 PHP 脚本,它的平均执行速度约为 10x/s,高峰时间为 100x/s。由于 LOCK_EX.

脚本必须相互等待,这导致了瓶颈
<?php
    // ... do some things
    // then log the result:
    file_put_contents("/home/logs/public.log", "\n" . date("d.m. G:i:s") . " (some log) $httpcode $retry_count", FILE_APPEND | LOCK_EX);
?>

我正在考虑重写它以像这样使用 Monolog:

<?php
    require_once(DIR.'/vendor/autoload.php');
    use MonologLogger;
    use MonologHandlerStreamHandler;
    
    $logger = new Logger('public-script');
    $logger->pushHandler(new StreamHandler('/home/logs/public.log', Logger::INFO));

    // ... do some things
    // then log the result:
    $logger->info("(some log) $httpcode $retry_count");
?>

--

根据 Koala Yeung 的回答,使用 Monolog 无助于 解决瓶颈。我需要找到能够在不阻止脚本执行的情况下写入文件的其他东西。

您可以简单地编写一个脚本来测试自己:

main.php:

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

require __DIR__ . '/vendor/autoload.php';

$logger = new Logger('public-script');
$logger->pushHandler(new StreamHandler(__DIR__ . '/public.log', Logger::INFO));

$options = getopt("n:l:s:");

$name = $options['n'] ?? 'pid:' . getmypid();
$limit = (int) ($options['l'] ?? 1000);
$sleep = (int) ($options['s'] ?? 1000);

// ... do some things
// then log the result:
for ($i=1; $i<=$limit; $i++) {
        $logger->info("workering", [
                'worker' => $name,
                'count' => $i,
        ]);
        usleep($sleep);
}

运行它:

php main.php -n 'worker 1' &; php main.php -n 'worker 2' &

或运行它具有更小的间隔(每个日志 10 微秒):

php main.php -s 10 -n 'worker 1' &; php main.php -s 10 -n 'worker 2' &

结果:

[2021-02-09T19:37:51.649015+08:00] public-script.INFO: workering {"worker":"worker 1","count":1} []
[2021-02-09T19:37:51.650283+08:00] public-script.INFO: workering {"worker":"worker 1","count":2} []
[2021-02-09T19:37:51.650370+08:00] public-script.INFO: workering {"worker":"worker 1","count":3} []
[2021-02-09T19:37:51.650447+08:00] public-script.INFO: workering {"worker":"worker 1","count":4} []
[2021-02-09T19:37:51.649321+08:00] public-script.INFO: workering {"worker":"worker 2","count":1} []
[2021-02-09T19:37:51.650522+08:00] public-script.INFO: workering {"worker":"worker 1","count":5} []
[2021-02-09T19:37:51.650555+08:00] public-script.INFO: workering {"worker":"worker 2","count":2} []
[2021-02-09T19:37:51.650596+08:00] public-script.INFO: workering {"worker":"worker 1","count":6} []
[2021-02-09T19:37:51.650640+08:00] public-script.INFO: workering {"worker":"worker 2","count":3} []
[2021-02-09T19:37:51.650670+08:00] public-script.INFO: workering {"worker":"worker 1","count":7} []
[2021-02-09T19:37:51.650716+08:00] public-script.INFO: workering {"worker":"worker 2","count":4} []
[2021-02-09T19:37:51.650743+08:00] public-script.INFO: workering {"worker":"worker 1","count":8} []
[2021-02-09T19:37:51.650789+08:00] public-script.INFO: workering {"worker":"worker 2","count":5} []
[2021-02-09T19:37:51.650815+08:00] public-script.INFO: workering {"worker":"worker 1","count":9} []
[2021-02-09T19:37:51.650861+08:00] public-script.INFO: workering {"worker":"worker 2","count":6} []
[2021-02-09T19:37:51.650886+08:00] public-script.INFO: workering {"worker":"worker 1","count":10} []
[2021-02-09T19:37:51.650911+08:00] public-script.INFO: workering {"worker":"worker 2","count":7} []
[2021-02-09T19:37:51.650958+08:00] public-script.INFO: workering {"worker":"worker 1","count":11} []
[2021-02-09T19:37:51.650985+08:00] public-script.INFO: workering {"worker":"worker 2","count":8} []
[2021-02-09T19:37:51.651031+08:00] public-script.INFO: workering {"worker":"worker 1","count":12} []
[2021-02-09T19:37:51.651057+08:00] public-script.INFO: workering {"worker":"worker 2","count":9} []
[2021-02-09T19:37:51.651103+08:00] public-script.INFO: workering {"worker":"worker 1","count":13} []
...

日志交织得很好。

如果你阅读了StreamHandler::write(), you can see that it is using the same exclusive locking (LOCK_EX) mechanism as file_put_contents的源代码。