sile / sloggers

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

Feature request: specify timestamp format #44

Open crusty-dave opened 3 years ago

crusty-dave commented 3 years ago

I cannot seem to find any way to do this with slog / sloggers. In log4rs I can do the following:

        // Logging to log file.
        let rolling_appender = match RollingFileAppender::builder()
            // Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
            .encoder(Box::new(PatternEncoder::new(
                "{d(%Y-%m-%dT%H:%M:%S%.3f)} {t} {l} - {m}{n}",
            )))
            .build(&self.log_path, Box::new(compound_policy))
        {
            Ok(appender) => appender,
            Err(e) => {
                return Err(format! {"rolling log builder failed, error {:?}", e});
            }
        };

I would really like to be able to store timestamps as the equivalent of the following:

time.to_rfc3339_opts(SecondsFormat::Millis, true)

I didn't see any way to do this, my apologies if I missed it.

Thanks in advance.

sile commented 3 years ago

Though sloggers currently doesn't provide this feature, slog has a way to customize the timestamp format (e.g., slog_term::CompactFormatBuilder.use_custom_timestamp), so it seems not hard to implement it in sloggers. Please let me consider whether the implementation is actually possible.

sile commented 3 years ago

After considering the above possibility, it turned out that it's difficult to allow users to specify arbitrary timestamp format in the current slog and sloggers designs. However, it could be possible to add specific predefined formats (e.g., RFC3339) to the available list. Does it satisfy your requirements?