簡體   English   中英

當寫消息到日志時,php monolog是否換行?

[英]Is php monolog wrap line when write message to log?

我有一段代碼。

class LogApi {

    private $log;
    private $path;
    private $format = array(
        'date' => 'Y-m-d H:i:s',
        'message' => '[%datetime%][%channel%][%level_name%] : %message% %context%',
    );
    private $formatter;

    function __construct() {
        $this->log =  new \Monolog\Logger('Log');
        $this->path = LOG_ADMIN_REDIS_INIT_FILE;
        $this->formatter = new \Monolog\Formatter\LineFormatter($this->format['message'], $this->format['date']);

    }

    public function log($path = LOG_ADMIN_REDIS_INIT_FILE, $level = Monolog\Logger::INFO){

        $stream = new \Monolog\Handler\StreamHandler($path, $level);
        $stream->setFormatter($this->formatter);
        $this->log->pushHandler($stream);

        $this->log->addInfo('Mytest', array('name'=>'John'));
    }
}

這是日志信息:

[2014-04-04 07:20:41] [日志] [INFO]:Mytest {“ name”:“ John”}

但是,如果我有更多的日志,則所有內容都在一行中。

[2014-04-04 07:20:41] [日志] [INFO]:Mytest {“名稱”:“約翰”} [2014-04-04 07:20:41] [日志] [INFO]:Mytest { “ name”:“ John”} [2014-04-04 07:20:41] [Log] [INFO]:Mytest {“ name”:“ John”}

當我使用StreamHandler ,我看一下它的源代碼

protected function write(array $record)
{
    if (null === $this->stream) {
        if (!$this->url) {
            throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
        }
        $errorMessage = null;
        set_error_handler(function ($code, $msg) use (&$errorMessage) {
            $errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg);
        });
        $this->stream = fopen($this->url, 'a');
        restore_error_handler();
        if (!is_resource($this->stream)) {
            $this->stream = null;
            throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$errorMessage, $this->url));
        }
    }
    fwrite($this->stream, (string) $record['formatted']);
}

它只是附加消息日志文件。

所以我的問題是:我需要自己處理wrap line ,還是monolog已經提供了換行功能?

提前致謝。

哦,不,我的錯。

private $format = array(
    'date' => 'Y-m-d H:i:s',
    'message' => '[%datetime%][%channel%][%level_name%] : %message% %context%',
);

其簡單的配置格式如下:

'message' => '[%datetime%][%channel%][%level_name%] : %message% %context% \n',

但是我發現那沒有用。

但這有效

'message' => "[%datetime%][%channel%][%level_name%] : %message% %context% \n",

所以用雙引號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM