Skip to content

Commit

Permalink
feat: re-export fasyslog to allow modify sender before constructing a…
Browse files Browse the repository at this point in the history
…ppender (#74)

Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Nov 12, 2024
1 parent c94686e commit b13c145
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [0.17.1] 2024-11-12

### Refactors

* Change the re-export of `syslog` to `logforth::syslog::fasyslog` ([#74](https://github.com/fast/logforth/pull/74))

## [0.17.0] 2024-11-12

### New features
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ log = { version = "0.4", features = ["std", "kv_unstable"] }
# Optional dependencies
crossbeam-channel = { version = "0.5", optional = true }
fastrace = { version = "0.7", optional = true }
fasyslog = { version = "0.1.1", optional = true }
fasyslog = { version = "0.2", optional = true }
opentelemetry = { version = "0.27", features = ["logs"], optional = true }
opentelemetry-otlp = { version = "0.27", features = [
"logs",
Expand Down
51 changes: 26 additions & 25 deletions src/append/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use std::io;

use fasyslog::format::SyslogContext;
use fasyslog::sender::SyslogSender;
use fasyslog::SDElement;
use log::Record;
Expand All @@ -46,12 +47,7 @@ use crate::non_blocking::Writer;
use crate::Append;
use crate::Layout;

// re-exports to avoid version conflicts
mod exported {
pub use fasyslog::format::SyslogContext;
pub use fasyslog::Facility;
}
pub use exported::*;
pub extern crate fasyslog;

/// The format of the syslog message.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -179,51 +175,56 @@ pub struct SyslogWriter {
}

impl SyslogWriter {
/// Create a new syslog writer that sends messages to the given syslog sender.
pub fn new(sender: SyslogSender) -> Self {
Self { sender }
}

/// Create a new syslog writer that sends messages to the well-known TCP port (514).
pub fn tcp_well_known() -> io::Result<SyslogWriter> {
fasyslog::sender::tcp_well_known().map(|sender| Self {
sender: SyslogSender::Tcp(sender),
})
fasyslog::sender::tcp_well_known()
.map(SyslogSender::Tcp)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the given TCP address.
pub fn tcp<A: std::net::ToSocketAddrs>(addr: A) -> io::Result<SyslogWriter> {
fasyslog::sender::tcp(addr).map(|sender| Self {
sender: SyslogSender::Tcp(sender),
})
fasyslog::sender::tcp(addr)
.map(SyslogSender::Tcp)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the well-known UDP port (514).
pub fn udp_well_known() -> io::Result<SyslogWriter> {
fasyslog::sender::udp_well_known().map(|sender| Self {
sender: SyslogSender::Udp(sender),
})
fasyslog::sender::udp_well_known()
.map(SyslogSender::Udp)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the given UDP address.
pub fn udp<L: std::net::ToSocketAddrs, R: std::net::ToSocketAddrs>(
local: L,
remote: R,
) -> io::Result<SyslogWriter> {
fasyslog::sender::udp(local, remote).map(|sender| Self {
sender: SyslogSender::Udp(sender),
})
fasyslog::sender::udp(local, remote)
.map(SyslogSender::Udp)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the given Unix stream socket.
#[cfg(unix)]
pub fn unix_stream(path: impl AsRef<std::path::Path>) -> io::Result<SyslogWriter> {
fasyslog::sender::unix_stream(path).map(|sender| Self {
sender: SyslogSender::UnixStream(sender),
})
fasyslog::sender::unix_stream(path)
.map(SyslogSender::UnixStream)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the given Unix datagram socket.
#[cfg(unix)]
pub fn unix_datagram(path: impl AsRef<std::path::Path>) -> io::Result<SyslogWriter> {
fasyslog::sender::unix_datagram(path).map(|sender| Self {
sender: SyslogSender::UnixDatagram(sender),
})
fasyslog::sender::unix_datagram(path)
.map(SyslogSender::UnixDatagram)
.map(Self::new)
}

/// Create a new syslog writer that sends messages to the given Unix socket.
Expand All @@ -232,7 +233,7 @@ impl SyslogWriter {
/// path.
#[cfg(unix)]
pub fn unix(path: impl AsRef<std::path::Path>) -> io::Result<SyslogWriter> {
fasyslog::sender::unix(path).map(|sender| Self { sender })
fasyslog::sender::unix(path).map(Self::new)
}
}

Expand Down

0 comments on commit b13c145

Please sign in to comment.