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

Logger stops logging after some time #387

Open
jonygomes06 opened this issue Aug 19, 2024 · 7 comments
Open

Logger stops logging after some time #387

jonygomes06 opened this issue Aug 19, 2024 · 7 comments

Comments

@jonygomes06
Copy link

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
Copy link
Collaborator

bconn98 commented Aug 25, 2024

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

@jonygomes06
Copy link
Author

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
Copy link
Collaborator

bconn98 commented Aug 26, 2024

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
Copy link
Author

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
Copy link
Collaborator

bconn98 commented Aug 27, 2024

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
Copy link
Author

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
Copy link
Collaborator

bconn98 commented Sep 8, 2024

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

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

2 participants