Crate spirit_log[][src]

A spirit configuration helper for logging.

Set of configuration fragments and config helpers for the spirit configuration framework that configures logging for the log crate and updates it as the configuration changes (even at runtime).

Currently, it also allows asking for log output on the command line and multiple logging destinations with different log levels set.

It assumes the application doesn't set the global logger (this crate sets it on its own).

For details about added options, see the Opts and Cfg configuration fragments.

Startup

The logging is set in multiple steps:

  • As soon as the config helper 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.

Integration with other loggers

If you need something specific (for example sentry), you can provide functions to add additional loggers in parallel with the ones created in this crate. The crate will integrate them all together.

Performance warning

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

Planned features

These pieces are planned some time in future, but haven't happened yet.

  • Reconnecting to the remote server if a TCP connection is lost.
  • Log file rotation.
  • Setting of logging format (choosing either local or UTC, the format argument or JSON).
  • Colors on stdout/stderr.
  • Async and buffered logging and ability to drop log messages when logging doesn't keep up.

Examples

#[macro_use]
extern crate log;
extern crate spirit;
extern crate spirit_log;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate structopt;

use spirit::Spirit;
use spirit_log::{Cfg as LogCfg, Opts as LogOpts};

#[derive(Clone, Debug, StructOpt)]
struct Opts {
    #[structopt(flatten)]
    log: LogOpts,
}

impl Opts {
    fn log(&self) -> LogOpts {
        self.log.clone()
    }
}

#[derive(Clone, Debug, Default, Deserialize)]
struct Cfg {
    #[serde(flatten)]
    log: LogCfg,
}

impl Cfg {
    fn log(&self) -> LogCfg {
        self.log.clone()
    }
}

fn main() {
    Spirit::<Opts, Cfg>::new()
        .config_helper(Cfg::log, Opts::log, "logging")
        .run(|_spirit| {
            info!("Hello world");
            Ok(())
        });
}

Structs

Cfg

A configuration fragment to set up logging.

Extras

Description of extra configuration for logging.

Opts

A fragment for command line options.

SyslogError

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