• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

newline in error_log in php

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

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

      • 2
    • I wouldn’t do that. I would expect that every line in the error log is one single entry. Spreading a message over two or more lines would break this.

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.

  • 63
Reply Report
    • So? The other parameters are optional. Maybe you should specify what you want to achieve, because AFAICT you've just been given the solution to your problem.
      • 1
    • @Satish, sorry for the late comment, but, if we do not put any second and third parameter, the log gets written to php's default error log file: 'php_errors.log' which is welcome.

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

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

Simple solution is to use sed in order to replace \\\\n with \\n:

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

See this answer:

  • 30
Reply Report
      • 2
    • This is actually proof that error_log is not outputting newline characters properly. tail doesn't auto escape anything. This was the only thing that solved the issue for me.

PHP_EOL manage newlines on multiple platforms:

error_log("Error message".PHP_EOL, 3, 'error.log');
  • 26
Reply Report
      • 1
    • Good answer, but I'd suggest the other order: print each new error message on a new line: error_log(PHP_EOL."Error message", 3, 'error.log');

I occasionally run into this problem, but certain PHP interpreters don't always play fair with \n on their own. My ugly solution requires adding the raw ASCII code for newline, \10, seems to do the trick. So try \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.

  • 3
Reply Report

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.

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

highlight_error_log('Msg here');

function highlight_error_log($str){
    error_log('CUSTOM_LOG:            ' . $str . '            ');
  • 0
Reply Report

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).

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

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

or in each PHP function call like this:

error_log("error message", 3, $logFileLocation);
  • 0
Reply Report