php error log save date-wise - php

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.

Related

How do I detect error in shared hosting/live server?

In localhost, we can easily find the errors like missing semicolon in line number xxx, undefined variable in line number xxx,class already declared etc. But, when I upload all my files in shared hosting and try to see results in the web browser, I get to see same error page everytime and it's really really hard to detect what exactly caused the error. The project which i uploaded to the live server was written in laravel. I have tried 'Display errors On' in php.ini, error_reporting(E_ALL). I have even checked the error_log in the root directory but, those logs are usually from few days ago. error image description here
Any possible solutions ? Or should I switch to server where proc_open is enabled?
Check this:
https://laravel.com/docs/7.x/errors
Laravel handles errors and can write them in a log file.
You can customize it if you want, but I don't think it's necessary to do so.
If you dont use any frameworks or ones that don't support such a feature for that matter you could always use the native PHP function set_error_handler and catch errors and write them in a log file.
Note: BEWARE of the excessive log file size. If your project has a lot of visitors or has lots of notices and warnings, this log files can get excessively massive, unreadable, and consuming your space. Don't turn it off, it's always good to know where are the errors, but check and debug them often and delete them when not needed.

PHP Error Log - php.ini Unique file for php errors

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

require_once fails on a readable file php wordpress site

I have a php script that is really simple, but requires some of the wordpress includes. I have used the code from their website for most of it but it is failing whenever I try to call the require_once parts of the scripts. here is the relevant code:
$fn = dirname(__FILE__) . '/wp-admin/includes/media.php';
if(!file_exists($fn))
{
echo 'No File';
}
if(!is_readable($fn))
{
echo 'File is unreadable';
}
require_once $fn;
Interstingly enough, The only echo that I get when the require_once is uncommented is the full path to the document. The file is both existing and readable. However, when I uncomment the require_once code it comes back with a 500 error.
On a slightly related point. What is the easiest way of debugging php. I haven't found anything that is VS easy yet (or even as easy to debug as Django!!)
PHP has various settings in php.ini for how logging works. You can also set them at runtime. It might be logging to a file somewhere rather than displaying the error. Consider trying:
error_reporting(E_ALL)
To debug your application. Don't leave it on when you're done working with it.
You should be developing and debugging the code locally on your workstation, not on a shared hosting server. On your local workstation you have access to the php.ini, Apache error log, and the PHP error log. You can also set project-specific settings for debugging using .htaccess files. For example, you can configure separate PHP error log files for each different project you're working on.
As #Steve Howard pointed out, there are many settings you can alter in the php.ini in order to get more detailed error reporting. In fact, most LAMP packages install a development copy of the php.ini, which is basically a template of a php.ini file that's optimized for detailed error reporting. Detailed error reporting is something you want on your local environment for debugging. It is NOT something want on your production environment where customers and potential hackers will end up seeing more information than they should!
If you don't know how to set up PHP on your workstation, take a look at XAMPP, MAMP, Macports, etc. There are many options. You don't need to be super technical to get a stack set up on your computer. Once you have a good dev environment setup on your computer you can tail the PHP error log and the Apache log.
One thing I like to do is to use the PHP error_log() function. This allows you to output custom messages to the PHP error log. For example, if I'm debugging code and I need to know what the value of $foo is, I can do something like this:
<?php
error_log("Foo is: " . print_r($foo, true));
This is much better than using var_dump() or echo to print debugging data to the screen because it won't interrupt the normal execution of the program or the display of its view layer.

Configuring PHP to store errors in database

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.

Get the PHP error log from PHP on remote hosting

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.

Categories