【问题标题】:\n not putting text on a new line in php error_log function\n 不在 php error_log 函数中将文本放在新行上
【发布时间】:2013-10-04 12:30:08
【问题描述】:

我试图将我的 error.log 文件中的错误放在单独的行上,但 \n 似乎没有这样做。我觉得我错过了一些简单的东西,但它们都在同一行,所以如果我在那里有一堆错误,几乎不可能找到它们。

<?php

$msg_one = "Error message 1.\n ";
$msg_two = "Error message 2.\n ";

$log_file = "C:\\inetpub\\wwwroot\\logging\\errors.log";

error_log($msg_one, 3, $log_file); 
error_log($msg_two, 3, $log_file);

?>

日志文件中的输出如下所示:

Error message 1. Error message 2.

【问题讨论】:

  • Windows,你能试试"\r\n"吗?或者更好的是,PHP_EOL 常量?附带说明:我更喜欢使用trigger_error,它只是遵循标准的error_reporting / log_errors / display_errors 配置开箱即用,所以我只需要为开发和生产进行不同的配置,而无需在产品开发中使用不同的代码。

标签: php error-handling error-logging


【解决方案1】:

使用"\r\n" 而不是"\n"

<?php

$msg_one = "Error message 1.\r\n ";
$msg_two = "Error message 2.\r\n ";

$log_file = "C:\\inetpub\\wwwroot\\logging\\errors.log";

error_log($msg_one, 3, $log_file); 
error_log($msg_two, 3, $log_file);

?>

【讨论】:

  • 这对我不起作用。文本用 \n 打印消息,而 \r 消失了,之后的任何文本都将在同一行上。
  • 发现问题(需要所有的双引号)stackoverflow.com/questions/41978290/…
猜你喜欢
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2015-09-14
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多