Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix(logger): add structured api (#450)
Browse files Browse the repository at this point in the history
* doc(logger): add structured api

* fix(logger): wrong api usage example
  • Loading branch information
zeroqn authored Sep 10, 2020
1 parent aca0fb2 commit 4ef3d93
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion common/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,25 @@ This signature of the function is showed below. The `JsonValue` is a `enum` from
pub fn metrics(name: &str, mut content: JsonValue)
```

## Structured Event Log With TraceId Included

Structured event log api provide a convenient way to log structured json data. It's signature is provided as below:

```rust
pub fn log(level: Level, module: &str, event: &str, ctx: &Context, mut msg: JsonValue)
```

`module` should be your component name, `event` is just event name, better begin with 4 chars with 4 digits
to identify this event. `Context` is used to extract trace id. `msg` is `JsonValue` which is same as `metrics`.

Useage example:

```rust
common_logger::log(Level::Info, "network", "netw0001", &ctx, common_logger::object!{"music": "beautiful world"});
```

## Yaml File

The `log.yml` in this crate is the yaml style config of log4rs with default logger config.

If you need more customized configurations, you can copy the file to some config path, edit the file, and replace the `init` function with `log4rs::init_file("/path/to/log.yml", Default::default()).unwrap();`.
If you need more customized configurations, you can copy the file to some config path, edit the file, and replace the `init` function with `log4rs::init_file("/path/to/log.yml", Default::default()).unwrap();`.
4 changes: 2 additions & 2 deletions common/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub fn metrics(name: &str, mut content: JsonValue) {
}

// Usage:
// log(Level::Info, "network", "netw0001", &ctx, json!{"music": "beautiful
// world"})
// log(Level::Info, "network", "netw0001", &ctx, common_logger::object!{"music"
// : "beautiful world"})
pub fn log(level: Level, module: &str, event: &str, ctx: &Context, mut msg: JsonValue) {
if let Some(trace_ctx) = trace_context(ctx) {
msg["trace_id"] = trace_ctx.trace_id.to_string().into();
Expand Down

0 comments on commit 4ef3d93

Please sign in to comment.