250

How do I turn on all error and warnings and log them to a file, but to set up all of that within the script (not changing anything in php.ini)?

I want to define a file name and so that all errors and warnings get logged into it.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Gorep
  • 2,525
  • 2
  • 13
  • 3

7 Answers7

374

Use the following code:

ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "Hello, errors!" );

Then watch the file:

tail -f /tmp/php-error.log

Or update php.ini as described in this blog entry from 2008.

Dave Jarvis
  • 28,853
  • 37
  • 164
  • 291
Aman
  • 4,316
  • 1
  • 14
  • 16
  • 43
    `ini_set` does only work if that code is executed. Not useful for code that has *parse errors* because the error will be *before* the code is executed. Instead write those changes into the php.ini. – hakre Apr 16 '13 at 23:31
  • 9
    If you can't edit php.ini, you should be able to add this in the .htaccess : `php_flag log_errors on php_value error_log /home/path/public_html/domain/PHP_errors.log`. See http://perishablepress.com/how-to-enable-php-error-logging-via-htaccess/ – Matthieu Jan 08 '14 at 08:47
  • 1
    I have a question, how to get the error.log file to get created in my htdocs folder instead? – Tommy Mar 05 '14 at 21:51
  • I guess you just change the folder from `tmp/php-error.log` to which ever location you desire? – Luke Apr 16 '14 at 12:37
  • This crashes my PHP in 5.4.0 – Ben Leggiero May 10 '14 at 03:05
  • @Tommy, So what was the error? Does changing the value of `error_log` work? – Pacerier Feb 23 '15 at 04:08
  • Could you explain `tail` is that linux command line tail recursion or windows? – Kolob Canyon Oct 19 '16 at 19:21
  • @KolobCanyon Tail is a Linux command (http://man7.org/linux/man-pages/man1/tail.1.html) – sianipard Dec 16 '16 at 14:51
98

See

  • error_log — Send an error message somewhere

Example

error_log("You messed up!", 3, "/var/tmp/my-errors.log");

You can customize error handling with your own error handlers to call this function for you whenever an error or warning or whatever you need to log occurs. For additional information, please refer to the Chapter Error Handling in the PHP Manual

Edward
  • 3,883
  • 7
  • 32
  • 76
Gordon
  • 296,205
  • 68
  • 508
  • 534
50

Simply put these codes at top of your PHP/index file:

error_reporting(E_ALL); // Error/Exception engine, always use E_ALL

ini_set('ignore_repeated_errors', TRUE); // always use TRUE

ini_set('display_errors', FALSE); // Error/Exception display, use FALSE only in production environment or real server. Use TRUE in development environment

ini_set('log_errors', TRUE); // Error/Exception file logging engine.
ini_set('error_log', 'your/path/to/errors.log'); // Logging file path
Yousha Aleayoub
  • 3,221
  • 2
  • 42
  • 58
  • 5
    That's only if you *also* want all errors to be displayed in output and/or to the browser, *in addition* to logging. `display_errors` should NEVER be turned on on a live production server -- that directive is specifically for output to the user, and has no effect on logging. http://www.php.net/manual/en/errorfunc.configuration.php#ini.display_errors – Aaron Wallentine Mar 09 '18 at 22:51
25

add this code in .htaccess (as an alternative of php.ini / ini_set function):

<IfModule mod_php5.c>
php_flag log_errors on 
php_value error_log ./path_to_MY_PHP_ERRORS.log
# php_flag display_errors on 
</IfModule>

* as commented: this is for Apache-type servers, and not for Nginx or others.

T.Todua
  • 44,747
  • 17
  • 195
  • 185
14

That's my personal short function

# logging
/*
[2017-03-20 3:35:43] [INFO] [file.php] Here we are
[2017-03-20 3:35:43] [ERROR] [file.php] Not good
[2017-03-20 3:35:43] [DEBUG] [file.php] Regex empty

mylog ('hallo') -> INFO
mylog ('fail', 'e') -> ERROR
mylog ('next', 'd') -> DEBUG
mylog ('next', 'd', 'debug.log') -> DEBUG file debug.log
*/
function mylog($text, $level='i', $file='logs') {
    switch (strtolower($level)) {
        case 'e':
        case 'error':
            $level='ERROR';
            break;
        case 'i':
        case 'info':
            $level='INFO';
            break;
        case 'd':
        case 'debug':
            $level='DEBUG';
            break;
        default:
            $level='INFO';
    }
    error_log(date("[Y-m-d H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);
}
Juergen
  • 1,126
  • 17
  • 27
7

Take a look at the log_errors configuration option in php.ini. It seems to do just what you want to. I think you can use the error_log option to set your own logging file too.

When the log_errors directive is set to On, any errors reported by PHP would be logged to the server log or the file specified with error_log. You can set these options with ini_set too, if you need to.

(Please note that display_errors should be disabled in php.ini if this option is enabled)

Amal Murali
  • 70,371
  • 17
  • 120
  • 139
Frxstrem
  • 30,162
  • 8
  • 66
  • 99
  • 1
    Why should `display_errors` be disabled if you enable `log_errors`? Doesn't make sense in my opinion. :) – Guido Hendriks Aug 20 '10 at 14:34
  • 6
    Because there's no need to output the content of the errors to the public on a production server, especially if the text of the error is being discreetly logged into a file. – Shabbyrobe Aug 20 '10 at 14:38
  • 3
    Display errors should be always disabled on production server. Not if error logging is configured to somewhere else, but always. Errors are logged to apache error log by default, that is often enough. – Pihhan Jul 31 '13 at 13:49
  • 1
    Displaying errors on production makes PHP more popular :) – PeterM Jul 19 '18 at 18:10
0

In addition, you need the "AllowOverride Options" directive for this to work. (Apache 2.2.15)

Stéphane
  • 375
  • 1
  • 5
  • 20