Skip to content

Commit b640cd4

Browse files
author
Joshua Tracey
committed
feat!: integrated with rusts tracing library for access to wider logging capability
BREAKING-CHANGE: Now uses rusts tracing library, so will need to intancicate global subscriber.
1 parent b0e546e commit b640cd4

4 files changed

Lines changed: 368 additions & 35 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "scribe-rust"
3-
version = "0.2.0"
3+
version = "1.0.0"
44
edition = "2021"
55
license = "MIT"
66
authors = ["joshua.tracey08@gmail.com"]
@@ -9,7 +9,10 @@ repository = "https://github.com/josh-tracey/scribe-rust"
99
keywords = ["logging", "logger", "scribe", "simple", "color-coded"]
1010

1111

12-
1312
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1413

1514
[dependencies]
15+
serde = "1.0.214"
16+
serde_json = "1.0.132"
17+
tracing = "0.1.40"
18+
tracing-subscriber = "0.3.18"

README.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# scribe-rust
2+
23
Scribe-rust is a straightforward and color-coded logging library for Rust applications. It provides an easy-to-use API to log messages with different severity levels. Here's what you can expect from scribe-rust:
34

4-
- **Flexible Log Levels:** With five different levels (Trace, Debug, Info, Warn, Error), you have granular control over the logging output based on the severity of the messages. You can use these levels to differentiate critical errors from minor debugging information.
5-
- **Intuitive Logging Methods:** The library provides a set of logging methods corresponding to each level (trace, debug, info, warn, error). You just need to call the appropriate method with your message, and scribe-rust takes care of the rest.
6-
- **Color-Coded Output:** Scribe-rust makes reading logs easier by color-coding the output based on the log level. For example, 'Error' messages are displayed in red, and 'Info' messages in green. This visual cue can help you spot critical issues more quickly.
7-
- **Environment Variable Support:** You can control the log level of your application at runtime using the LOG_LEVEL environment variable. This feature makes it easy to adjust the verbosity of your logs without changing the code.
5+
- **Flexible Log Levels:** With five different levels (Trace, Debug, Info, Warn, Error), you have granular control over the logging output based on the severity of the messages. This allows you to differentiate critical errors from minor debugging information.
6+
7+
- **Intuitive Logging Methods:** The library provides a set of logging methods corresponding to each level (`trace`, `debug`, `info`, `warn`, `error`). You can call the appropriate method with your message, and scribe-rust handles the rest.
8+
9+
- **Color-Coded Output:** Scribe-rust enhances readability by color-coding the output based on the log level. For example, 'Error' messages are displayed in red, and 'Info' messages in green. This visual cue helps you spot critical issues more quickly.
10+
11+
- **Environment Variable Support:** You can control the log level of your application at runtime using the `LOG_LEVEL` environment variable. This feature makes it easy to adjust the verbosity of your logs without changing the code.
12+
13+
## Integration with `tracing`
814

9-
#### As an Example
15+
Scribe-rust integrates seamlessly with the [`tracing`](https://docs.rs/tracing/latest/tracing/) crate, a framework for instrumenting Rust programs to collect structured, event-based diagnostic information. By implementing a custom `Layer`, scribe-rust can process and log events emitted by `tracing`.
16+
17+
### Custom Logger Layer
18+
19+
The `CustomLoggerLayer` struct implements the `Layer` trait from `tracing_subscriber`, allowing it to intercept and log events. It utilizes a `FieldVisitor` to extract structured data from events, which is then serialized into JSON for detailed logging.
20+
21+
Here's an example of how to set up the custom logger layer:
1022

1123
```rust
12-
let logger = Logger::default();
24+
use std::sync::Arc;
25+
use tracing::info;
26+
use tracing_subscriber::prelude::*;
1327

14-
logger.info("Starting My Service!");
28+
// Assuming Logger and CustomLoggerLayer are defined as in your code
1529

16-
#[derive(Debug)]
17-
pub enum ActionType {
18-
Lambda,
19-
Webhook,
20-
}
30+
fn main() {
31+
// Initialize the logger
32+
let logger = Logger::default();
2133

22-
let action_type = ActionType::Lambda;
34+
// Create the custom layer
35+
let custom_layer = CustomLoggerLayer {
36+
logger: Arc::clone(&logger),
37+
};
2338

24-
logger.debug(&format!("Executing {:?} Action...", action_type));
25-
```
39+
// Set up the subscriber with the custom layer
40+
let subscriber = tracing_subscriber::Registry::default().with(custom_layer);
2641

42+
// Set the global default subscriber
43+
tracing::subscriber::set_global_default(subscriber)
44+
.expect("Failed to set global subscriber");
2745

28-
**Please note: as of the current version, scribe-rust is designed for simplicity and ease of use. It focuses on console output and does not yet support logging to files, remote systems, or custom message formatting. Future enhancements may add these and other advanced features.
46+
// Example usage
47+
info!("This is an info message.");
48+
logger.warn("This is a warning message.");
49+
}

0 commit comments

Comments
 (0)