# Writing an Async Logger in Nim

DevFeed: [Writing an Async Logger in Nim](<https://devfeed.tech/articles/writing-an-async-logger-in-nim-30847.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/writing-an-async-logger-in-nim/>)

Published: 2016-01-27T23:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [Logging](<https://devfeed.tech/topics/logging.md>), [async](<https://devfeed.tech/topics/async.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [Server](<https://devfeed.tech/topics/server.md>), [client](<https://devfeed.tech/topics/client.md>)

Tags: [async](<https://devfeed.tech/tags/async.md>), [logging](<https://devfeed.tech/tags/logging.md>), [nim](<https://devfeed.tech/tags/nim.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language](<https://devfeed.tech/tags/programming-language.md>), [syslog](<https://devfeed.tech/tags/syslog.md>), [terminal](<https://devfeed.tech/tags/terminal.md>)

## AI overview

A step-by-step Nim tutorial that builds an asynchronous logger for a game server and client. It discusses configurable output to files or stdout, non-blocking disk writes, performance, logging formats, compile-time caller information, and existing Nim logger implementations.

## Source excerpt

Surprisingly I'm working on HookRace again. I might share a few interesting code snippets and thoughts in this blog along the way. I'm still going with Nim as the programming language. For an easy start let's write a logging module that can be used everywhere in the game's server as well as client. There are mostly three aspects that I care about: Can be configured to write to different files and/or stdout Writes to the disk asynchronously, preventing any blocking when the disk is overloaded Reasonable performance Follow along if you want to witness how fun it is to write code in Nim! Motivation A few logger implementations for Nim already exist: logging: A simple logger in the standard library. Configurable, but no async writing omnilog: An advanced logger with lots of features, no async writing syslog: A bit too high level for my taste and no Windows support We could adapt the logging or omnilog modules to our requirements, mainly by adding async writing. For now let's write our own simple logging module from scratch instead. If it turns out to be usable after a few iterations one might merge it with an existing module. Also, this post would be rather boring if I just ended up using an existing logger. Implementation Let's start with the simplest logger possible: proc log*(text: string) = echo text log "Hello World!" We have a procedure log that is exported (*). It takes a value text of type string and prints this text to the standard output. Super simple and can be used like this from another file (or the same one): import logger log "Hello World!" This simply prints Hello World to the terminal for now when we execute it. We want to keep using the logger in this exact format. Next step: Add a fancy logging format: import strutils, times proc log*(text: string) = echo "[$# $#]: $#" % [getDateStr(), getClockStr(), text] This uses the strutils and times modules from Nim's standard library. The % operator formats the string "[$# $#]: $#" with a date, clock and our te