dongmo3413 2014-04-04 07:38
浏览 138
已采纳

写邮件到日志时是php monolog换行吗?

I have a piece of code.

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'));
    }
}

This is the log info:

[2014-04-04 07:20:41] [Log] [INFO] : Mytest {"name":"John"}

But if I have more logs, everything are in one line.

[2014-04-04 07:20:41] [Log] [INFO] : Mytest {"name":"John"}[2014-04-04 07:20:41] [Log] [INFO] : Mytest {"name":"John"}[2014-04-04 07:20:41] [Log] [INFO] : Mytest {"name":"John"}

As I am using StreamHandler , I look at its source code

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']);
}

Its just append message the log file.

So my question is: Do I need to handle wrap line by myself, or monolog already provide function to wrap a line?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongyutan1703 2014-04-04 07:51
    关注

    Oh no, my fault.

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

    Its simple, just config format like this:

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

    But I found that not working.

    But this works

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

    So use Double quotes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