Browse Source

Allow to send logs to syslog

ng
Rodolphe Bréard 1 month ago
parent
commit
517a87028c
Failed to extract signature
  1. 19
      Cargo.lock
  2. 1
      Cargo.toml
  3. 11
      src/log.rs
  4. 1
      src/main.rs

19
Cargo.lock

@ -7,6 +7,7 @@ name = "acmed"
version = "0.25.0-dev" version = "0.25.0-dev"
dependencies = [ dependencies = [
"clap", "clap",
"syslog-tracing",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -268,6 +269,17 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "syslog-tracing"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d349bc2df408b4bf656709a29643641cef7f1795d708f88b105c626a8f64f6e4"
dependencies = [
"libc",
"tracing-core",
"tracing-subscriber",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.8" version = "1.1.8"
@ -305,6 +317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"valuable",
] ]
[[package]] [[package]]
@ -331,6 +344,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

1
Cargo.toml

@ -23,6 +23,7 @@ ed448 = []
[dependencies] [dependencies]
clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std"] } clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std"] }
syslog-tracing = { version = "0.3.1", default-features = false }
tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread"] } tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread"] }
tracing = { version = "0.1.41", default-features = false, features = ["std"] } tracing = { version = "0.1.41", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3.19", default-features = false, features = ["ansi", "fmt", "std"] } tracing-subscriber = { version = "0.3.19", default-features = false, features = ["ansi", "fmt", "std"] }

11
src/log.rs

@ -23,8 +23,17 @@ impl Level {
} }
pub fn init(level: Level, is_syslog: bool) { pub fn init(level: Level, is_syslog: bool) {
if is_syslog {
let identity = std::ffi::CStr::from_bytes_with_nul(crate::APP_IDENTITY).unwrap();
let (options, facility) = Default::default();
let syslog = syslog_tracing::Syslog::new(identity, options, facility)
.expect("building syslog subscriber failed");
tracing_subscriber::fmt().with_writer(syslog).init();
} else {
let subscriber = FmtSubscriber::builder() let subscriber = FmtSubscriber::builder()
.with_max_level(level.tracing()) .with_max_level(level.tracing())
.finish(); .finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
}
} }

1
src/main.rs

@ -3,6 +3,7 @@ mod log;
use clap::Parser; use clap::Parser;
pub const APP_IDENTITY: &[u8] = b"acmed\0";
pub const APP_THREAD_NAME: &str = "acmed-runtime"; pub const APP_THREAD_NAME: &str = "acmed-runtime";
pub const DEFAULT_CONFIG_PATH: &str = "/etc/acmed/acmed.toml"; pub const DEFAULT_CONFIG_PATH: &str = "/etc/acmed/acmed.toml";
pub const DEFAULT_LOG_LEVEL: log::Level = log::Level::Warn; pub const DEFAULT_LOG_LEVEL: log::Level = log::Level::Warn;

Loading…
Cancel
Save