Logging

print("here") never gets old...

Using print() statements are okay for debugging, but it quickly leads to a mess of log messages which makes debugging increasing difficult as the here!!! that you're searching for gets buried by a mountain of other similarly-ambiguous messages (we've all been there). As you slowly move to make your bot production-ready, simply using print()s for logging isn't sustainable. Many logging packages were created precisely to solve this problem, providing semantic logging and logging to various transports (not just your console).

Logging Transports

You might be used to viewing logs streamed into your console, but on a server, that might be difficult or even impossible. Besides, you can't be constantly viewing the logs as they stream in from your server to debug a problem reported by one of your customers xD.

Logging transports are the solution to that, allowing the logs to be persisted to disk or even streamed to various logging services for analysis. SwiftLog provides the ability to log to various "backends" or transports, of which a few notable implementations are listed in this table.

SwiftLog

A logging solution for Swift built by Apple themselves, it's a perfect solution for logging in Swift. Being both lightweight and providing a wide variety of logging transports, it's the logging solution I'd recommend for your Swift Discord bot. An example of a "kitchen sink" Discord bot built with DiscordKit that also uses SwiftLog is available in this repository.

Semantic Logging

Using a dedicated logging solution is definitely an improvement over littering print()s everywhere, but logs that do not follow a specific style are also difficult to analyse and interpret with utilities. I'd strongly recommend following semantic logging guidelines for the cleanest logs.

What About DiscordKit?

Well, I'd be quite the hypocrite to recommend these practices and not follow them myself. Thankfully, DiscordKit itself fully uses SwiftLog for all logging and follows Semantic Logging as far as possible! You might notice that logging messages from DiscordKit look a little different, now you know why!

Are DiscordKit's logs too verbose? At the moment, DiscordKit is set to trace if the binary is built in debug mode, which displays log messages of all levels. However, the log level will be set to info if the binary is built in release mode, which filters out all log messages below the info level. That significantly decreases the verbosity of logs.

Last updated