Skip to content

Commit

Permalink
Merge pull request #142 from BlackDex/add-syslog-7-support
Browse files Browse the repository at this point in the history
Add syslog v7 support
  • Loading branch information
daboross authored Oct 21, 2024
2 parents 120231d + 0052cd7 commit 3e775cc
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
with:
command: test
args: --features=syslog-6
- uses: actions-rs/cargo@v1
with:
command: test
args: --features=syslog-7
- uses: actions-rs/cargo@v1
with:
command: test
Expand Down Expand Up @@ -103,6 +107,7 @@ jobs:
- run: cargo run --example syslog3 --features syslog-3
- run: cargo run --example syslog4 --features syslog-4
- run: cargo run --example syslog --features syslog-6
- run: cargo run --example syslog7 --features syslog-7
msrv:
name: MSRV Compatability - fern
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -169,6 +174,22 @@ jobs:
toolchain: 1.67.0
override: true
- run: cargo build --features syslog-6
msrv_syslog_7:
name: MSRV Compatability - fern/syslog-7
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
override: true
- run: cargo build --features syslog-7
fmt_and_clippy:
name: Optional Lints
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ nb-configuration.xml
*.iws
*.sublime-project
*.sublime-workspace
.vscode
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ chrono = { version = "0.4", default-features = false, features = ["std", "clock"
syslog3 = { version = "3", package = "syslog", optional = true }
syslog4 = { version = "4", package = "syslog", optional = true }
syslog6 = { version = "6", package = "syslog", optional = true }
syslog7 = { version = "7", package = "syslog", optional = true }
reopen1 = { version = "~1", package = "reopen", features = ["signals"], optional = true }
reopen03 = { version = "^0.3", package = "reopen", optional = true }
libc = { version = "0.2.58", optional = true }
Expand All @@ -35,6 +36,7 @@ libc = { version = "0.2.58", optional = true }
syslog-3 = ["syslog3"]
syslog-4 = ["syslog4"]
syslog-6 = ["syslog6"]
syslog-7 = ["syslog7"]
reopen-03 = ["reopen03", "libc"]
reopen-1 = ["reopen1", "libc"]
meta-logging-in-format = []
Expand All @@ -60,6 +62,10 @@ required-features = ["colored"]
name = "pretty-colored"
required-features = ["colored"]

[[example]]
name = "syslog7"
required-features = ["syslog-7"]

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

#[cfg(not(windows))]
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.");
}
88 changes: 87 additions & 1 deletion src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use std::path::{Path, PathBuf};
#[cfg(all(not(windows), any(feature = "syslog-4", feature = "syslog-6")))]
use std::collections::HashMap;

#[cfg(all(not(windows), feature = "syslog-7"))]
use std::collections::BTreeMap;

use log::Log;

use crate::{log_impl, Filter, FormatCallback, Formatter};
Expand All @@ -24,6 +27,9 @@ use crate::{Syslog4Rfc3164Logger, Syslog4Rfc5424Logger, Syslog4TransformFn};
#[cfg(all(not(windows), feature = "syslog-6"))]
use crate::{Syslog6Rfc3164Logger, Syslog6Rfc5424Logger, Syslog6TransformFn};

#[cfg(all(not(windows), feature = "syslog-7"))]
use crate::{Syslog7Rfc3164Logger, Syslog7Rfc5424Logger, Syslog7TransformFn};

/// The base dispatch logger.
///
/// This allows for formatting log records, limiting what records can be passed
Expand Down Expand Up @@ -504,6 +510,21 @@ impl Dispatch {
transform,
}))
}
#[cfg(all(not(windows), feature = "syslog-7"))]
OutputInner::Syslog7Rfc3164(logger) => {
max_child_level = log::LevelFilter::Trace;
Some(log_impl::Output::Syslog7Rfc3164(log_impl::Syslog7Rfc3164 {
inner: Mutex::new(logger),
}))
}
#[cfg(all(not(windows), feature = "syslog-7"))]
OutputInner::Syslog7Rfc5424 { logger, transform } => {
max_child_level = log::LevelFilter::Trace;
Some(log_impl::Output::Syslog7Rfc5424(log_impl::Syslog7Rfc5424 {
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 @@ -706,6 +727,14 @@ enum OutputInner {
logger: Syslog6Rfc5424Logger,
transform: Box<Syslog6TransformFn>,
},
#[cfg(all(not(windows), feature = "syslog-7"))]
Syslog7Rfc3164(Syslog7Rfc3164Logger),
/// Sends all messages through the transform then passes to the syslog.
#[cfg(all(not(windows), feature = "syslog-7"))]
Syslog7Rfc5424 {
logger: Syslog7Rfc5424Logger,
transform: Box<Syslog7TransformFn>,
},
/// Panics with messages text for all messages.
Panic,
/// File logger with custom date and timestamp suffix in file name.
Expand Down Expand Up @@ -939,12 +968,31 @@ impl From<Syslog6Rfc3164Logger> for Output {
/// 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.
/// This requires the `"syslog-6"` feature.
fn from(log: Syslog6Rfc3164Logger) -> Self {
Output(OutputInner::Syslog6Rfc3164(log))
}
}

#[cfg(all(not(windows), feature = "syslog-7"))]
impl From<Syslog7Rfc3164Logger> 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-7"` feature.
fn from(log: Syslog7Rfc3164Logger) -> Self {
Output(OutputInner::Syslog7Rfc3164(log))
}
}

impl From<Panic> for Output {
/// Creates an output logger which will panic with message text for all
/// messages.
Expand Down Expand Up @@ -1249,6 +1297,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-7"` feature.
///
/// [the rfc]: https://tools.ietf.org/html/rfc5424
#[cfg(all(not(windows), feature = "syslog-7"))]
pub fn syslog7_5424<F>(logger: Syslog7Rfc5424Logger, transform: F) -> Self
where
F: Fn(&log::Record) -> (u32, BTreeMap<String, BTreeMap<String, String>>, String)
+ Sync
+ Send
+ 'static,
{
Output(OutputInner::Syslog7Rfc5424 {
logger,
transform: Box::new(transform),
})
}

/// Returns a logger which simply calls the given function with each
/// message.
///
Expand Down Expand Up @@ -1407,6 +1483,16 @@ impl fmt::Debug for OutputInner {
.debug_tuple("Output::Syslog6Rfc5424")
.field(&"<unprintable syslog::Logger>")
.finish(),
#[cfg(all(not(windows), feature = "syslog-7"))]
OutputInner::Syslog7Rfc3164 { .. } => f
.debug_tuple("Output::Syslog7Rfc3164")
.field(&"<unprintable syslog::Logger>")
.finish(),
#[cfg(all(not(windows), feature = "syslog-7"))]
OutputInner::Syslog7Rfc5424 { .. } => f
.debug_tuple("Output::Syslog7Rfc5424")
.field(&"<unprintable syslog::Logger>")
.finish(),
OutputInner::Dispatch(ref dispatch) => {
f.debug_tuple("Output::Dispatch").field(dispatch).finish()
}
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ use std::{
#[cfg(all(not(windows), any(feature = "syslog-4", feature = "syslog-6")))]
use std::collections::HashMap;

#[cfg(all(not(windows), feature = "syslog-7"))]
use std::collections::BTreeMap;

pub use crate::{
builders::{Dispatch, Output, Panic},
errors::InitError,
Expand Down Expand Up @@ -291,6 +294,12 @@ type Syslog6Rfc3164Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::For
#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6Rfc5424Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::Formatter5424>;

#[cfg(all(not(windows), feature = "syslog-7"))]
type Syslog7Rfc3164Logger = syslog7::Logger<syslog7::LoggerBackend, syslog7::Formatter3164>;

#[cfg(all(not(windows), feature = "syslog-7"))]
type Syslog7Rfc5424Logger = syslog7::Logger<syslog7::LoggerBackend, syslog7::Formatter5424>;

#[cfg(all(not(windows), feature = "syslog-4"))]
type Syslog4TransformFn =
dyn Fn(&log::Record) -> (i32, HashMap<String, HashMap<String, String>>, String) + Send + Sync;
Expand All @@ -299,6 +308,10 @@ type Syslog4TransformFn =
type Syslog6TransformFn =
dyn Fn(&log::Record) -> (u32, HashMap<String, HashMap<String, String>>, String) + Send + Sync;

#[cfg(all(not(windows), feature = "syslog-7"))]
type Syslog7TransformFn =
dyn Fn(&log::Record) -> (u32, BTreeMap<String, BTreeMap<String, String>>, String) + Send + Sync;

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

0 comments on commit 3e775cc

Please sign in to comment.