简体   繁体   English

php中的error_log中的换行符

[英]newline in error_log in php

How to insert a newline while using error_log() in php 如何在php中使用error_log()时插入换行符

I tried to use <br> and \\n but those didn't work. 我试图使用<br>\\n但这些都不起作用。

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. 不起作用:注意单引号。

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

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

As mentioned before, you can use either PHP_EOL or 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 tail command will show the new line as \\\\\\\\n . 无论如何,当使用linux控制台调试应用程序时, tail命令会将新行显示为\\\\\\\\n

Simple solution is to use sed in order to replace \\\\\\\\n with \\\\n : 简单的解决方案是使用sed以用\\\\n替换\\\\\\\\n \\\\n

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

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

I occasionally run into this problem, but certain PHP interpreters don't always play fair with \\n on their own. 我偶尔会遇到这个问题,但是某些PHP解释器并不总是能够自己公平地使用\\n My ugly solution requires adding the raw ASCII code for newline, \\10 , seems to do the trick. 我丑陋的解决方案需要为换行添加原始ASCII代码, \\10 ,似乎可以解决问题。 So try \\n\\10 : 所以试试\\n\\10

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

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. 我也不知道为什么这样做效果更好......不在乎找不到,但如果有人知道为什么,听到它会很酷。

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

If your error log path is undefined in both your php.ini file and your function call, then \\n and \\r\\n won'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);

This is the correct answer PHP_EOL manage newlines on multiple platforms : 这是正确答案PHP_EOL管理多个平台上的换行符:

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

https://blog.zohear.com/ https://blog.zohear.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM