pub struct MessageBuilder { /* private fields */ }Expand description
Fluent builder for a single syslog message with a JSON key-value payload.
Fields default to the RFC 5424 nil value (-) unless set explicitly,
except proc_id which defaults to the current process ID.
Keys provided to kv are sorted alphabetically in the
serialized JSON object. Duplicate keys are deduplicated — the last value
provided for a given key is used.
Call cee_cookie to prefix the JSON payload with
@cee:, producing a MITRE CEE-compatible syslog message.
Implementations§
Source§impl MessageBuilder
impl MessageBuilder
Sourcepub fn new(facility: Facility, severity: Severity) -> Self
pub fn new(facility: Facility, severity: Severity) -> Self
Create a new builder with the given facility and severity.
The process ID is populated automatically from std::process::id.
All other optional header fields default to nil (-).
Prefix the JSON payload with the MITRE CEE cookie (@cee:).
When enabled, the built message body will be @cee:{...} instead of
{...}, making it compatible with CEE-aware log processors (e.g.
rsyslog’s mmjsonparse module).
The default is false (no CEE cookie).
Sourcepub fn timestamp(self, ts: impl Into<String>) -> Self
pub fn timestamp(self, ts: impl Into<String>) -> Self
Set the RFC 5424 TIMESTAMP field (RFC 3339 formatted string).
Use now_rfc3339 to obtain the current UTC time without pulling
in an external time library.
Sourcepub fn proc_id(self, pid: impl Into<String>) -> Self
pub fn proc_id(self, pid: impl Into<String>) -> Self
Override the RFC 5424 PROCID field (defaults to the current PID).
Sourcepub fn kv(
self,
key: impl Into<String>,
val: impl Serialize,
) -> Result<Self, BuildError>
pub fn kv( self, key: impl Into<String>, val: impl Serialize, ) -> Result<Self, BuildError>
Add a JSON key-value field to the message payload.
val can be any type that implements serde::Serialize: strings,
integers, booleans, floats, nested objects, and arrays are all accepted.
If the same key is added more than once, the last value is used.
§Errors
Returns BuildError::EmptyKey if key is the empty string.
Returns BuildError::Serialize if val cannot be serialized to JSON
(rare — only custom Serialize implementations can fail).
Sourcepub fn build(self) -> Result<SyslogMessage, BuildError>
pub fn build(self) -> Result<SyslogMessage, BuildError>
Assemble the accumulated fields into a SyslogMessage.
The JSON payload is serialized with keys in alphabetical order.
§Errors
Returns BuildError::Serialize if the field map cannot be serialized
to a JSON string (should not happen in practice for well-formed values).
Sourcepub async fn send<T: Transport>(self, transport: &T) -> Result<(), ClientError>
pub async fn send<T: Transport>(self, transport: &T) -> Result<(), ClientError>
Build the message and send it via transport in one step.
§Errors
Returns ClientError::Build if build fails, or a
transport-specific error if delivery fails.