godot-logger – A Rust Logger for Godot
Tags: gamedev, opensource, rust, godot
Published: 2021-10-21 14:56:06

---
title: godot-logger – A Rust Logger for Godot
published: true
date: 2021-10-20 19:30:00 UTC
tags: gamedev,opensource,rust,godot
canonical_url: https://www.jdno.dev/godot-logger-for-rust-and-godot/
---

![godot-logger – A Rust Logger for Godot](https://www.jdno.dev/content/images/2021/10/godot-logger.png)

[godot-logger](https://github.com/jdno/godot-logger) is a crate that complements the [godot-rust](https://godot-rust.github.io) bindings for Godot, and prints logs to Godot's output console using the `godot_print!` and `godot_warn!` macros. It is available as open source on [GitHub](https://github.com/jdno/godot-logger).

# Introduction

[Godot](https://godotengine.org/) is an open source game engine with a powerful editor, tools to debug and profile the game, and support for many different programming langauges. Building games with [Rust](https://www.rust-lang.org/) is possible thanks to the [godot-rust](https://godot-rust.github.io) library, which provides bindings to Godot's API as well as a few convenience methods.

One of the debugging tools inside [Godot](https://godotengine.org/) is the output console. It prints information about the execution of the game, as well as log messages that have been placed in the code.

![godot-logger – A Rust Logger for Godot](https://www.jdno.dev/content/images/2021/10/godot-output.png)

[godot-rust](https://godot-rust.github.io) provides two macros to write to the output console: `godot_print!` and `godot_warn!`. These can be called anywhere in the code, and will print the given message to the console.

The problem that I encountered while working on my game was that these macros do not capture logs from third-party Rust libraries. It is very common for libraries in Rust to use the [log](https://crates.io/crates/log) crate and its logging facade to define log statements in their code. These messages can then be printing by initializing a logger such as [env\_logger](https://crates.io/crates/env_logger) or [log4rs](https://crates.io/crates/log4rs). I wanted to see what the libraries in my project were doing, and I wanted to see the logs inside Godot's editor where I was running the game. After searching for an existing library and coming up empty, I decided to implement a logger myself that would print to Godot's output console.

# Usage

[godot-logger](https://github.com/jdno/godot-logger) is very similar to popular logging frameworks such as [env\_logger](https://crates.io/crates/env_logger) or [log4rs](https://crates.io/crates/log4rs). It integrates with the logging facade of the [log](https://crates.io/crates/log) crate, and prints log statements to Godot's output console.

The simplest way to use [godot-logger](https://github.com/jdno/godot-logger) is to add it and the [log](https://crates.io/crates/log) crate as dependencies to your project's `Cargo.toml`:

```
[dependencies]
godot-logger = "1.0"
log = "0.4
```

Then configure and initialize the logger in the same method that is passed to `godot_init!`.

```
use gdnative::prelude::*;
use godot_logger::GodotLogger;
use log::{Level, LevelFilter};

fn init(handle: InitHandle) {
    GodotLogger::builder()
        .default_log_level(Level::Info)
        .add_filter("godot_logger", LevelFilter::Debug)
        .init();
    log::debug!("Initialized the logger");
}

godot_init!(init);
```

The example shows the configuration options that [godot-logger](https://github.com/jdno/godot-logger) provides:

- The default log level of `GodotLogger` is `Level::Warn`, but this can be changed with `default_log_level`.
- Module-specific log levels that take precedence over the default log level can be set by adding filters to the logger. A filter combines a [module path](https://doc.rust-lang.org/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html) and a level filter, which makes it possible to very granularly control what gets logged.

The example above will print the following line to the console:

```
2021-10-20 20:00:00 DEBUG godot_logger Initialized the logger
```

Below is a screenshot from my own project, which runs an embedded server built with [Bevy](https://bevyengine.org) and has its code split across a few different crates ( `aomos` namespace). As you can see, `log` statements in these libraries are picked up and printed inside [Godot](https://godotengine.org/).

![godot-logger – A Rust Logger for Godot](https://www.jdno.dev/content/images/2021/10/godot-logger-output-1.png)

# Contributing

[godot-logger](https://github.com/jdno/godot-logger) is published as an open source project on [GitHub](https://github.com/jdno/godot-logger), and bug reports are always welcome.