Skip to content

Commit

Permalink
docs: integrate with log and others
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Nov 12, 2024
1 parent 6a5bee5 commit 3fa13e7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ windows-targets = { version = "0.52.6" }

[dev-dependencies]
names = { version = "0.14.0", default-features = false }


[[example]]
doc-scrape-examples = true
name = "tcp_sender"
path = "examples/tcp_sender.rs"

[[example]]
doc-scrape-examples = true
name = "udp_sender"
path = "examples/udp_sender.rs"

[[example]]
doc-scrape-examples = true
name = "unix_sender"
path = "examples/unix_sender.rs"
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@ Add `fasyslog` to your `Cargo.toml`:
cargo add fasyslog
```

```rust
use fasyslog::Severity;

fn main() {
let mut sender = fasyslog::sender::tcp_well_known().unwrap();
let message = format!("Hello, fasyslog!");
// send a message with RFC 3164 format
sender.send_rfc3164(Severity::ERROR, message).unwrap();
sender.flush().unwrap();

// send a message with RFC 5424 format
const EMPTY_MSGID: Option<&str> = None;
const EMPTY_STRUCTURED_DATA: Vec<fasyslog::SDElement> = Vec::new();
sender.send_rfc5424(Severity::ERROR, EMPTY_MSGID, EMPTY_STRUCTURED_DATA, message).unwrap();
sender.flush().unwrap();
}
```

If you'd like to integrate with `log` crate, you can try the `logforth` example:

```toml
[dependencies]
log = { version = "..." }
logforth = { version = "...", features = ["syslog"] }
```

```rust
use logforth::append::syslog;
use logforth::append::syslog::Syslog;
use logforth::append::syslog::SyslogWriter;

fn main() {
let syslog_writer = SyslogWriter::tcp_well_known().unwrap();
let (non_blocking, _guard) = syslog::non_blocking_builder().finish(syslog_writer);

logforth::builder()
.dispatch(|d| {
d.filter(log::LevelFilter::Trace)
.append(Syslog::new(non_blocking))
})
.apply();

log::info!("This log will be written to syslog.");
}
```

## Example

Check the [examples](examples) directory for more examples.
Expand Down
8 changes: 8 additions & 0 deletions src/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ impl SyslogSender {
}

/// Flush the underlying writer if needed.
///
/// When the underlying writer is a streaming writer (TCP, UnixStream, etc.), periodically
/// flush is essential to ensure that the message is sent to the syslog server [1].
///
/// When the underlying writer is a datagram writer (UDP, UnixDatagram, etc.), flush is a no-op,
/// and every call to `send_xxx` defines the boundary of the packet.
///
/// [1]: https://github.com/Geal/rust-syslog/issues/69
pub fn flush(&mut self) -> io::Result<()> {
match self {
SyslogSender::Tcp(sender) => sender.flush(),
Expand Down

0 comments on commit 3fa13e7

Please sign in to comment.