#rocket #slog #applications #logger #fairing #system #place

rocket-slog

Provides a fairing for use in rocket.rs applications that allows the user to use a slog Logger in place of the built-in rocket.rs logging system

7 unstable releases (3 breaking)

Uses old Rust 2015

0.4.0 Dec 9, 2018
0.4.0-rc.2 Dec 1, 2018
0.3.0 Nov 28, 2018
0.2.2 Oct 11, 2018
0.1.0 Oct 11, 2018

#4 in #fairing

Download history 9/week @ 2024-07-22 7/week @ 2024-07-29 10/week @ 2024-08-05 15/week @ 2024-08-12 9/week @ 2024-08-19 10/week @ 2024-08-26 8/week @ 2024-09-02 2/week @ 2024-09-09 1/week @ 2024-09-16 16/week @ 2024-09-23 20/week @ 2024-09-30 5/week @ 2024-10-07 12/week @ 2024-10-14 12/week @ 2024-10-21 13/week @ 2024-10-28 14/week @ 2024-11-04

51 downloads per month

GPL-3.0 license

17KB
90 lines

Integrate a slog Logger with your Rocket.rs application

Documentation (master)

This is a fairing that you can attach to your rocket.rs application to enable use of a slog Logger in your handlers

Installation (for rocket v0.3)

In your Cargo.toml, put the following in the [dependencies] section:

rocket-slog = "0.3"

For pre-2018-edition crates, put the following in your crate root:

extern crate rocket_slog;

Installation (for rocket 0.4)

In your Cargo.toml, put the following in the [dependencies] section:

rocket-slog = "0.4.0-rc.2"

For pre-2018-edition crates, put the following in your crate root:

extern crate rocket_slog;

Example (for rocket 0.3)

Here is an example application that uses the rocket-slog fairing. Note that you should probably disable the builtin rocket logger unless you want the output from both logging systems.

#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;
extern crate rocket_slog;
#[macro_use(debug)] extern crate slog;
extern crate sloggers;

use std::error::Error;
use rocket::{Config};
use rocket_slog::{SyncLogger, SlogFairing};
use sloggers::{
    Build,
    terminal::{
        TerminalLoggerBuilder,
        Destination,
    },
    types::Severity,
};

#[get("/")]
fn index(logger: SyncLogger) -> &'static str {
    debug!(logger.get(), "THIS IS A CUSTOM MESSAGE");
    "hello, world"
}

fn main() -> Result<(), Box<Error>> {
    let mut builder = TerminalLoggerBuilder::new();
    builder.level(Severity::Debug);
    builder.destination(Destination::Stderr);
    let logger = builder.build()?;

    let fairing = SlogFairing::new(logger);

    let config = Config::development().unwrap();
    rocket::custom(config, false) // disables logging
        .attach(fairing)
        .mount("/", routes![index])
        .launch();
    Ok(())
}

Example (for rocket 0.4)

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use(debug)] extern crate slog;
extern crate rocket_slog;
extern crate sloggers;

use std::error::Error;
use rocket::config::{Config, Environment, LoggingLevel};
use rocket_slog::{SlogFairing, SyncLogger};
use sloggers::{
    Build,
    terminal::{
        TerminalLoggerBuilder,
        Destination,
    },
    types::Severity,
};

#[get("/")]
fn index(log: SyncLogger) -> &'static str {
    debug!(log, "some log message");
    "Hello world"
}

fn main() -> Result<(), Box<Error>> {
    let mut builder = TerminalLoggerBuilder::new();
    builder.level(Severity::Debug);
    builder.destination(Destination::Stderr);
    let logger = builder.build()?;
    let fairing = SlogFairing::new(logger);
    let config = Config::build(Environment::Development)
            .log_level(LoggingLevel::Off) // disables logging
            .finalize()
            .unwrap();
    rocket::custom(config)
        .mount("/", routes![index])
        .attach(fairing)
        .launch();
    Ok(())
}

Dependencies

~11MB
~211K SLoC