sile / sloggers

A Rust library which provides frequently used slog loggers and convenient functions
MIT License
37 stars 18 forks source link

Support for record format #22

Closed rohitjoshi closed 9 months ago

rohitjoshi commented 5 years ago

How do I format a record as Timestamp|Level|Module:Line|Msg e.g. 2019-03-14T07:13:58.740|INFO|abc::network_server:97|Starting connection handler child loop

sile commented 5 years ago

sloggers only supports the formats defined by slog_term::CompactFormat and slog_term::FullFormat. To keep this crate simple, sloggers probably does not provide custom message format functionality. If you need it (or any other elaborate features), it is recommended to use slog directly.

rohitjoshi commented 5 years ago

Thanks. Any suggestions on how to override? I have been using slog with implementing log trait for a drain but missing some of the common capabilities of sloggers. E.g. log rotate, keep num of files, compression etc

sile commented 5 years ago

I tried adding BuildWithCustomFormat for this purpose (in custom-format branch). It can be used as follows:

#[macro_use]
extern crate slog;
extern crate sloggers;

use sloggers::BuildWithCustomFormat; // Added
use sloggers::terminal::{TerminalLoggerBuilder, Destination};

let mut builder = TerminalLoggerBuilder::new();
builder.destination(Destination::Stderr);

let logger = builder.build_with_custom_format(|decorator| {
    let drain  = ...;  // Create your own drain that uses `decorator` (slog_term::Decorator).
    Ok(drain)
})).unwrap();
info!(logger, "Hello World!");

Is it sufficient for you?

rohitjoshi commented 5 years ago

Thanks, I will give a try. Appreciate your help.