Under the covers, Godot uses GD.Print() to send output to its Output tab and Console.WriteLine() is just redirected to nowhere. That quickly made it a pain to do logging because sometimes you’d get to see it, and other times you wouldn’t. Worse, no one wants to change their logging lines back and forth between two different approaches. For that reason, pretty early on in this project I created a simple logging system. Its primary purpose was to be a logging wrapper that can be used safely whether running under the Godot Engine or not. I call it Flogger which is short for Fracterebus Logger, but also because I found it funny.
This system worked well enough and was accessible from anywhere in the code very easily. For simple things everything worked just fine. Switching its mode between Godot mode and Console mode was a single command that could be issued from anywhere in the code. The problem is that because it’s a global thing those mode switches tend to pile up in the code and can goof things up. In the past I’ve written considerably more capable logging systems, but they were written in Java instead of C# so it wasn’t just a chunk of code to drop in. That means I started from scratch instead. It also meant I was missing a bunch of features that I used to have because I didn’t immediately want to recreate all the things.
It’s probably easier to represent today’s work as a couple of lists.
Before:
- Part of the Fracterebus.core namespace.
- Could be set to send logs to Godot’s output panel or to the console.
- Could log to standard or error output streams.
- Could eat logs so they don’t go anywhere.
After:
- The stuff in the Before list still works.
- Moved to the Fracterebus.log namespace with its supporting things.
- Automatically detects whether the code is running under Godot and sends logs to the right place by itself by default.
- Can redirect log messages to a file instead of any console or output panel.
- Supports 6 verbosity levels. System can be set to a level and will eat messages that are for the wrong verbosity level.
- Log messages automatically include their verbosity level and a timestamp without a date.
- Some things are optional:
- The verbosity level marker can be disabled.
- The timestamp can also include the date, or be disabled entirely.
There is still a bit of work to do to assign verbosity levels to all the places I log output. It defaults to the “unknown” verbosity level which is typically hidden except in the wildest of logging scenarios as it’s the most verbose mode.
