Skip to content

Commit 6f9a095

Browse files
committed
Rewrite most documentation and examples
1 parent 62de1c6 commit 6f9a095

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+972
-975
lines changed

README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
[![](https://img.shields.io/badge/docs.rs-spdlog--rs-ff69b4?style=flat-square&logo=rust)](https://docs.rs/spdlog-rs)
66
[![](https://img.shields.io/github/actions/workflow/status/SpriteOvO/spdlog-rs/ci.yml?branch=main&style=flat-square&logo=githubactions&logoColor=white)](https://github.com/SpriteOvO/spdlog-rs/actions/workflows/ci.yml)
77

8-
A fast and combinable Rust logging crate, inspired by the C++ logging library [spdlog].
8+
Fast, highly configurable Rust logging crate, inspired by the C++ logging library [spdlog].
99

1010
## Features
1111

1212
- Very fast (see [Benchmarks]).
13-
- Various log targets:
14-
- Standard streams with optional colors.
15-
- Files.
16-
- Rotating log files by file size.
17-
- Rotating log files hourly.
18-
- Rotating log files daily.
19-
- ... (more targets are implementing, PRs are welcome)
20-
- Extendable with custom log targets.
21-
- Compatible with [log crate] (optional).
2213
- Asynchronous support.
23-
- Configured via environment variable.
24-
- Custom formatting.
25-
- Log filtering - log levels can be modified in runtime as well as in compile time.
14+
- Compatible with `log` crate.
15+
- Custom log formats:
16+
- compile-time zero-cost pattern or runtime pattern;
17+
- manually implementing for more flexibility.
18+
- Various combinable sinks:
19+
- standard streams with optional color support;
20+
- files (single file, rotating hourly, daily or by file size);
21+
- platform-specific (e.g. `journald` for Linux and `OutputDebugStringW` for Windows);
22+
- ... and able to implement one yourself.
23+
- Configuring via environment variables or TOML[^1].
24+
- More readable level filters.
25+
26+
[^1]: TOML deserialization support is working in progress, tracking issue [#25]
2627

2728
## Getting started
2829

@@ -32,11 +33,11 @@ Add this to `Cargo.toml`:
3233
spdlog-rs = "0.3"
3334
```
3435

35-
The documentation of this crate is hosted on [docs.rs], and you can find examples under [./examples] directory.
36+
The documentation of this crate is hosted on [docs.rs], and you can learn examples under [./examples] directory along with it.
3637

37-
If you have any questions or need help while using this crate, feel free to [open a discussion]. For feature requests or bug reports, please [open an issue].
38+
If you have any trouble while using this crate, please don't hesitate to [open a discussion] for help. For feature requests or bug reports, please [open an issue].
3839

39-
## Supported Rust Versions
40+
## Supported Rust versions
4041

4142
<!--
4243
When updating this, also update:
@@ -55,22 +56,18 @@ The current minimum supported Rust version is 1.56.
5556

5657
Licensed under either of
5758

58-
* Apache License, Version 2.0
59-
([LICENSE-APACHE](/LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
60-
* MIT license
61-
([LICENSE-MIT](/LICENSE-MIT) or http://opensource.org/licenses/MIT)
59+
* Apache License, Version 2.0 ([LICENSE-APACHE](/LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
60+
* MIT license ([LICENSE-MIT](/LICENSE-MIT) or http://opensource.org/licenses/MIT)
6261

6362
at your option.
6463

6564
## Contribution
6665

67-
Unless you explicitly state otherwise, any contribution intentionally submitted
68-
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
69-
dual licensed as above, without any additional terms or conditions.
66+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
7067

7168
[spdlog]: https://github.com/gabime/spdlog
7269
[Benchmarks]: https://github.com/SpriteOvO/spdlog-rs/blob/main/spdlog/benches/README.md
73-
[log crate]: https://crates.io/crates/log
70+
[#25]: https://github.com/SpriteOvO/spdlog-rs/issues/25
7471
[./examples]: https://github.com/SpriteOvO/spdlog-rs/tree/main/spdlog/examples
7572
[docs.rs]: https://docs.rs/spdlog-rs/
7673
[open a discussion]: https://github.com/SpriteOvO/spdlog-rs/discussions/new

spdlog/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ name = "ftlog_3_level_off"
147147
path = "benches/ftlog/3_level_off.rs"
148148

149149
[[example]]
150-
name = "06_compatible_with_log_crate"
150+
name = "06_log_crate"
151151
required-features = ["log"]
152152
[[example]]
153-
name = "07_async_pool_sink"
153+
name = "07_async"
154154
required-features = ["multi-thread"]

spdlog/examples/01_default_logger.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

spdlog/examples/01_macro.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// All log macros and common types are under `spdlog::prelude` module.
2+
use spdlog::prelude::*;
3+
4+
fn main() {
5+
// Writes a log at "info" level with the info level, and this log will be
6+
// processed by the global default logger - It will be output to `stdout`.
7+
info!("program started");
8+
9+
let file = "config.json";
10+
11+
// They will be output to `stderr`.
12+
error!("failed to open file: {}", file);
13+
warn!("undetermined locale, defaults to `en_US.UTF-8`");
14+
15+
// Level "trace" and "debug" will be ignored by default, you can modify the
16+
// level filter of the global default logger to enable all levels.
17+
spdlog::default_logger().set_level_filter(LevelFilter::All);
18+
19+
trace!("position x: {}, y: {}", 11.4, -5.14);
20+
}

spdlog/examples/02_building_logger.rs

Lines changed: 0 additions & 59 deletions
This file was deleted.

spdlog/examples/02_file.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::{env, sync::Arc};
2+
3+
use spdlog::{
4+
prelude::*,
5+
sink::{FileSink, RotatingFileSink, RotationPolicy},
6+
};
7+
8+
fn main() -> Result<(), Box<dyn std::error::Error>> {
9+
configure_file_logger()?;
10+
configure_rotating_file_logger()?;
11+
12+
Ok(())
13+
}
14+
15+
fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
16+
let path = env::current_exe()?.with_file_name("file.log");
17+
18+
let file_sink = Arc::new(FileSink::builder().path(path).build()?);
19+
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
20+
spdlog::set_default_logger(new_logger);
21+
22+
info!("this log will be written to the file `all.log`");
23+
24+
Ok(())
25+
}
26+
27+
fn configure_rotating_file_logger() -> Result<(), Box<dyn std::error::Error>> {
28+
let path = env::current_exe()?.with_file_name("rotating.log");
29+
30+
let file_sink = Arc::new(
31+
RotatingFileSink::builder()
32+
.base_path(path)
33+
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
34+
.build()?,
35+
);
36+
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
37+
spdlog::set_default_logger(new_logger);
38+
39+
info!("this log will be written to the file `rotating.log`, and the file will be rotated daily at 00:00");
40+
41+
Ok(())
42+
}

spdlog/examples/03_file_sink.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

spdlog/examples/03_logger.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::{env, sync::Arc, time::Duration};
2+
3+
use spdlog::{
4+
prelude::*,
5+
sink::{FileSink, Sink},
6+
};
7+
8+
fn main() -> Result<(), Box<dyn std::error::Error>> {
9+
// `spdlog-rs` has a global default logger and logs will be processed by it
10+
// by default, You can configure it.
11+
let default_logger = spdlog::default_logger();
12+
default_logger.set_level_filter(LevelFilter::All);
13+
14+
// Or completely replace it with a new one.
15+
let path = env::current_exe()?.with_file_name("all.log");
16+
let file_sink = Arc::new(FileSink::builder().path(path).build()?);
17+
18+
let new_logger = Arc::new(
19+
Logger::builder()
20+
.level_filter(LevelFilter::All)
21+
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
22+
.sink(file_sink.clone())
23+
.build()?,
24+
);
25+
new_logger.set_flush_period(Some(Duration::from_secs(3)));
26+
spdlog::set_default_logger(new_logger);
27+
28+
info!("this log will be written to the file `all.log`");
29+
30+
// In addition to having the global default logger, more loggers are allowed to
31+
// be configured, stored and used independently.
32+
let db = AppDatabase::new(file_sink)?;
33+
db.write_i32(114514);
34+
35+
Ok(())
36+
}
37+
38+
struct AppDatabase {
39+
logger: Logger,
40+
}
41+
42+
impl AppDatabase {
43+
fn new(all_log_sink: Arc<dyn Sink>) -> Result<Self, Box<dyn std::error::Error>> {
44+
let path = env::current_exe()?.with_file_name("db.log");
45+
let db_file_sink = Arc::new(FileSink::builder().path(path).build()?);
46+
47+
let logger = Logger::builder()
48+
.name("database")
49+
.level_filter(LevelFilter::All)
50+
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
51+
.sinks([all_log_sink, db_file_sink])
52+
.build()?;
53+
Ok(Self { logger })
54+
}
55+
56+
fn write_i32(&self, value: i32) {
57+
// This log will be written to both files `all.log` and `db.log`.
58+
trace!(logger: self.logger, "writing value {} to the database", value);
59+
}
60+
}

0 commit comments

Comments
 (0)