简体   繁体   中英

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.

使用set_error_handler并使用任意数量的日志记录来创建自定义错误函数。

如果使用诸如log4php之类的库,则可以配置多种日志记录选项,包括根据日志类型写入多个文件,发送电子邮件等。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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