CHECKING STATUS
I AM LISTENING TO
|

Making Your Life Easier: Debugging with Monolog and Wonolog in WordPress

24. April 2025
.SHARE

Table of Contents

Why Bother with Proper Logging?

Let’s be honest – debugging can be a real pain. We’ve all been there, staring at a blank screen with no clue what went wrong. Or worse, getting vague error messages that lead nowhere. That’s where good logging comes in!

PHP’s built-in error reporting is… well, it’s there. But it’s pretty basic. It’s like trying to fix a complex machine with just a hammer. You need more specialized tools for serious development work.

That’s where Monolog enters the picture. Think of it as your debugging Swiss Army knife – it gives you a ton of options to see what’s really happening in your code.

Looking for a log navigator? Checkout my article about LNAV.
Looking for a external error logger or Sentry alternative? Checkout these … Bugsink / Glitchtip.

What’s Monolog Anyway?

Monolog is basically the rock star of PHP logging libraries. It’s what all the cool frameworks use (Laravel, Symfony – you name it). With millions of downloads, it’s become the go-to solution for anyone who needs to know what’s happening in their application.

The beauty of Monolog is how flexible it is. Want to log to files? Sure. Databases? No problem. Email, Slack, or pretty much anywhere else? Monolog has got you covered. It plays nicely with other systems too, thanks to following the PSR-3 standard (fancy talk for “it works well with other logging tools”).

Why Monolog Rocks for Debugging

Here’s why Monolog is your new best friend when it comes to squashing bugs:

  • It’s multi-talented – Send your logs to different places at once (like keeping detailed logs in a file while getting urgent errors via email)
  • It knows how serious things are – With eight different levels from “just FYI” to “everything’s on fire!”, you can filter what matters
  • It’s context-aware – Automatically add extra info to your logs like user data or request details
  • It’s pretty – Customize how your logs look so they’re actually readable
  • It’s organized – Group logs by channels so you’re not searching through a haystack

All this means you spend less time scratching your head and more time actually fixing problems!

Getting Started with Monolog

Installation: Quick and Painless

First things first, let’s get Monolog installed. It’s as simple as:

That’s it! One line and you’re ready to roll. (If you’re new to Composer, check out the getting started guide first.)

Basic Setup: Your First Debug Logger

Here’s a simple setup to get you started:

This creates a logger that will capture all your debug info in a file and tell you exactly which line of code triggered each log. Pretty neat, right?

Making the Most of Monolog

Log Levels: Choose Your Battle

Monolog gives you eight different log levels, from “I’m just being chatty” to “EVERYTHING IS BROKEN!”:

  • DEBUG: “Hey, just FYI, this happened” (for development)
  • INFO: “Something normal but noteworthy happened”
  • NOTICE: “This is unusual but not worrying”
  • WARNING: “This isn’t great but we’re still functioning”
  • ERROR: “Houston, we have a problem”
  • CRITICAL: “Major component down!”
  • ALERT: “Someone needs to fix this NOW”
  • EMERGENCY: “The building is on fire and everyone’s panicking”

When you’re debugging, you’ll mostly use the first three levels, saving the scarier ones for actual problems. Here’s how you might use them:

Context: The Secret Sauce

Want to make your logs actually useful? Add context! Compare these two:

See the difference? The second one tells you what’s actually going on. When you come back to these logs later (and trust me, you will), you’ll thank yourself for the extra details.

Cool Monolog Tricks for Debugging

Rotating Log Files (So Your Disk Doesn’t Explode)

Nobody wants a 10GB log file. Here’s how to create daily logs that clean up after themselves:

Browser Console Magic

When working on frontend stuff, why not send debug info right to your browser’s console?

Wake-Me-Up Alerts

For those “someone fix this now!” moments:

The Multi-Logger Approach

Why settle for one logging method when you can have several? Here’s a setup that gives you the best of all worlds:

Automatic Context: Be Lazy (In a Good Way)

Why manually add the same info to every log when you can automate it?

Now every log entry automatically includes this info. It’s like having an assistant who fills in all the details you’d otherwise forget! Check out the full list of available processors.

Wonolog: Monolog for WordPress Fans

