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

add support for syslog 6 to fern #91

Merged
merged 2 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ script:
- cargo test --verbose --features=colored
- cargo test --verbose --features=syslog-3
- cargo test --verbose --features=syslog-4
- cargo test --verbose --features=syslog-6
- cargo test --verbose --features=reopen-03
- cargo test --verbose --features=meta-logging-in-format
- cargo test --verbose --all-features
- cargo run --example cmd-program
- cargo run --example cmd-program -- --verbose
- cargo run --example colored --features colored
- cargo run --example pretty-colored --features colored
- cargo run --example syslog --features syslog-4
- cargo run --example syslog --features syslog-6
- cargo run --example syslog4 --features syslog-4
- cargo run --example syslog3 --features syslog-3
- cargo run --example date-based-file-log --features date-based
# we don't exactly have a good test suite for DateBased right now, so let's at least do this:
Expand Down
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@ maintenance = { status = "passively-maintained" }
[dependencies]
log = { version = "0.4", features = ["std"] }
colored = { version = "1.5", optional = true }
chrono = { version = "0.4", optional = true }
chrono = { version = "0.4", default-features = false, features = ["std", "clock"], optional = true }

[target."cfg(not(windows))".dependencies]
syslog3 = { version = "3", package = "syslog", optional = true }
syslog4 = { version = "4", package = "syslog", optional = true }
syslog6 = { version = "6", package = "syslog", optional = true }
reopen = { version = "^0.3", optional = true }
libc = { version = "0.2.58", optional = true }

[features]
syslog-3 = ["syslog3"]
syslog-4 = ["syslog4"]
syslog-6 = ["syslog6"]
reopen-03 = ["reopen", "libc"]
meta-logging-in-format = []
date-based = ["chrono"]

[dev-dependencies]
tempdir = "0.3"
clap = "2.22"
chrono = "0.4"
chrono = { version = "0.4", default-features = false, features = ["std", "clock"]}

[[example]]
name = "cmd-program"
Expand All @@ -63,6 +65,10 @@ required-features = ["colored"]

[[example]]
name = "syslog"
required-features = ["syslog-6"]

[[example]]
name = "syslog4"
required-features = ["syslog-4"]

[[example]]
Expand Down
2 changes: 1 addition & 1 deletion examples/syslog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(windows))]
// This is necessary because `fern` depends on both version 3 and 4.
use syslog4 as syslog;
use syslog6 as syslog;

use log::{debug, info, warn};

Expand Down
53 changes: 53 additions & 0 deletions examples/syslog4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[cfg(not(windows))]
// This is necessary because `fern` depends on both version 3 and 4.
use syslog4 as syslog;

use log::{debug, info, warn};

#[cfg(not(windows))]
fn setup_logging() -> Result<(), Box<dyn std::error::Error>> {
let syslog_fmt = syslog::Formatter3164 {
facility: syslog::Facility::LOG_USER,
hostname: None,
process: "fern-syslog-example".into(),
pid: 0,
};
fern::Dispatch::new()
// by default only accept warning messages so as not to spam
.level(log::LevelFilter::Warn)
// but accept Info if we explicitly mention it
.level_for("explicit-syslog", log::LevelFilter::Info)
.chain(syslog::unix(syslog_fmt)?)
.apply()?;

Ok(())
}

#[cfg(not(windows))]
fn main() {
setup_logging().expect("failed to initialize logging.");

// None of this will be shown in the syslog:
for i in 0..5 {
info!("executing section: {}", i);

debug!("section {} 1/4 complete.", i);

debug!("section {} 1/2 complete.", i);

debug!("section {} 3/4 complete.", i);

info!("section {} completed!", i);
}

// these two *will* show.

info!(target: "explicit-syslog", "hello to the syslog! this is rust.");

warn!("AHHH something's on fire.");
}

#[cfg(windows)]
fn main() {
panic!("this example does not work on Windows.");
}
89 changes: 88 additions & 1 deletion src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
#[cfg(feature = "date-based")]
use std::path::{Path, PathBuf};

#[cfg(all(not(windows), feature = "syslog-4"))]
#[cfg(all(not(windows), any(feature = "syslog-4", feature = "syslog-6")))]
use std::collections::HashMap;

use log::Log;
Expand All @@ -21,6 +21,9 @@ use crate::log_impl::DateBasedState;
#[cfg(all(not(windows), feature = "syslog-4"))]
use crate::{Syslog4Rfc3164Logger, Syslog4Rfc5424Logger};

#[cfg(all(not(windows), feature = "syslog-6"))]
use crate::{Syslog6Rfc3164Logger, Syslog6Rfc5424Logger};

