estk / log4rs

A highly configurable logging framework for Rust
Apache License 2.0
1.01k stars 150 forks source link

Logger stops logging after some time #387

Open jonygomes06 opened 3 months ago

jonygomes06 commented 3 months ago

I have a windows service that logs every minute, and i found that after 30min to 60min it just stops logging even tough the service keeps running perfectly

bconn98 commented 3 months ago

Can you provide some details? I.e. version, configuration, etc.

jonygomes06 commented 3 months ago

I am using log4rs = "1.3.0" and this is the logger configuration:

let stdout = ConsoleAppender::builder()
    .encoder(Box::new(PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S)} | {h({({l}):5.5})} | {f}:{L} — {m}{n}")))
    .build();

let trigger = Box::new(SizeTrigger::new(2 * 1024 * 1024));

let roller = Box::new(
    FixedWindowRoller::builder()
        .base(1)
        .build(".\\Logs\\log_{}.csv", 5)
        .unwrap(),
);

let compound_policy = Box::new(CompoundPolicy::new(trigger, roller));
let file = RollingFileAppender::builder()
    .encoder(Box::new(PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S)}\t{P}:{I}\t{l}\t{f}(({L}))\t{m}{n}")))
    .build("\\Logs\\log.csv", compound_policy)
    .unwrap();

let config = Config::builder()
    .appender(Appender::builder().build("stdout", Box::new(stdout)))
    .appender(
        Appender::builder()
            .filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info)))
            .build("file", Box::new(file)))
    .logger(Logger::builder().build("app::backend::db", log::LevelFilter::Info))
    .build(Root::builder().appender("stdout").appender("file").build(log::LevelFilter::Debug))
    .unwrap();

log4rs::init_config(config).unwrap();
bconn98 commented 3 months ago

What OS (with Version) are you running? I ran the following and it seems okay, but kicking off a long run now. Have you tried adding println calls in concert with the logger or a debugger to make sure that your code is still executing?

use log::info;
use log4rs::{
    append::console::ConsoleAppender,
    append::rolling_file::policy::compound::{
        roll::fixed_window::FixedWindowRoller, trigger::size::SizeTrigger, CompoundPolicy,
    },
    append::rolling_file::RollingFileAppender,
    config::{Appender, Logger, Root},
    encode::pattern::PatternEncoder,
    filter::threshold::ThresholdFilter,
    Config,
};
use std::thread;
use std::time::Duration;

fn main() {
    let stdout = ConsoleAppender::builder()
        .encoder(Box::new(PatternEncoder::new(
            "{d(%Y-%m-%d %H:%M:%S)} | {h({({l}):5.5})} | {f}:{L} — {m}{n}",
        )))
        .build();

    let trigger = Box::new(SizeTrigger::new(2 * 1024 * 1024));

    let roller = Box::new(
        FixedWindowRoller::builder()
            .base(1)
            .build(".\\Logs\\log_{}.csv", 5)
            .unwrap(),
    );

    let compound_policy = Box::new(CompoundPolicy::new(trigger, roller));
    let file = RollingFileAppender::builder()
        .encoder(Box::new(PatternEncoder::new(
            "{d(%Y-%m-%d %H:%M:%S)}\t{P}:{I}\t{l}\t{f}(({L}))\t{m}{n}",
        )))
        .build("\\Logs\\log.csv", compound_policy)
        .unwrap();

    let config = Config::builder()
        .appender(Appender::builder().build("stdout", Box::new(stdout)))
        .appender(
            Appender::builder()
                .filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info)))
                .build("file", Box::new(file)),
        )
        .logger(Logger::builder().build("app::backend::db", log::LevelFilter::Info))
        .build(
            Root::builder()
                .appender("stdout")
                .appender("file")
                .build(log::LevelFilter::Debug),
        )
        .unwrap();

    log4rs::init_config(config).unwrap();

    let mut x = 0;
    loop {
        info!("example!! {x}");
        x += 1;
        thread::sleep(Duration::from_millis(1000));
    }
}
jonygomes06 commented 3 months ago

I ran it in a win 11 environment, i tested in 2 vm machines, and one non vm machine(laptop also win 11).

Im sure the service is still running because it is in a never ending loop that executes some code and writes some files so i constantly get feedback.

bconn98 commented 3 months ago

Hmm so 75 minutes of logging once per second, it's still running true on Rocky Linux 9. I don't have a Windows 11 system, but I can test it on Windows 10 some time next week. Can you test your app on something none Windows based?

jonygomes06 commented 3 months ago

It probably just happens in windows, so I believe there's no need for now to test in other OS, also because we can't have a windows service running in any other OS, so we cant recreate the exact same conditions.

And I believe the trick here is it to be a service in windows not a common program and it should be in build mode.

I had also the same problem with c# (because I'm migrating from c# to rust), and in these version the logger was made by me it just opened the log to write, wrote the new log and flushed it, and after a while it also had the same problem. And I ended using an existing lib called NLog.

Since I need this quickly fixed I am now using other rust lib and it appears to be all good. It's called flexi_logger.

bconn98 commented 2 months ago

Appreciate the notification then. I'll leave this open as a reminder to try testing as a windows service.