php php中error_log中的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2987465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:20:19  来源:igfitidea点击:

newline in error_log in php

php

提问by Satish

How to insert a newline while using error_log()in php

如何error_log()在php中使用时插入换行符

I tried to use <br>and \nbut those didn't work.

我尝试使用<br>\n但那些没有用。

回答by Konerak

Use double quotes when putting the error message:

放置错误消息时使用双引号:

error_log("This is a two lined message. \nThis is line two.");

should work.

应该管用。

error_log('This is a one lined message. \nThis is the same line still.');

will not work: notice the single quotes.

不起作用:注意单引号。

回答by guyaloni

As mentioned before, you can use either PHP_EOLor use double quotes in order to output a newline to your log file.

如前所述,您可以使用PHP_EOL或使用双引号将换行符输出到日志文件。

Anyway, when using linux console in order to debug the application, the tailcommand will show the new line as \\\\n.

无论如何,当使用 linux 控制台调试应用程序时,该tail命令会将新行显示为\\\\n.

Simple solution is to use sedin order to replace \\\\nwith \\n:

简单的解决方案是使用sed以替换\\\\n\\n

tail -f file | sed 's/\n/\n/g'

See this answer:
https://serverfault.com/a/126409

看到这个答案:https:
//serverfault.com/a/126409

回答by Mladen Janjetovic

PHP_EOL manage newlines on multiple platforms:

PHP_EOL 在多个平台上管理换行符:

error_log("Error message".PHP_EOL, 3, 'error.log');

回答by Berto

I occasionally run into this problem, but certain PHP interpreters don't always play fair with \non their own. My ugly solution requires adding the raw ASCII code for newline, \10, seems to do the trick. So try \n\10:

我偶尔会遇到这个问题,但某些 PHP 解释器\n本身并不总是公平对待。我丑陋的解决方案需要为换行符添加原始 ASCII 代码\10,似乎可以解决问题。所以尝试\n\10

error_log("\n\n".$this->db->last_query()."\n\n");

It's miserable looking code, so you might want to wrap this into your own function... but it makes the error log look a lot better if you want something to stand out.

看起来很糟糕的代码,所以你可能想把它包装到你自己的函数中......但是如果你想要一些突出的东西,它会使错误日志看起来更好。

I also have no clue why this works better... don't care enough to find out, but if someone knows why, it'd be cool to hear.

我也不知道为什么这会更好......不要太在意找出原因,但如果有人知道为什么,听到会很酷。

回答by Trentj

When debugging, if you want to make your Error Log line stand out while you're watching the log 'tail -f', I use a little function like this to make the line stand out and easily readable.

调试时,如果您想在查看日志 'tail -f' 的同时使错误日志行突出,我会使用一个像这样的小函数来使该行突出并易于阅读。

Just throw in some white space, and a common term like 'CUSTOM_LOG' for later grepping purposes.

只需添加一些空白,以及一个像“CUSTOM_LOG”这样的常用术语,以便以后进行搜索。

highlight_error_log('Msg here');

function highlight_error_log($str){
    error_log('CUSTOM_LOG:            ' . $str . '            ');
}

回答by Pikamander2

If your error log path is undefined in both your php.inifile and your function call, then \nand \r\nwon't work, even when double quoted (they'll show up as literal characters instead).

如果您的php.ini文件和函数调用中的错误日志路径都未定义,则\n\r\n将不起作用,即使是双引号(它们将显示为文字字符)。

You can fix that by either specifying a location in php.ini:

您可以通过在 中指定一个位置来解决这个问题php.ini

error_log = /var/log/php-errors.log

or in each PHP function call like this:

或在每个 PHP 函数调用中,如下所示:

error_log("error message", 3, $logFileLocation);

回答by Nguy?n H?u

This is the correct answer PHP_EOLmanage newlines on multiple platforms :

这是PHP_EOL在多个平台上管理换行符的正确答案:

error_log("Error message".PHP_EOL, 3, 'error.log');

https://blog.zohear.com/

https://blog.zohear.com/