Skip to main content

MessageBuilder

Struct MessageBuilder 

Source
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

Source

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).

Source

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.

Source

pub fn hostname(self, h: impl Into<String>) -> Self

Set the RFC 5424 HOSTNAME field.

Source

pub fn app_name(self, name: impl Into<String>) -> Self

Set the RFC 5424 APP-NAME field.

Source

pub fn proc_id(self, pid: impl Into<String>) -> Self

Override the RFC 5424 PROCID field (defaults to the current PID).

Source

pub fn msgid(self, id: impl Into<String>) -> Self

Set the RFC 5424 MSGID field.

Source

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).

Source

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).

Source

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.

Trait Implementations§

Source§

impl Debug for MessageBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.