If you’re a WordPress developer, you might be thinking “This Monolog thing sounds great, but I work in WordPress-land!” Well, you’re in luck – that’s exactly where Wonolog comes in.

What’s Wonolog All About?

Wonolog is basically Monolog wearing a WordPress costume. It’s a special package that connects WordPress’s way of doing things with Monolog’s powerful logging features. The team at Inpsyde created it to let WordPress developers join the logging party.

The cool part? You don’t have to rewrite your WordPress code to use it. Wonolog listens for WordPress actions and hooks, then does the logging for you.

Getting Wonolog Up and Running

Quick Install

Just like Monolog, installation is a one-liner:

Super Simple Setup

Wonolog’s setup is ridiculously easy:

  1. Make sure Composer’s autoloader is loaded early in WordPress
  2. Create a super-simple must-use plugin with this tiny code:

Tada! You now have powerful logging for WordPress. Wonolog will create daily log files in {WP_CONTENT_DIR}/wonolog/{Y/m/d}.log – for example, /wp-content/wonolog/2023/04/24.log.

What Gets Logged Automatically

Wonolog is smart about what it logs based on your WP_DEBUG_LOG setting:

  • If WP_DEBUG_LOG is true: “Log all the things!”
  • If WP_DEBUG_LOG is false: “Only log the scary stuff” (ERROR level and above)

Right out of the box, Wonolog catches:

  • PHP notices, warnings, and errors (oops!)
  • Exceptions that nobody caught (double oops!)
  • WordPress errors like database problems, HTTP API fails, wp_mail() issues, and those pesky 404s

WordPress-Style Debugging with Wonolog

The WordPress Way to Log

The real magic of Wonolog is how it works with WordPress’s action system. Want to log something? Just do:

See how natural that feels in WordPress? No need to create logger objects or remember Monolog syntax – just use WordPress actions like you normally would.

Keeping Things Organized with Channels

If you’re working on a bigger project, you’ll want to organize your logs. Wonolog lets you create custom channels:

Now all your shop order logs are grouped together, making it way easier to track down issues.

WordPress-Specific Debugging

Here are some cool tricks for debugging WordPress components:

Taking Wonolog to the Next Level

Custom Log Files

Want your logs somewhere specific? No problem:

Different Logging for Different Environments

You don’t want the same logging setup in development and production. Here’s how to adjust based on environment:

Auto-Magical WordPress Info

Want to add WordPress-specific details to all your logs automatically? Here’s how:

Pro Tips for Debugging with Wonolog

  1. Dev vs Prod: Log everything in development, be picky in production
  2. Organize with Channels: Create separate channels for different parts of your site
  3. Context is King: Always include user IDs, post IDs, and other relevant data
  4. Be Specific with Levels: Don’t mark everything as ERROR – be honest about severity
  5. Log All the Things (in Development): When building features, log liberally to understand what’s happening
  6. Keep Production Logs Clean: Nobody wants to sift through gigabytes of debug logs
  7. Think About Performance: In high-traffic sites, use buffered logging to reduce disk writes

Wrapping Up

Monolog and Wonolog might sound like fancy technical tools, but they’re really just about making your life easier as a developer. Think of them as your debugging buddies – there to help you figure out what’s going wrong and why.

With Monolog, you get powerful, flexible logging for any PHP application. With Wonolog, you get all that power in a WordPress-friendly package that just feels natural to use.

Start adding proper logging to your projects, and you’ll wonder how you ever lived without it. Trust me – your future self will thank you when you’re trying to track down that weird bug at 2 AM!

Want to learn more? Check out these resources:

Now go forth and log all the things (but maybe not in production)!

Bonus: Integrating with Sentry / Glitchtip / Bugsink

Sentry SDK must be present.

FAQ

What is Monolog and why should I use it for debugging?

Monolog is the de-facto standard logging library for PHP that allows you to send your logs to files, sockets, inboxes, databases, and various web services. It’s powerful for debugging because it offers eight severity levels, multiple handlers, processors for adding context automatically, and formatters for customizing log appearance. Using Monolog gives you better visibility into your application’s behavior, making it easier to identify and fix issues.

How do I install Monolog in my PHP project?

Installing Monolog is simple using Composer. Just run:

