PHP: Making sense of the 8 log levels

In reality, you‘ll probably use four

Photo by Osman Rana on Unsplash

Every time I need to create an app, there are parts that I want to log for different reasons: debugging, statistics, potential warnings or flat-out errors.

Most frameworks and packages follow the PSR-3 standard, which describes how a Logging system works. It’s basically an interface you should rely on when pushing logs to a system. From these, Monolog is the most used as it’s very flexible and easy to understand.

Back to the point, the PSR-3 implementation describes eight log levels. You read that right. In order of “severity”: Emergency, Alert, Critical, Error, Warning, Notice, Info and Debug. That’s a lot, if you ask me.

It’s not a problem to understand for which scenarios you should use each log level, but trying to decide which is sometimes confusing. Is an error also an alert? Is a debug message informational too?

Logs fall on many levels

Making sense of the log levels is not difficult if we make an example with an application.

This application captures the activity of many digital keylocks (IoT) from a third party hub, which later can be seen in a dashboard. Each employee has its own card and password for every keylock. These would send events to the hub, which in turn would push the data into the app through a simple HTTP Request.

8. Debug is development

When I finished development, I mounted a staging version for internal use, where we could see how the application behaved transparently. A lot of debug messages with fake but sensible information was written verbatim, with an old keylock we used for testing.

On production, these messages are not written, since the Log Level is 7, meaning, log everything that’s not a debug message.

7. Informational is statistical

The app logs every sign in as info, with a lot of information except sensible data, like the credentials. This is pure statistical information about the activity on the securer compound, which should be useful to make graphs from about who moved where, when.

6. Notice is relevant

When an user cycles its keycode for a door, we log that event as notice. It’s normal to cycle the keycode after a period of time, as its mandatory, but it’s not normal to see employees changing it before this period.

That doesn’t mean they were forced to do it, but sometimes people forgets their keycode and decide to generate a new one.

5. Warning is undesired

When an user fails to sign in, I push a multiple-fails message at warning level, with the ID of the user. This way we can know if someone tried to access by brute-forcing the code.

Most of the time this is not an error, but a significant event that may help when dealing with unauthorized access.

4. Error is unstable

Some old keylock models pushed garbage from time to time. Worn-out keycard would spit wrong IDs. Signal interference is also a thing. When any of these occurred, the hub would report an “error” to the app, which in turn would write an error message in the log.

The message includes which door was not working fine. A technician would be dispatched later to check the keylock.

3. Critical is stateful

Each minute, plus some random seconds, a “ping” would be sent by the hub to each keylock to check their status. I was pretty much sure it worked with WebSockets over BLE, but be my guest.

These rarely failed to respond, but when they do, they’re practically AWOL as the hub probably ensures multiple times it’s not a false positive. Any non-responding keycode event would be logged as critical.

2. Alert is unsecure

If any keycode sent a “blocked” response to the app, like when somebody tries to force their way in, it would be logged as alert. Security (or law enforcement) would be dispatched immediately to secure the room. Rarely this was set by someone accidentally bumping on the door or knocking, so this mechanism was very reliable. My guess is that the keylock has some well-calibrated smart sensors and a gyroscope.

This was also sent if the app received any weird or not expected request (like a “pong”, for sure from the hub. It’s not that the hub can’t be hacked, but nonetheless.

1. Emergency is unusable

Finally, if the hub itself went down for more than 1 minute, an emergency event would be logged, as someone would have probably cut the power for the hub, which would make a blind spot on door activity.

Putting everything into a table

I made this table to check which log level I should need to put the message I want to log. This has helped me a lot cleaning messages once I apply a filter by severity.

It’s normal to take some liberties and push messages in other log levels to make more sense even if these do not follow any property at all. For example, when the storage is half-full, that’s an critical right away, since the provider takes its sweet time to add more storage to the machine.

Just using four

At the beginning of the article I wrote that I mostly use four log levels. This is true for most simple applications, as some other log levels are covered by default by other systems.

  • Debug: For anything that shows the inner workings of the app.
  • Error: Something that shouldn’t happen, happened.
  • Alert: Anything that risks the state of the app on the short term.
  • Emergency: Any part of the application has stopped working.

This gives me a bit of peace of mind since I can know easily what happened, and with which severity. I hope it works for you too.

--

--

--

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Pub/Sub Lite as a source with Spark Structured Streaming on Databricks

Trias Weekly Report ( September 20th, 2021 — September 26th, 2021)

A Data Engineer’s Perspective on AWS Managed Airflow vs. AWS Step Function (State Machine)

https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-workflow-studio-using.html

Introduction to CS Bootcamp Recap

Weekly Report of Trias (Dec 16, 2019 — Dec 22, 2019)

Adding NAT Gateway to provide internet access to instances running in the private subnet

DrHook — The UM / UKCA Profiling Tool

Building a Grammar Corrector App with React, Python, Flask, Tesseract, and Gramformer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Italo Baeza Cabrera

Italo Baeza Cabrera

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.

More from Medium

Migrating From PHP 7.4 to PHP 8.0

An image of migrating birds in a sunset sky

Calculate PHP cache time with ease

PHP: 10 tips to use for Mockery

Voila! Symfony and PHP 8.1