PHP Error Log Guide: Configuration And Use Cases - Stackify (2024)

By: Gaege

| March 14, 2024

PHP Error Log Guide: Configuration And Use Cases - Stackify (1)

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.inifile on your server.
  • Look for the line containing theerror_reportingentry.
  • Ensure there is not a semicolon (;) in front of the entry.
  • Set the error_reportingentry equal to the desired level of logging (covered next). For example, you might set it toerror_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 thedisplay_errorsentry in thephp.ini file
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the display_errors entry to Onor Off.For example,display_errors = Offwill 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 theerror_logentry in the php.inifile
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the error_logentry to the desired path of the log file. For example, you might useerror_log = /logs/php-errors.log.

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

PHP Error Log Guide: Configuration And Use Cases - Stackify (2)

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

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

error_log()

Theerror_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 areafew 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.

PHP Error Log Guide: Configuration And Use Cases - Stackify (3)

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

Have fun and happy coding!

Related posts:

  • PHP Error Log Basics
  • PHP Monolog Tutorial: A Step by Step Guide
  • PHP Error Handling Guide
  • A Guide to Logging in Azure Functions

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

  • App Performance Management
  • Code Profiling
  • Error Tracking
  • Centralized Logging

Learn More

PHP Error Log Guide: Configuration And Use Cases - Stackify (9)

Author

Gaege

More articles by Gaege

PHP Error Log Guide: Configuration And Use Cases - Stackify (2024)
Top Articles
Amazon Prime Rewards Visa Signature Card Review
How sellers can cancel an order
Mate Me If You May Sapir Englard Pdf
New Slayer Boss - The Araxyte
Nwi Police Blotter
Craigslist Pet Phoenix
Ncaaf Reference
Geometry Escape Challenge A Answer Key
Uc Santa Cruz Events
litter - tłumaczenie słowa – słownik angielsko-polski Ling.pl
What Does Dwb Mean In Instagram
Power Outage Map Albany Ny
Void Touched Curio
Otterbrook Goldens
Fdny Business
Hellraiser III [1996] [R] - 5.8.6 | Parents' Guide & Review | Kids-In-Mind.com
Buy Swap Sell Dirt Late Model
How To Level Up Roc Rlcraft
Aps Day Spa Evesham
Invitation Homes plans to spend $1 billion buying houses in an already overheated market. Here's its presentation to investors setting out its playbook.
The BEST Soft and Chewy Sugar Cookie Recipe
PCM.daily - Discussion Forum: Classique du Grand Duché
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Anesthesia Simstat Answers
Taylored Services Hardeeville Sc
Core Relief Texas
Angel del Villar Net Worth | Wife
Red Sox Starting Pitcher Tonight
Kristen Hanby Sister Name
Sf Bay Area Craigslist Com
2024 Coachella Predictions
Robot or human?
Appraisalport Com Dashboard /# Orders
Truckers Report Forums
Vanessa West Tripod Jeffrey Dahmer
To Give A Guarantee Promise Figgerits
Die Filmstarts-Kritik zu The Boogeyman
Academic important dates - University of Victoria
Kelly Ripa Necklace 2022
Chatropolis Call Me
Is The Nun Based On a True Story?
2700 Yen To Usd
sacramento for sale by owner "boats" - craigslist
Discover Things To Do In Lubbock
Craigslist Odessa Midland Texas
Disassemble Malm Bed Frame
Poe Self Chill
Quaally.shop
1990 cold case: Who killed Cheryl Henry and Andy Atkinson on Lovers Lane in west Houston?
Ty Glass Sentenced
Fahrpläne, Preise und Anbieter von Bookaway
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6193

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.