简体   繁体   中英

PHP errors to log only

I'm using PHP-CLI to generate an XML file of business reports on a schedule. I have error reporting off because having errors in my XML breaks the file.

Normally the script iterates through about 11000 products check for price changes and keeping a record of quantities and fulfillment channel data.

99% of the time the script runs perfectly. But sometimes it breaks for some reason after getting through a few thousand products. It doesn't seem to be repeatable. And it never happens when I'm at work.

So in my script (to protect valid xml), I have error_reporting(0); . The script is piping output from the commandline (win7) like so: c:\\...\\php.exe -fc:\\...\\testXMLUpdater.php > "C:\\...\\Database.xml".

I believe I can't use batch file error piping 2> because it's from PHP CLI and the errors aren't displayed when I watch the batch file running. I know the error is occurring in the php, not the batch file, because the php output is incomplete but the batch file finishes correctly.

From what I can see there are 2 options:
1. write the php errors into valid xml <error>Error Info</error> or
2. find some way to direct the error output to the log only.

Any thoughts on how to do either of these? Is one preferred?

There are no errors from the date in question in the error log or other times it's had an error. There are plenty of errors from while I was writing the code.

You should turn on error_reporting, but also have PHP log those errors instead of outputting them: http://php.net/manual/en/errorfunc.configuration.php#ini.error-log .

As well, if corruption of your output is a concern, then you should not be dumping your output to stdout and redirecting to file. Instead, have PHP directly write the generated text to file, eg

$out = fopen('Database.xml', 'wb');
fwrite($out, '<xml>.....</xml>');

that way no matter what PHP barfs up for errors, it'll never reach your output file, because PHP's errors went to a completely different location (stdout/logs).

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