log4php appender that logs to error_log() - php

How can you log to web server's log ( error_log() ) using log4php? I think what this ultimately means is to have a log appender that logs to error_log(). Seems like a no brainer, but I couldn't find anything in the log4php documentation and I couldn't find anything online about someone already doing this... Is this something that's easy to do and I'm just missing it?

I haven't found such an appender either. Seems like there is no such thing. You'd probably be best of if you are using the FileAppender and give it the name of the error_log ini setting if that is a filename.
If the error_log directive is set to syslog, there is a SyslogAppender.
Note however that the error_log() function itself has a target parameter that most of the time is identical to what dedicated appenders already provide. With the added uncertainty of where the error messages really end up depending on configuration, I feel it's a better idea to leave it up to the Log4PHP configuration. Sending mail, logging to a file, logging to syslog: All already supported. The only case missing: Logging to the SAPI logging handler.

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 save date-wise

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.

Set PHP error_handler on server side

We are trying to secure a large application (including some third party applications) and we want to disable anything that would allow programmers or potential hackers to hide errors (that is because we are using some monitoring scripts to check the logs in real time and identify any security breach).
We have disabled error_reporting, ini_set (along with some other functions); we have also installed php extension scream to disable suppression operator (no errors are displayed to the user but everything is logged)
We are now looking for a solution to set the default php error_handler server side (instead of using set_error_handler as we do now)? We want to use a custom error handler (for a better logging of the error environment) but we do not want to allow anyone to suppress errors using set_error_handler function.
So basically we can either disable this function (and make sure that all errors will be logged by the default handler) or maybe find a solution to set this server side (maybe an extension or some other trick) for better logging.
Any idea would really help!
UPDATE
We're using Apache with mod_php so we cannot use [PATH] to disable_functions for a certain script. Changing this is not exactly an option.
You can try to use a prepend script which runs before each request
php_value auto_prepend_file prepend.php
In this script you can set the error handler:
<?php
set_error_handler('yourHandler');
/* EOF */
And give only this script the right to use the set_error_handler() function.

PHP - attempt to log using error_log() causes 500 error

I have an IIS server which is serving PHP via fastcgi.
When the error log file is written to by a user other than one in IIS_IUSRS group (The group the IIS User is running under) the file becomes un-writable by IIS and the PHP calls to error_log() causes a 500 error. (At least that's my guess seeing as if I delete the log file, the error dissapears and the log file is re-created).
Is there anyway I can stop the 500 error from happening?
EDIT: To be clear I know I can stop this by stopping logging, logging to event log or different location etc, but that's not what I mean. I mean I just want to prevent the 500 error, I don't care enough that my system can't log that it should break the site when it tries to. That's exactly the worst behaviour it could have. I just want the 500 error to not happen and the site to continue working.
Since your scheduled task is actually changing the permissions on the error log file, the only viable options I can see are:
1) Make the scheduled task not write to the error_log. Add the following to the top of the cron job:
error_reporting(E_NONE);
2) Make the scheduled task write to the system log (event viewer in windows) by issuing the following command at the start of your scheduled task (PHP file):
ini_set('error_log', 'syslog');
3) If all of the above do not suit you, you can try scheduling the task as the IIS User/Group. This would insure that the permissions are met and error 500 is no longer caused.
There is no magic fix to this, you can either change the scheduled task so it has the same UID/GID as the PHP process, or you can stop logging in the scheduled_task.
Edit the php.ini, and find this line and edit:
error_log = /your/website/path/to/log
And sure to change the display_errors to off:
display_errors = off
Remember to put chmod 777 to the file :). If u wanna to see the file in the browser, can put something like this in the .htaccess file:
<Files /your/site/path/to/log/file.log>
allow from 10.0.1.1/16
deny from all
</Files>
PS: Sry, i dnt see the notation are a ISS server... hum, maybe can view more information about the error in the error_log of the ISS (i dont know where is)
I experienced this. Your error_log might be too big for a file. For example, when your error_log reaches 16mb the server will throw an error 500. You may delete your error_log and try if it still throws that error. You may wanna check the error_logs permissions and ownership too.
You want to avoid the error_log function from throwing a 500 Internal Server Error!
Well, if that may be the case, have you tried prepending your call to error_log with an '#'. That could possibly suppress the error. Please try and revert! :)
You could try using error_log() to write your application log in an alternative path:
error_log("Some fancy error to log", 3, "c:\tmp\your-custom-errors.log");
Or you could configure php to log to windows event log (in your php.ini):
error_log = syslog

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