5

Is it possible to send the same error to a global php error log in /var/log/php_errors as well as send the same error to a local error log in /var/www/mysite/php_errors?

On our staging server, I tail the log file, and a lot of wordpress stuff as well as some big ugly print_r's will come through from other developers. I'd like to have a global error file to see if there is anything breaking server wide from time to time, as well as separate out my local errors so I can follow them better if I'm only interested in one site at the moment.

1
  • This sucks, error_log=log1;log2 doesn't work either. Commented Apr 15, 2016 at 13:57

4 Answers 4

3

Use set_error_handler and make a custom error function with as much logging as you like.

Sign up to request clarification or add additional context in comments.

3 Comments

"The following error types cannot be handled with a user defined function: E_ERROR, ..." Good idea, but I cause way too many fatal errors.
For those you can use register_shutdown_function (php.net/manual/en/function.register-shutdown-function.php), would also make sense to use set_custom_handler (php.net/manual/en/function.set-exception-handler.php) too for any uncaught exceptions
There is a class available here that does what @Ing suggests: stackoverflow.com/a/30637618/5240159
2

If you use a library such as log4php then you can configure a wide range of logging options, including writing to multiple files, sending emails, etc based on log type.

Comments

1

Not that I am aware of. However, you could use the error_prepend_string ini setting and prepend all of your error logs with something. And then when you tail your log files you can grep on whatever you set and it will show you only messages from that site (so long as you choose something fairly unique.)

http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-prepend-string

2 Comments

Can we set this in a .htaccess file? This is probably the simplest solution. I need to brush up on my grep-pipe-fu.
error_prepend_string works for error display output but not for logging.
0

You can use this code in a shared file and call appropriate logging function:

function log_to_file_1($msg){
   ini_set("log_errors", 1); 
   ini_set("error_log", "/tmp/file_1.log");
   ini_set('log_errors_max_len', 1024); // Logging file size
   error_log($msg);
}

function log_to_file_2($msg){
   ini_set("log_errors", 1); 
   ini_set("error_log", "/tmp/file_2.log");
   ini_set('log_errors_max_len', 1024); // Logging file size
   error_log($msg);
}

You can also use a single logging function with filename as a parameter:

function mysql_error_log($filename, $msg){
  ini_set("log_errors", 1); 
  ini_set("error_log", "/tmp/$filename");
  ini_set('log_errors_max_len', 1024); // Logging file size
  error_log($msg);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.