pub struct Logger { /* private fields */ }Expand description
Writes log lines with a set of fields; cheap to clone (see the module documentation).
Ctx::log gives the request’s logger; code without a
Ctx uses Logger::new.
§Examples
use ocre::log::{Format, Level, Logger};
let log = Logger::new().with("order_id", 42).with("password", "hunter2");
log.info("paid"); // written to the Worker logs
assert_eq!(
log.line(Level::Info, "paid", Format::Json),
r#"{"level":"info","message":"paid","order_id":42,"password":"[FILTERED]"}"#
);
assert_eq!(log.line(Level::Warn, "slow", Format::Text), "WARN slow order_id=42 password=[FILTERED]");Implementations§
Source§impl Logger
impl Logger
Sourcepub fn with(&self, key: &str, value: impl Serialize) -> Self
pub fn with(&self, key: &str, value: impl Serialize) -> Self
A copy of this logger with one more field on every line (Rails’ logger.tagged).
Any serializable value; a value that does not serialize is written
as null. A field with the same name is replaced.
§Examples
let log = ocre::log::Logger::new().with("user_id", 7);
assert_eq!(log.field("user_id"), Some(&serde_json::json!(7)));Sourcepub fn field(&self, key: &str) -> Option<&Value>
pub fn field(&self, key: &str) -> Option<&Value>
The value of a field, e.g. request_id.
§Examples
assert_eq!(ocre::log::Logger::new().field("request_id"), None);Sourcepub fn enabled(&self, level: Level) -> bool
pub fn enabled(&self, level: Level) -> bool
Whether lines of level are written, under the current LOG_LEVEL.
Check it before building an expensive message that is not
format_args!(...) (Rails’ block form of logger.debug { ... }).
§Examples
let log = ocre::log::Logger::new();
if log.enabled(ocre::log::Level::Debug) {
log.debug(serde_json::json!({"big": [1, 2, 3]}));
}Sourcepub fn debug(&self, message: impl Display)
pub fn debug(&self, message: impl Display)
Writes a debug line.
§Examples
let rows = 3;
ocre::log::Logger::new().debug(format_args!("{rows} rows"));Sourcepub fn log(&self, level: Level, message: &dyn Display)
pub fn log(&self, level: Level, message: &dyn Display)
Writes a line of level when it is enabled; the message is formatted only then.
§Examples
ocre::log::Logger::new().log(ocre::log::Level::Info, &"hello");Trait Implementations§
Auto Trait Implementations§
impl Freeze for Logger
impl RefUnwindSafe for Logger
impl Send for Logger
impl Sync for Logger
impl Unpin for Logger
impl UnsafeUnpin for Logger
impl UnwindSafe for Logger
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more