This will add Monolog to your project and update your composer.json and composer.lock files.

What are the different log levels in Monolog and when should I use each?

Monolog follows the PSR-3 standard with eight log levels from lowest to highest severity:

  • DEBUG: Detailed information for development
  • INFO: Noteworthy events (user logins, SQL logs)
  • NOTICE: Normal but significant events
  • WARNING: Exceptional occurrences that aren’t errors
  • ERROR: Runtime errors that don’t require immediate action
  • CRITICAL: Critical conditions like component unavailability
  • ALERT: Issues requiring immediate action
  • EMERGENCY: System is unusable

For debugging, DEBUG, INFO, and WARNING are most commonly used, while higher levels are for actual error conditions.

How can I prevent my log files from getting too large?

You can use Monolog’s RotatingFileHandler to automatically rotate log files and limit their size:

This creates daily log files and automatically deletes files older than the specified number of days.

How do I add contextual information to my logs?

You can add context by passing an array as the second parameter to any log method:

You can also use processors to automatically add context to all logs, such as web request information, file/line where the log was triggered, and memory usage.

Can I send logs to multiple destinations at once?

Yes, Monolog allows you to use multiple handlers simultaneously. For example:

This setup logs all messages to a file, but only sends errors and above via email.

What is Wonolog and how does it relate to Monolog?

Wonolog is a WordPress-specific implementation of Monolog, created by Inpsyde. It bridges WordPress and Monolog, allowing WordPress developers to use Monolog’s powerful logging capabilities within WordPress’s ecosystem. Wonolog integrates with WordPress’s actions and hooks system, so you can log using familiar WordPress patterns without having to directly couple your code with Monolog.

How do I install Wonolog in WordPress?

Install Wonolog via Composer:

Then create a must-use plugin with this minimal code:

This simple setup will automatically start logging WordPress events to daily log files in the wp-content/wonolog/ directory.

What does Wonolog log by default?

By default, Wonolog logs the following:

  • PHP notices, warnings, and errors
  • Uncaught exceptions
  • WordPress errors (database errors, HTTP API errors, wp_mail() errors, 404 errors)

When WP_DEBUG_LOG is true, Wonolog logs everything. When it’s false, Wonolog only logs events with a level of ERROR or higher.

How do I log my own custom events with Wonolog?

You can log custom events using WordPress actions:

You can also create custom log channels:

How can I configure Wonolog to send error notifications?

You can customize Wonolog handlers to send notifications via email, Slack, or other services:

How can I change where Wonolog stores log files?

You can change the log directory using the ‘wonolog.bootstrap-config’ filter:

How do I use Monolog to debug browser/JavaScript issues?

For browser/JavaScript debugging, you can use Monolog’s BrowserConsoleHandler:

This handler sends log messages to the browser’s JavaScript console, which can be useful for debugging AJAX calls or other browser interactions.

Is there a way to format logs differently for different handlers?

Yes, you can use different formatters for different handlers:

This allows you to customize how logs appear in different destinations.

How do I setup different logging for development vs production in WordPress?

With Wonolog, you can configure environment-specific logging using the WP_ENV constant:

How do I log database queries in WordPress for debugging?

You can log WordPress database queries using Wonolog by hooking into the ‘query’ action:

This logs database queries along with a backtrace to help you find where queries are being generated.

Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

Ever hear web designers obsess about “above the fold” content? This term isn’t just designer jargon—it’s a fundamental concept that can make or break your website’s effectiveness. Let’s dive into what it means, why it matters, and how it works differently across the devices we use every day. What Exactly Is “Above the Fold”? The […]

Look, these days a solid VPN isn’t just nice to have—it’s practically essential. Whether you want to keep your browsing habits private, access that show that’s somehow available everywhere except your country, or just avoid getting hacked on sketchy coffee shop Wi-Fi, picking the right VPN makes all the difference. We’ve spent hours testing and […]

Need your Raspberry Pi to only update its display when content actually changes? Tired of constant refreshes wasting bandwidth and causing annoying flickers? I’ve got you covered! This guide walks through three practical solutions – from simple HTTP caching to real-time WebSockets – that will make your Pi display work smarter, not harder. Let’s dive […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.