Logging with SwiftLog

This blog is intended for beginners to get started with SwiftLog. Logs provide easy and quick observability into your applications.

SwiftLog is the standard logging library developed by Apple and the sections below aim to introduce its use cases.

  1. How to log a simple message:

import Logging

let logger = Logger(label: "app-identifier")

logger.info("Hello World!")

The available log levels are .trace, .debug, .info, .notice, .warning, .error, and .critical. For example, to set the logging level to .debug:

logger.logLevel = .debug

The log messages will only output if their level is equal to or higher than the configured logging level.

  1. Logging with Metadata:

You can add additional context to your log message using metadata.

import Logging

// Create a logger
var logger = Logger(label: "com.example.MyLogger")

// Log a message with metadata
logger.info("user logged in", metadata: ["user-id": "\(user.id)"])

You can also set metadata on your logger to log your context with each message. Setting metadata on your logger can be useful to differentiate logs from various sources.

// Define metadata
var metadata = Logger.Metadata()
metadata["requestID"] = "12345"
metadata["userID"] = 42

// Set metadata on the logger 
logger.metadata = metadata
  1. Setting your logger with a custom logHandler:

A logHandlerserves as the backend implementation of the logger, determining where or how your log messages should be emitted. There are various logHandler available by SwiftLog like MultiplexLogHandler, StreamLogHandler, SwiftLogNoOpLogHandler. More here

For example: StreamLoghandler directs your log message to stderr/stdout and MultiplexLogHandler can be used to send your log messages to multiple handlers. This provides end users with great flexibility.

  1. You can set a logHandler on your logger as below:
import Logging

// Create a logger
var logger = Logger(label: "com.example.MyLogger")

// Create a StreamLogHandler for logging to standard output (console)
let streamLogHandler = StreamLogHandler.standardOutput(label: "com.example.MyLogger"

// Set a custom log handler on the logger
logger.logHandler = streamlogHandler

// Log some messages
logger.info("This is an info message")
logger.error("This is an error message")
  1. You can also set up a LogginSystem for your application

LoggingSystem is set up just once in a given program to set up the desired logging backend implementation.

import Logging

LoggingSystem.bootstrap(MyLogHandler.init) // your LogHandler might have a different bootstrapping step
var logger1 = Logger(label: "first logger")
logger1.logLevel = .debug
var logger2 = logger1
logger2.logLevel = .error

where both logger1 and logger2 have the same logHandler

I hope this gets you started with logging in Swift. Please reach out in case of any errors. Happy Developing!

References:
- https://apple.github.io/swift-log/docs/current/Logging/index.html
- https://theswiftdev.com/logging-for-beginners-in-swift/