How do i log errors and warnings into a file in php?

 Posted in PHP

How to Log Errors and Warnings(into error.log file) in a PHP

This is a quick post on error logging in PHP. The reason for sharing this small tip is I found it can be helpful for users to track the errors or debug the errors which are thrown in the background.

Recently, I was working on integrating Facebook chatbot for one of our clients. While developing a Facebook chatbot we need to set up a webhook to which Facebook sends a user’s message in the background. Then our code should respond back on the basis of the message.

For some reason, our app was not responding back to the users. This whole process operates in the background and I got stuck on the problem for almost 2 hours.

Then suddenly I got an idea of logging the errors in a file so I can track what is wrong with the code. And then I was able to track errors and resolved all the problems. Without error logs, I couldn’t solve my problem as I was in a completely wrong direction.

Place the below code in your configuration file which will create an error.log file in your root directory.

Once the above code is added, all your application errors and warnings start logging into the error.log file. This tip is also helpful for live servers to track the problems in your code.

Related Articles

  • How to Log Query in Laravel
  • Debugging WordPress – How To Use WP_DEBUG On Production Site

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

When developing PHP applications, error logs are under-used because of their apparent complexity. PHP error logs are helpful, especially when configured and used properly.

While there are advanced tricks to truly squeeze every last drop of utility out of error logs, this article will cover the basics of configuration and the most common use cases so you can get up and running quickly.

Once you’ve gotten comfortable using error logs effectively, you can move on to more advanced tools for further enhancing your PHP development productivity.

Setting up PHP error logging

To enable error logging for your site or application, follow these steps:

  • Locate the php.ini file on your server.
  • Look for the line containing the error_reporting entry.
  • Ensure there is not a semicolon (;) in front of the entry.
  • Set the error_reporting entry equal to the desired level of logging (covered next). For example, you might set it to error_reporting = E_ALL.

Error reporting levels

There are numerous reporting levels to allow you to select exactly what you’d like to be notified of. Below are some of the most commonly used ones. A full list can be found in the official PHP documentation.

  • E_ALL—Logs all errors and warnings
  • E_ERROR—Logs fatal runtime errors
  • E_WARNING—Logs non-fatal runtime errors
  • E_NOTICE—Logs runtime notices (typical bugs in code)

As a final step in the basic configuration of PHP error logging, you’ll want to decide whether to display the errors on the client (browser). This is easily done:

  • Look for the line containing the display_errors entry in the php.ini file
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the display_errors entry to On or Off. For example, display_errors = Off will not show errors on the client

Where are the error logs?

Once you’re all set up with logging and generating plenty of errors (for practice!), you’ve got to actually look at them to determine what’s going on.

The default error log differs from environment to environment, so it’s advisable to manually set the location of the file. Here’s how:

  • Look for the line containing the error_log entry in the php.ini file
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the error_log entry to the desired path of the log file. For example, you might use error_log = /logs/php-errors.log.

Note this, though: you must restart your server for the changes to the php.ini file to take effect.

How do i log errors and warnings into a file in php?

How to use PHP error logs effectively

There are several handy error logging functions built directly into the PHP engine, which should be enough to cover basic use cases. When your codebase gets to a point where it needs more advanced solutions, something like Stackify’s post on PHP logging best practices may be what you’re looking for

Some more commonly used error logging functions are covered below, but you can find a comprehensive list (naturally) in the official PHP documentation.

error_log()

The error_log()  function allows for a string (required) to be sent to the log file. You can also send the message to an email address. There are a few other options, but they’re more advanced so they won’t be covered here.

In its most basic form, error_log() writes a message to the error log:

error_log(“There’s been a problem!”);

Using this function to troubleshoot data structures or variables is fairly straightforward. Here’s an example:

$toolsArray = array("Retrace", "Basic Error Logging", "Hammer");
error_log(print_r($toolsArray, true));

In this way, the contents of the variable sent as the first parameter of the print_r() function will be displayed in the logs, which is a great way to keep track of data that may change over time or needs to be monitored during development.

debug_print_backtrace()

While viewing code backtraces isn’t typically done via the error log files, it can be helpful for smaller applications or when getting familiar with troubleshooting.

The most readable way to send a backtrace to the log file is:

ob_start();
debug_print_backtrace();
error_log(ob_get_clean());

This uses PHP’s built-in buffer as well as the previously mentioned error_log() function to provide insight as to the source of errors produced by an application.

Clean up after yourself

PHP error logs don’t typically clear or truncate themselves by default. Good practice suggests you only enable logging when needed during development and you delete log files when they’re no longer needed.

Where to go from here

Now you’ve got all the tools for effective error logging in PHP, you can debug with confidence and eventually explore more advanced utilities like Retrace. Retrace allows you to see the error in context with the rest of your logging messages. These additional insights will give you a trail into what was happening leading up to the error.

How do i log errors and warnings into a file in php?

Start sending your logs and errors to Stackify by signing up for a free trial.

Have fun and happy coding!

  • About the Author
  • Latest Posts

Where does PHP log errors?

The location of the error log file itself can be set manually in the php. ini file. On a Windows server, in IIS, it may be something like "'error_log = C:\log_files\php_errors. log'" in Linux it may be a value of "'/var/log/php_errors.

How do you log something in PHP?

Enabling the PHP Error Log Log messages can be generated manually by calling error_log() or automatically when notices, warnings, or errors come up during execution. By default, the error log in PHP is disabled. You can enable the error log in one of two ways: by editing php. ini or by using ini_set .

How can I send error message from PHP to HTML?

To show invalid input in PHP, set the name of the input textbox which is in HTML. All the fields are first checked for empty fields and then it is validated for correctness. If all fields are correct then it shows the success message.

How do I enable PHP logging?

To enable error logging for your site or application, follow these steps:.
Locate the php. ini file on your server..
Look for the line containing the error_reporting entry..
Ensure there is not a semicolon (;) in front of the entry..
Set the error_reporting entry equal to the desired level of logging (covered next)..