Skip to content

Commit 6fada4c

Browse files
authored
refactor!: logger builder (#124)
Signed-off-by: tison <wander4096@gmail.com>
1 parent 42ce584 commit 6fada4c

File tree

14 files changed

+81
-35
lines changed

14 files changed

+81
-35
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ jobs:
7979
run: |
8080
set -x
8181
cargo run --example simple_stdout
82+
cargo run --example log_with_logger
8283
cargo run --example multiple_dispatches
8384
cargo run --example custom_layout_filter
8485
cargo run --no-default-features --example simple_stdout

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file.
1010
* `Append::flush` is now fallible.
1111
* `Diagnostic`'s and `Visitor`'s `visit` methods are fallible.
1212
* `NonBlocking` related types and the feature flag are now private.
13+
* `logforth::Builder` is renamed to `logforth::LoggerBuilder`.
14+
* `LoggerBuilder` has no longer an option to configure the global `max_level`. Check its documentation for more details.
1315
* Constructing `RollingFile` and `Syslog` appender is heavily simplified.
1416

1517
Before:
@@ -75,6 +77,7 @@ fn construct_syslog() {
7577

7678
* Add `LogfmtLayout` to support logfmt format.
7779
* Add `GoogleStructuredLogLayout` to support Google structured log format.
80+
* `LoggerBuilder` now has a `build` method to construct the `Logger` for use.
7881

7982
## [0.23.1] 2025-03-23
8083

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal-serde = ["dep:serde", "log/kv_serde", "jiff/serde"]
5656
anyhow = { version = "1.0" }
5757
env_filter = { version = "0.1.1" }
5858
jiff = { version = "0.2" }
59-
log = { version = "0.4", features = ["std", "kv"] }
59+
log = { version = "0.4.27", features = ["std", "kv"] }
6060

6161
# Optional dependencies
6262
colored = { version = "3.0", optional = true }
@@ -91,6 +91,11 @@ doc-scrape-examples = true
9191
name = "simple_stdout"
9292
path = "examples/simple_stdout.rs"
9393

94+
[[example]]
95+
doc-scrape-examples = true
96+
name = "log_with_logger"
97+
path = "examples/log_with_logger.rs"
98+
9499
[[example]]
95100
doc-scrape-examples = true
96101
name = "json_stdout"

examples/log_with_logger.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2024 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use log::LevelFilter;
16+
17+
fn main() {
18+
log::set_max_level(LevelFilter::Trace);
19+
let l = logforth::stdout().build();
20+
21+
log::error!(logger: l, "Hello error!");
22+
log::warn!(logger: l, "Hello warn!");
23+
log::info!(logger: l, "Hello info!");
24+
log::debug!(logger: l, "Hello debug!");
25+
log::trace!(logger: l, "Hello trace!");
26+
}

src/append/journald/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Rolling File Appender
1+
# Journald Appender
22

33
This appender is a remix of [tracing-journald](https://crates.io/crates/tracing-journald) and [systemd-journal-logger](https://crates.io/crates/systemd-journal-logger), with several modifications to fit this crate's needs.

src/append/rolling_file/rotation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum Rotation {
3232
}
3333

3434
impl Rotation {
35+
/// Get the next date timestamp based on the current date and rotation policy.
3536
pub fn next_date_timestamp(&self, current_date: &Zoned) -> Option<usize> {
3637
let timestamp_round = ZonedRound::new().mode(RoundMode::Trunc);
3738

@@ -50,6 +51,7 @@ impl Rotation {
5051
Some(next_date.timestamp().as_millisecond() as usize)
5152
}
5253

54+
/// Get the date format string for the rotation policy.
5355
pub fn date_format(&self) -> &'static str {
5456
match *self {
5557
Rotation::Minutely => "%F-%H-%M",

src/diagnostic/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub trait Visitor {
3636

3737
/// A trait representing a Mapped Diagnostic Context (MDC) that provides diagnostic key-values.
3838
pub trait Diagnostic: fmt::Debug + Send + Sync + 'static {
39+
/// Visits the MDC key-values with the provided visitor.
3940
fn visit(&self, visitor: &mut dyn Visitor) -> anyhow::Result<()>;
4041
}
4142

src/diagnostic/static_global.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ pub struct StaticDiagnostic {
3434
}
3535

3636
impl StaticDiagnostic {
37+
/// Creates a new [`StaticDiagnostic`] instance with a prebuilt key-value store.
3738
pub fn new(kvs: BTreeMap<String, String>) -> Self {
3839
Self { kvs }
3940
}
4041

42+
/// Inserts a key-value pair into the static diagnostic .
4143
pub fn insert<K, V>(&mut self, key: K, value: V)
4244
where
4345
K: Into<String>,
@@ -46,6 +48,7 @@ impl StaticDiagnostic {
4648
self.kvs.insert(key.into(), value.into());
4749
}
4850

51+
/// Remove a key-value pair from the static diagnostic.
4952
pub fn remove(&mut self, key: &str) {
5053
self.kvs.remove(key);
5154
}

src/diagnostic/thread_local.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ thread_local! {
3636
pub struct ThreadLocalDiagnostic {}
3737

3838
impl ThreadLocalDiagnostic {
39+
/// Inserts a key-value pair into the thread local diagnostic .
3940
pub fn insert<K, V>(key: K, value: V)
4041
where
4142
K: Into<String>,
@@ -46,6 +47,7 @@ impl ThreadLocalDiagnostic {
4647
});
4748
}
4849

50+
/// Removes a key-value pair from the thread local diagnostic.
4951
pub fn remove(key: &str) {
5052
CONTEXT.with(|map| {
5153
map.borrow_mut().remove(key);

0 commit comments

Comments
 (0)