/// The base dispatch logger.
///
/// This allows for formatting log records, limiting what records can be passed
Expand Down Expand Up @@ -478,6 +481,21 @@ impl Dispatch {
transform,
}))
}
#[cfg(all(not(windows), feature = "syslog-6"))]
OutputInner::Syslog6Rfc3164(logger) => {
max_child_level = log::LevelFilter::Trace;
Some(log_impl::Output::Syslog6Rfc3164(log_impl::Syslog6Rfc3164 {
inner: Mutex::new(logger),
}))
}
#[cfg(all(not(windows), feature = "syslog-6"))]
OutputInner::Syslog6Rfc5424 { logger, transform } => {
max_child_level = log::LevelFilter::Trace;
Some(log_impl::Output::Syslog6Rfc5424(log_impl::Syslog6Rfc5424 {
inner: Mutex::new(logger),
transform,
}))
}
OutputInner::Panic => {
max_child_level = log::LevelFilter::Trace;
Some(log_impl::Output::Panic(log_impl::Panic))
Expand Down Expand Up @@ -669,6 +687,18 @@ enum OutputInner {
+ Send,
>,
},
#[cfg(all(not(windows), feature = "syslog-6"))]
Syslog6Rfc3164(Syslog6Rfc3164Logger),
/// Sends all messages through the transform then passes to the syslog.
#[cfg(all(not(windows), feature = "syslog-6"))]
Syslog6Rfc5424 {
logger: Syslog6Rfc5424Logger,
transform: Box<
dyn Fn(&log::Record) -> (u32, HashMap<String, HashMap<String, String>>, String)
+ Sync
+ Send,
>,
},
/// Panics with messages text for all messages.
Panic,
/// File logger with custom date and timestamp suffix in file name.
Expand Down Expand Up @@ -877,6 +907,25 @@ impl From<Syslog4Rfc3164Logger> for Output {
}
}

#[cfg(all(not(windows), feature = "syslog-6"))]
impl From<Syslog6Rfc3164Logger> for Output {
/// Creates an output logger which writes all messages to the given syslog.
///
/// Log levels are translated trace => debug, debug => debug, info =>
/// informational, warn => warning, and error => error.
///
/// Note that due to https://github.com/Geal/rust-syslog/issues/41,
/// logging to this backend requires one allocation per log call.
///
/// This is for RFC 3164 loggers. To use an RFC 5424 logger, use the
/// [`Output::syslog_5424`] helper method.
///
/// This requires the `"syslog-4"` feature.
fn from(log: Syslog6Rfc3164Logger) -> Self {
Output(OutputInner::Syslog6Rfc3164(log))
}
}

impl From<Panic> for Output {
/// Creates an output logger which will panic with message text for all
/// messages.
Expand Down Expand Up @@ -1114,6 +1163,34 @@ impl Output {
})
}

/// Returns a logger which logs into an RFC5424 syslog (using syslog version 6)
///
/// This method takes an additional transform method to turn the log data
/// into RFC5424 data.
///
/// I've honestly got no clue what the expected keys and values are for
/// this kind of logging, so I'm just going to link [the rfc] instead.
///
/// If you're an expert on syslog logging and would like to contribute
/// an example to put here, it would be gladly accepted!
///
/// This requires the `"syslog-6"` feature.
///
/// [the rfc]: https://tools.ietf.org/html/rfc5424
#[cfg(all(not(windows), feature = "syslog-6"))]
pub fn syslog6_5424<F>(logger: Syslog6Rfc5424Logger, transform: F) -> Self
where
F: Fn(&log::Record) -> (u32, HashMap<String, HashMap<String, String>>, String)
+ Sync
+ Send
+ 'static,
{
Output(OutputInner::Syslog6Rfc5424 {
logger,
transform: Box::new(transform),
})
}

/// Returns a logger which simply calls the given function with each
/// message.
///
Expand Down Expand Up @@ -1253,6 +1330,16 @@ impl fmt::Debug for OutputInner {
.debug_tuple("Output::Syslog4Rfc5424")
.field(&"<unprintable syslog::Logger>")
.finish(),
#[cfg(all(not(windows), feature = "syslog-6"))]
OutputInner::Syslog6Rfc3164 { .. } => f
.debug_tuple("Output::Syslog6Rfc3164")
.field(&"<unprintable syslog::Logger>")
.finish(),
#[cfg(all(not(windows), feature = "syslog-6"))]
OutputInner::Syslog6Rfc5424 { .. } => f
.debug_tuple("Output::Syslog6Rfc5424")
.field(&"<unprintable syslog::Logger>")
.finish(),
OutputInner::Dispatch(ref dispatch) => {
f.debug_tuple("Output::Dispatch").field(dispatch).finish()
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ type Syslog4Rfc5424Logger = syslog4::Logger<
syslog4::Formatter5424,
>;

#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6Rfc3164Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::Formatter3164>;

#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6Rfc5424Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::Formatter5424>;

/// Convenience method for opening a log file with common options.
///
/// Equivalent to:
Expand Down
Loading