yellottyellott
yellottyellott

Reputation: 985

Can you log php errors to multiple files?

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.

Upvotes: 5

Views: 3840

Answers (4)

Shawinder Sekhon
Shawinder Sekhon

Reputation: 1599

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

Upvotes: 0

Ingmar Boddington
Ingmar Boddington

Reputation: 3500

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

Upvotes: 3

Mark Baker
Mark Baker

Reputation: 212412

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.

Upvotes: 2

jasonlfunk
jasonlfunk

Reputation: 5249

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

Upvotes: 1

Related Questions