Crate spirit_log[][src]

Expand description

A spirit fragments and helpers to configure and control logging.

The Fragments here allow to configure relatively complex logging (multiple loggers, different formats, different destinations), both from command line and the configuration. It allows runtime reloading of them.

Internally it is based on the fern crate and just adds the configuration and runtime reloading (through log-reroute).

It assumes the application doesn’t set the global logger itself. It also sets the panic hook through the log_panics crate. The with-backtrace cargo feature is propagated through.


  • background: Includes the ability to log asynchronously ‒ the writing to log files happens in a background thread and allows the rest of the application to not block on IO.
  • cfg-help: Support for configuration options help at runtime. On by default.
  • with-backtrace: The log_panics logs with backtraces. On by default.
  • syslog: Adds the support for logging into syslog.


When using the automatic management with a pipeline, this is how a startup happens:

  • As soon as the pipeline is registered, a logging on the WARN level is sent to stderr.
  • After command line arguments are parsed the stderr logging is updated to reflect that (or left on the WARN level if nothing is set by the user).
  • After configuration is loaded from the files, full logging is configured according to that.

Integration with other loggers

If you need something specific (for example sentry), you can plug in additional loggers through the pipeline ‒ the Dispatch allows adding arbitrary loggers. The Pipeline::map is a good place to do it.

Performance warning

This allows the user to create arbitrary number of loggers. Furthermore, the logging is synchronous by default and not buffered. When writing a lot of logs or sending them over the network, this could become a bottleneck.

Background logging

The background feature flag adds the ability to do the actual logging in a background thread. This allows not blocking the actual application by IO or other expensive operations.

On the other hand, if the application crashes, some logs may be lost (or, depending on setup, when the logging thread doesn’t keep up). Also, you need to flush the logger on shutdown, by using the FlushGuard.

It is done through the Background transformation.

Planned features

These pieces are planned some time in future, but haven’t happened yet (pull requests are welcome).

  • Reconnecting to the remote server if a TCP connection is lost.
  • Log file rotation.
  • Colors on stdout/stderr.

Usage without Pipelines

It is possible to use without the Pipeline, manually. However, certain care needs to be taken to initialize everything that needs to be initialized.

It is either possible to just get the Dispatch object and call apply, that however is a single-shot initialization and the logger can’t be replaced.

The helper functions init and install can be used to gain the ability to replace Dispatch loggers multiple times.


Manual single use installation

use spirit::AnyError;
use spirit::prelude::*;
use spirit_log::Cfg;

// Well, you'd get it somewhere from configuration, but…
let cfg = Cfg::default();
let logger = cfg.create("logger")?;

Manual multiple-use installation

use spirit::AnyError;
use spirit::prelude::*;
use spirit_log::Cfg;

// This part can be done multiple times.
let cfg = Cfg::default();
let logger = cfg.create("logger")?;

Automatic usage with a Pipeline, reloading and command line options

use log::info;
use serde::Deserialize;
use spirit::{Pipeline, Spirit};
use spirit::prelude::*;
use spirit_log::{Cfg as LogCfg, CfgAndOpts as LogBoth, Opts as LogOpts};
use structopt::StructOpt;

#[derive(Clone, Debug, StructOpt)]
struct Opts {
    logging: LogOpts,

impl Opts {
    fn logging(&self) -> LogOpts {

#[derive(Clone, Debug, Default, Deserialize)]
struct Cfg {
    #[serde(default, skip_serializing_if = "LogCfg::is_empty")]
    logging: LogCfg,

impl Cfg {
    fn logging(&self) -> LogCfg {

fn main() {
    Spirit::<Opts, Cfg>::new()
            Pipeline::new("logging").extract(|opts: &Opts, cfg: &Cfg| LogBoth {
                cfg: cfg.logging(),
                opts: opts.logging(),
        .run(|_spirit| {
            info!("Hello world");

The configuration could look something like this:

level = "DEBUG"
type = "file"
filename = "/tmp/example.log"
clock = "UTC"


pub use background::Background;
pub use background::FlushGuard;
pub use background::OverflowMode;


Support for logging in the background.


A configuration fragment to set up logging.

A combination of Cfg and Opts.

An Installer for the Dispatch.

A fragment for command line options.

This error can be returned when initialization of logging to syslog fails.


Initialize the global state.

Replace the current logger with the provided one.

Replace the current logger with the provided one.