SpriteOvO / spdlog-rs

Fast, highly configurable Rust logging crate
https://crates.io/crates/spdlog-rs
Apache License 2.0
107 stars 11 forks source link

Add support for log target #50

Closed Lancern closed 9 months ago

Lancern commented 10 months ago

The log crate support specifying log target in the syntax below:

info!(target: "log-target", "log body");
//    ^~~~~~~~~~~~~~~~~~~~

The log target is a string that can be fully customized. Before this PR, this field is not used in spdlog-rs. Actually it can be used as a way to announce the logger name when you want to emit logs in a library through the log crate. Thus, in this PR, I used this information as another source of logger names.

Specifically, when constructing a spdlog::Record from a log::Record with a specific spdlog::Logger: (see also source)

API Breaking

This PR indeed has potential impacts. It's actually API breaking because the signature of spdlog::Record::logger_name is updated from:

impl<'a> Record<'a> {
    fn logger_name(&self) -> Option<&'a str>;
}

to:

impl<'a> Record<'a> {
    fn logger_name(&self) -> Option<&str>;
}

. This is because we have to change the way we store the logger name in spdlog::Record. Before this PR, the logger name is simply represented by a &'a str. In this PR, I need to update its type to Cow<&'a, str> because I have to store an owned version of the log target name if the record is constructed from a log crate record.

But I believe the impact of this change should be minimal.