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
How to Choose a Financial Advisor - NerdWallet
How much cash should you hold in your portfolio during bull and bear markets?
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
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.