从php脚本写入.log文件时创建新行


Creating new lines when writing to a .log file from a php script

我正在从php脚本中以以下方式写入名为mylog.log的.log文件

file_put_contents('mylog.log', "/ntestline");

脚本的这一部分将运行多次。它似乎在同一行上写入每个条目,当我运行它两长时间时,日志文件只是读取。

ASCII text, with very long lines

如何从php脚本写入.log文件时添加新行?

正如David Ericsson在他的评论中所发表的那样,您应该使用常数PHP_EOL,因为这将为服务器操作系统插入正确的行结束序列。

file_put_contents('mylog.log', PHP_EOL . "testline", FILE_APPEND);

也就是说,大多数文本查看器/编辑器可以处理'n'r'n -如果你进一步编辑文件,更智能的编辑器将继续使用相同的顺序。

由于PHP在Linux上最常用,如果您不使用PHP_EOL,我建议您使用'n

根据file_put_contents文档,您可以使用:

file_put_contents('mylog.log',"'r'ntestline",FILE_APPEND);

要在明文(如日志)中添加新行,请在字符串中使用"'n"。例子:

$string = "some text 'n some more text that will be on a new line";