|
1 | 1 | # scribe-rust |
| 2 | + |
2 | 3 | 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: |
3 | 4 |
|
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` |
8 | 14 |
|
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: |
10 | 22 |
|
11 | 23 | ```rust |
12 | | -let logger = Logger::default(); |
| 24 | +use std::sync::Arc; |
| 25 | +use tracing::info; |
| 26 | +use tracing_subscriber::prelude::*; |
13 | 27 |
|
14 | | -logger.info("Starting My Service!"); |
| 28 | +// Assuming Logger and CustomLoggerLayer are defined as in your code |
15 | 29 |
|
16 | | -#[derive(Debug)] |
17 | | -pub enum ActionType { |
18 | | - Lambda, |
19 | | - Webhook, |
20 | | -} |
| 30 | +fn main() { |
| 31 | + // Initialize the logger |
| 32 | + let logger = Logger::default(); |
21 | 33 |
|
22 | | -let action_type = ActionType::Lambda; |
| 34 | + // Create the custom layer |
| 35 | + let custom_layer = CustomLoggerLayer { |
| 36 | + logger: Arc::clone(&logger), |
| 37 | + }; |
23 | 38 |
|
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); |
26 | 41 |
|
| 42 | + // Set the global default subscriber |
| 43 | + tracing::subscriber::set_global_default(subscriber) |
| 44 | + .expect("Failed to set global subscriber"); |
27 | 45 |
|
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