Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to a custom function using a custom appender #330

Open
adrien3d opened this issue Dec 8, 2023 · 0 comments
Open

Redirect to a custom function using a custom appender #330

adrien3d opened this issue Dec 8, 2023 · 0 comments

Comments

@adrien3d
Copy link

adrien3d commented Dec 8, 2023

I have already a log4rs configuration that outputs logs to console and to a file, but I want to be able to use a custom function that will output to a websocket.
My configuration:

    log::set_logger(&crate::logger::LOGGER);
    let console_logs = console::ConsoleAppender::builder()
        .encoder(Box::new(pattern::PatternEncoder::new(
            "{h({d(%Y-%m-%dT%H:%M:%S%.3f)(utc)} [{l}]\t {m}{n})}",
        )))
        .build();
    // Build file appender to log console outputs in a file
    let log_file = file::FileAppender::builder()
        .encoder(Box::new(pattern::PatternEncoder::new(
            "{h({d(%Y-%m-%dT%H:%M:%S%.3f)(utc)} [{l}]\t {m}{n})}",
        )))
        .build(my_path)?;

    // Log at the argument specified level with Info as the default level for the logger.
    let config = Config::builder()
        .appender(Appender::builder().build("file_logs", Box::new(log_file)))
        .appender(Appender::builder().build("console_logs", Box::new(console_logs)))
        .logger(Logger::builder().build(
            "hyper",
            log_level.min(if reduce_db_logs {
                LevelFilter::Info
            } else {
                log_level
            }),
        ))
        .build(
            Root::builder()
                .appender("file_logs")
                .appender("console_logs")
                .build(log_level),
        )?;

    log4rs::init_config(config)?;

I have tried to build a custom logger:

pub static LOGGER: MyLogger = MyLogger ;
pub struct MyLogger;
impl log::Log for MyLogger {
    fn enabled(&self, _: &log::Metadata) -> bool {
        true
    }

    fn log(&self, record: &log::Record) {
        //let now = crate::ntp::Ntp::current_time(&self)
        if self.enabled(record.metadata()) {
            println!("My logger: {} - {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

But I am struggling to be able to call the custom logger with log4rs or log, it does not even compiles:

env_logger::init should not be called after logger initialized: SetLoggerError(())

Any idea on how to simply being able to access log level and content to call another function, while keeping logging on console and on file?

@adrien3d adrien3d changed the title Redirect to a custom function Redirect to a custom function using a custom appender Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant