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

Custom formatter for syslog not working? #168

Closed
krims0n32 opened this issue Jul 25, 2024 · 5 comments
Closed

Custom formatter for syslog not working? #168

krims0n32 opened this issue Jul 25, 2024 · 5 comments

Comments

@krims0n32
Copy link

krims0n32 commented Jul 25, 2024

Hello, thanks for your work on this library. I have a question about formatting when using a Syslog writer, it seems my formatting method is not being used. Considering the code below, my custom formatter is applied to the console output, but not to the syslog output. The predefined formatters also do not seem to work. Although the syslog entry do contain timestamp, hostname and such, I need the message itself to be more detailed. Am I doing something wrong here?

pub fn my_own_format(
    w: &mut dyn std::io::Write,
    now: &mut DeferredNow,
    record: &Record,
) -> Result<(), std::io::Error> {
    let level = record.level();
    write!(
        w,
        "{} [Thread {}] Severity {}, Message: {}",
        now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK),
        thread::current().name().unwrap_or("<unnamed>"),
        record.level(),
        &record.args()
    )
}

pub fn setup() {
    let syslog_logger = Syslog::try_udp(
        SocketAddr::from(([0, 0, 0, 0], 0)),
        format!(
            "{}:{}",
            env::var("GRAYLOG_SERVICE").expect("GRAYLOG_SERVICE is not set."),
            env::var("GRAYLOG_SYSLOG_PORT").expect("GRAYLOG_SYSLOG_PORT is not set."),
        )
        .to_socket_addrs()
        .expect("Invalid graylog service and/or port specified.")
        .next()
        .expect("Invalid graylog service and/or port specified."),
    )
    .expect("Unable to connect to graylog.");

    let syslog_writer = SyslogWriter::try_new(
        SyslogFacility::LocalUse0,
        None,
        LevelFilter::Debug,
        "proxy".into(),
        syslog_logger,
    )
    .expect("Error setting up syslog writer.");

    Logger::try_with_env_or_str("debug")
        .expect("LogSpecification String has errors")
        // .print_message()
        .log_to_writer(syslog_writer)
        .format_for_writer(my_own_format)
        .duplicate_to_stderr(flexi_logger::Duplicate::All)
        .format_for_stderr(my_own_format)
        .start()
        .unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
}
@emabee
Copy link
Owner

emabee commented Jul 25, 2024

SyslogWriter comes with two factory methods: try_new() and try_new_bsd(). Both use different, but hardcoded formats, corresponding to RFC5424 or RFC3164, respectively.

I'm really not an expert for syslog. Do you think it makes sense to allow a free customising of the format?

@krims0n32
Copy link
Author

I have tried both methods, but I think it would be nice to be able to format the log message itself using a formatter. That way you can add things like thread id, target, etc to it, similar to logging to a file or console.

@emabee
Copy link
Owner

emabee commented Jul 25, 2024

That makes sense, I see...

@emabee
Copy link
Owner

emabee commented Aug 25, 2024

Realized with 0.29.0

@emabee emabee closed this as completed Aug 25, 2024
@krims0n32
Copy link
Author

Thanks, works perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants