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

Syslog appender #26

Closed
wants to merge 20 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "A highly configurable multi-output logging implementation for the
license = "MIT/Apache-2.0"
repository = "https://github.com/sfackler/log4rs"
documentation = "https://sfackler.github.io/log4rs/doc/v0.4.8/log4rs"
keywords = ["logs", "logging", "syslog"]

[features]
yaml = ["serde_yaml"]
Expand All @@ -24,6 +25,7 @@ typemap = "0.3"
serde_json = { version = "0.7", optional = true }
serde_yaml = { version = "0.2", optional = true }
toml = { version = "0.1.28", optional = true, default_features = false, features = ["serde"] }
time = "0.1"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ appenders:
path: "log/requests.log"
encoder:
pattern: "{d} - {m}{n}"
audit:
kind: syslog
protocol: udp
address: logserver:514
format:
kind: rfc5424
hostname: appserver
app_name: MyService

root:
level: warn
appenders:
Expand All @@ -29,6 +38,7 @@ loggers:
level: info
appenders:
- requests
- audit
additive: false
```

Expand Down
3 changes: 2 additions & 1 deletion src/append/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Appenders
//! Appenders.

use std::error::Error;
use std::fmt;
use log::LogRecord;

pub mod file;
pub mod console;
pub mod syslog;

/// A trait implemented by log4rs appenders.
///
Expand Down
126 changes: 126 additions & 0 deletions src/append/syslog/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//! Common syslog constants.

use log::LogLevel;
use std::io;

/// The syslog `NILVALUE` constant.
pub const NILVALUE: &'static str = "-";

/// Facilities according to RFC 5424
#[derive(Debug)]
#[derive(Copy, Clone)]
pub enum Facility {
/// Kernel messages
KERN = 0,
/// User-level messages
USER = 1 << 3,
/// Mail system
MAIL = 2 << 3,
/// System daemons
DAEMON = 3 << 3,
/// Security/authorization messages
AUTH = 4 << 3,
/// Messages generated internally by syslogd
SYSLOG = 5 << 3,
/// Line printer subsystem
LPR = 6 << 3,
/// Network news subsystem
NEWS = 7 << 3,
/// UUCP subsystem
UUCP = 8 << 3,
/// Clock daemon
CRON = 9 << 3,
/// Security/authorization messages
AUTHPRIV = 10 << 3,
/// FTP daemon
FTP = 11 << 3,
/// NTP subsystem
NTP = 12 << 3,
/// Log audit
LOGAU = 13 << 3,
/// Log alert
LOGALT = 14 << 3,
/// Clock daemon (note 2)
CRON2 = 15 << 3,
/// Local use 0 (local0)
LOCAL0 = 16 << 3,
/// Local use 1 (local1)
LOCAL1 = 17 << 3,
/// Local use 2 (local2)
LOCAL2 = 18 << 3,
/// Local use 3 (local3)
LOCAL3 = 19 << 3,
/// Local use 4 (local4)
LOCAL4 = 20 << 3,
/// Local use 5 (local5)
LOCAL5 = 21 << 3,
/// Local use 6 (local6)
LOCAL6 = 22 << 3,
/// Local use 7 (local7)
LOCAL7 = 23 << 3
}

/// Severities as defined in RFC 3164/5424
#[derive(Debug)]
pub enum Severity {
/// Emergency: system is unusable
EMERGENCY = 0,
/// Alert: action must be taken immediately
ALERT = 1,
/// Critical: critical conditions
CRITICAL = 2,
/// Error: error conditions
ERROR = 3,
/// Warning: warning conditions
WARNING = 4,
/// Notice: normal but significant condition
NOTICE = 5,
/// Informational: informational messages
INFO = 6,
/// Debug: debug-level messages
DEBUG = 7
}

/// Converts log level to syslog severity
#[doc(hidden)]
pub fn level_to_severity(lvl: LogLevel) -> u8 {
match lvl {
LogLevel::Error => Severity::ERROR as u8,
LogLevel::Warn => Severity::WARNING as u8,
LogLevel::Info => Severity::INFO as u8,
LogLevel::Debug => Severity::DEBUG as u8,
LogLevel::Trace => Severity::DEBUG as u8
}
}

#[doc(hidden)]
pub fn parse_facility(f: &String) -> Result<Facility, io::Error> {
let res = match f.to_lowercase().as_str() {
"kern" => Facility::KERN,
"user" => Facility::USER,
"mail" => Facility::MAIL,
"daemon" => Facility::DAEMON,
"auth" => Facility::AUTH,
"syslog" => Facility::SYSLOG,
"lpr" => Facility::LPR,
"news" => Facility::NEWS,
"uucp" => Facility::UUCP,
"cron" => Facility::CRON,
"authpriv" => Facility::AUTHPRIV,
"ftp" => Facility::FTP,
"ntp" => Facility::NTP,
"logau" => Facility::LOGAU,
"logalt" => Facility::LOGALT,
"cron2" => Facility::CRON2,
"local0" => Facility::LOCAL0,
"local1" => Facility::LOCAL1,
"local2" => Facility::LOCAL2,
"local3" => Facility::LOCAL3,
"local4" => Facility::LOCAL4,
"local5" => Facility::LOCAL5,
"local6" => Facility::LOCAL6,
"local7" => Facility::LOCAL7,
_ => return Err(io::Error::new(io::ErrorKind::Other, format!("Unsupported facility {}", f).as_str()))
};
Ok(res)
}
Loading