Swift Log Levels: Specifying log message severity

In this lesson

In this lesson, I will teach about the concept of log level, which is used to indicate the severity of each message being logged.


Tap on time to skip ahead


Hello everyone. Let’s talk message severities. You may have noticed in some of the previous lessons, I had logged through a logger component some messages and you may have noticed that sometimes I specify debug. Sometimes I specified error. And I want to talk about that that a little. Message severity is also known as logging level. So let’s take a little bit deeper dive into this.


If we look at the out put from our log messages, you may notice that the messages have a prefix, the actual text of the message. So you may see WARNING, you may see DEBUG. Let’s see here. So if I look for all DEBUG messages, you can see that there’s 2,740 DEBUG messages. Let’s take a look at WARNING. There are 10 WARNING’s. And let’s try some others that I’m aware of, INFO. There are only 5 INFO’s. So the output of the log message contains this indicator as to what the severity of the message is. And let’s look at what the various severities actually are.


To do that, I want take you back to one of the oldest logging mechanisms ever known to man. It’s called syslog in it a Linux or Unix logging mechanism. If we look at the man page, which is the documentation for the syslog routine, we’ll notice here that even way back in the in the 1970’s or whenever this was first introduced, there was this notion of logging level. So here we have an emergency level, system is unusable. This is an alert, action must be taken immediately. Critical, critical conditions, error, warning, notice, info, debug etc. So even way back then, log messages came along with this notion of a log level to indicate the severity of the message being logged.


If we fast forward a little bit into the 1990’s, Log4J, which was a Java-based logging framework. The same notion is included. You’ll notice here that there are log levels; OFF, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE, etc. Log4J is a very popular logging framework for Java and even it has logging levels.


Well we’re not writing unix or Linux code. We’re also not writing Java code. We’re writing Swift code for iOS. Let’s take a look at traditionally one of the most popular logging frameworks in iOS called CocoaLumberjack. You’ll notice here that even in this library, there’s a notion of log level. We have OFF, ERROR, WARNING, INFO, DEBUG, VERBOSE and ALL. While they’re not identical to syslog or log4J, they are essentially the same.


And finally we’re going to take a look at it Swifty Beaver, which is one of the newer logging frameworks for Swift that you may find useful. Here as well, there’s this notion of log level. It doesn’t identically match any of the others, but we have a VERBOSE, DEBUG, INFO, WARNING and ERROR.


The important thing to note is that the logging level is used for you to encode your messages with the appropriate criticality or severity. Typically starting with the least severe or critical and working your way up to the most severe or critical. 

Additional Info

Register to get access to additional resources and info.