Is it possible to configure php.ini to store errors in MySQL database rather than plain error-log?
The only option that I see is using php.ini to append file containing custom error handling function to every PHP script. Though, this doesn't sound efficient.
This is more of a server level question, if you don't like the answer that Frankie provided in his comment. Without using set_error_handler there is no way (in PHP) to output all errors to a log file instead of the log.
If you are using Apache, you can do the following:
CustomLog "|/path/to/custom_log_script.php [OPTIONS]"
(note the pipe)
That will allow you to use a custom error log handler to control what does and doesn't wind up in the log files.
You can set a cron job to execute a php script read the error log file for new errors and store them in the database. The error log table won't be real time though but I think it shouldn't be too big a problem.
Related
I'm currently working on a project and to give a helping hand, I've changed my php.ini file to log errors to "php_errors.log" inside the directory of where a PHP file has returned an error.
I was wondering, after searching online (maybe I'm not wording this correctly), is there any way to set a unique file name in the php.ini file for the different errors.
For example, if an error occurred on let's say account.php, is there any way to log an error file "account_php_errors.log" through the ini file?
From the ini file you can set a global error file, if you want to override that you can add the following lines to the "account.php"
ini_set("log_errors", 1);
ini_set("error_log", "/path/to/logfile.log");
error_log( "one error" );
Or if you use a framework you could check the documentation to see how you can customize errors. Frameworks will have advanced logging mechanisms.
There is no way for you to use php.ini to log to multiple files based on the php file that generated the fatal error.
You could try and use register_shutdown_function to catch fatal errors then log to individual files using the ['file'] you get in the array response to a get_last_error.
If you were practicing Object Oriented Programming with try/catch Exceptions you could use some logging method to separate out error into individual files.
Options inside the php.ini file itself are not this flexible (which is actually a good thing so that other programs such as logrotate can be applied in an effective manner). You can look at accomplishing what you need via your application (whether that is a framework or purely custom code). You could just grep out what you need if you're on a linux system or search in the Event Viewer on Windows. It really depends on what your specific needs really are.
You can create a custom error handler within the application. You should be able to get all the needed details from here
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline) {
// file_put_contents
}
set_error_handler("myErrorHandler");
More info:
http://php.net/manual/en/function.set-error-handler.php
I would rethink the purpose for keeping errors logs in different directories, because it will make Your app messy. Greping logs from different folders alone is not so convienient and if You ever want to add log monitoring tools like Logstash with elasticsearch, you will be forced to do a more complex setup
We want to monitor all php errors. We can check all errors with error_log file.
Error logs files become so heavy.
So it is very difficult to check errors in error_log file.
Could it possible to write php error_log file on date-wise example '19-05_2015_error.log'
or we can write error like "fetal.log" "notice.log"
You can absolutely do it in PHP. A popular logging library is Monolog.
You can change the PHP config at beginning of your application, create the log file dynamically.
<?php
function logByDate(){
$sPath = '/logs/application/PhpError_' . date('Y-m-d') . '.log';
ini_set('error_log', $sPath);
}
// To validate only, it is not necessary
echo 'Actual log ' . ini_get('error_log') . PHP_EOL;
logByDate(); //Call it at beginning
// To validate only, it is not necessary
echo 'Validate new log ' . ini_get('error_log') . PHP_EOL;
To get logs in daily format (19-05_2015_error.log) you can use logrotate if you're on a UNIX stack (but it' a bit more complicated if you're on a Windows stack).
There are some useful hints on controlling duplication and suppressing error messages.
As far as I know, the mod_log_config module does not accept variables for file names so that excludes the most straightforward possibility. Apache-based solutions you can actually use include basically:
Send logs to a script and let the script choose the file name.
Good old log rotation.
Said that, I think PHP has pretty good built-in logging. You can do exactly what you are asking for with the error_log() function (together with e.g. date('Y-m-d')). You can even define a different file for different error types. Sure, that will not capture errors that prevent PHP code from running (such as parse errors, request time-outs...) but you can set the error_log directive as fallback mechanism—these situations should be rare enough to keep the log file manageable.
One more thought: make sure your development box has full error reporting enabled (many devs use third-party bundles with default settings and happily write buggy code). It isn't normal to have so many logged errors in a production server.
I am sending in error log information using PHP's error_log() function and it's working just fine except that every line -- when run from the web-browser under MAMP -- is prepending a timestamp such as this:
[26-Jun-2013 00:29:23 Europe/London] My message goes here
Obviously a timestamp isn't the worst idea but I'd like to have control over the full text and how and where the timestamp goes on the line. How does this initial text get set? I thought it must be in the php.ini or httpd.ini files but on quick perusal I couldn't find it.
You can't, at least not in Apache 2.
It is not possible to customize the error log by adding or removing
information. However, error log entries dealing with particular
requests have corresponding entries in the access log. For example,
the above example entry corresponds to an access log entry with status
code 403. Since it is possible to customize the access log, you can
obtain more information about error conditions using that log file.
http://httpd.apache.org/docs/2.2/logs.html#errorlog
So if you want a custom formatted log, don't use apache's error log.
Create your own error handler: http://php.net/manual/en/function.set-error-handler.php It works with trigger_error()
If you're running Apache it appears this may be something controlled by mod_log_config. There is a LogFormat directive in your httpd.conf that controls the format for error messages
Similar thread: How to change default timestamp that PHP uses for logging to file?
Is there a PHP function or some other way of obtaining the PHP error log as a string?
I need this because I cannot access the error log of a site I am running on someone else's server. - He offered to email me the error log, but that isn't exactly convenient.
Is there some way I could output the error log to a PHP page?
I realize that viewing the entire server's error log is not really going to happen for me. However, I know you can do something like this to email a manual error_log call to yourself:
error_log('A really bad error', 3, 'me#myemail.com');
Is it possible to configure a page to email errors to you instead of displaying them?
On a badly secured server, yes. But on most servers there are two users: apache and [ you ]. You don't have access to the server logs, since they are owned by the apache user (or whichever server you're using).
However, you could probably try it:
echo file_get_contents('/var/log/httpd/error_log');
Note: that's the default location on a RedHat-based apache server. It may be different
Update To reflect the updated question
No, you cannot view the error log with error_log - it is a one-way process that gets handled by the webserver. It only writes the log, but you cannot read it.
You can probably display the errors with this:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
You could even use set_error_handler to handle all warnings and notices (for example, to mail them). But that's pretty much all you can do.
How would I go about changing where PHP logs it's syntax errors? I've got multiple people working on the same server, and I want to be able to send my own logs to a custom file so that I'm not looking at every one else's errors.
I can do this:
error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log','path/to/mylogfile.log');
And that works for some errors, but for actual syntax errors, the logs are still being sent to the master /var/log/httpd/error_log. How do I make the syntax errors get sent to my custom log file?
Since the file cannot be correctly parsed, the ini_set function is not executed either and neither is the new error log set. You need to set php_value error_log /path/to/myfile.log in an .htaccess file or the global server config (I'm assuming that you're using Apache).
Given that syntax errors prevent PHP from running you will not be able to set log paths via PHP. It's a chicken before the egg situation.
Alternatives would be:
Change the error log at the webserver level
Enable output of error messages to the browser
I'd suggest the latter.