|
32 | 32 | //! * Log panics example: <https://github.com/iorust/structured-logger/blob/main/examples/panic_log.rs>
|
33 | 33 | //! * Async log example: <https://github.com/iorust/structured-logger/blob/main/examples/async_log.rs>
|
34 | 34 | //!
|
35 |
| -//! Muilti writers example: |
| 35 | +//! Simple example: |
36 | 36 | //! ```rust
|
37 | 37 | //! use serde::Serialize;
|
38 |
| -//! use std::{fs::File, io::stdout}; |
39 |
| -//! use structured_logger::{json::new_writer, unix_ms, Builder}; |
| 38 | +//! use structured_logger::{async_json::new_writer, unix_ms, Builder}; |
40 | 39 | //!
|
41 |
| -//! fn main() { |
| 40 | +//! #[tokio::main] |
| 41 | +//! async fn main() { |
42 | 42 | //! // Initialize the logger.
|
43 |
| -//! // Optional: create a file to write logs to. |
44 |
| -//! let log_file = File::options() |
45 |
| -//! .create(true) |
46 |
| -//! .append(true) |
47 |
| -//! .open("app.log") |
48 |
| -//! .unwrap(); |
49 |
| -//! |
50 |
| -//! // or Builder::with_level("debug") |
51 |
| -//! Builder::new() |
52 |
| -//! // Optional: set a specific writer (format to JSON, write to stdout) for target starts with "api". |
53 |
| -//! .with_target_writer("api*", new_writer(stdout())) |
54 |
| -//! // Optional: set a specific writer (format to JSON, write to app.log file) for target "file" and "db". |
55 |
| -//! .with_target_writer("file,db", new_writer(log_file)) |
| 43 | +//! Builder::with_level("info") |
| 44 | +//! .with_target_writer("*", new_writer(tokio::io::stdout())) |
56 | 45 | //! .init();
|
57 | 46 | //!
|
58 | 47 | //! let kv = ContextLog {
|
|
61 | 50 | //! };
|
62 | 51 | //!
|
63 | 52 | //! log::info!("hello world");
|
64 |
| -//! // This log will be written to stderr (default writer): |
| 53 | +//! // This log will be written to stdout: |
65 | 54 | //! // {"level":"INFO","message":"hello world","target":"simple","timestamp":1679745592127}
|
66 | 55 | //!
|
67 | 56 | //! log::info!(target: "api",
|
|
75 | 64 | //! );
|
76 | 65 | //! // This log will be written to stdout:
|
77 | 66 | //! // {"elapsed":10,"kv":{"uid":"user123","action":"upate_book"},"level":"INFO","message":"","method":"GET","path":"/hello","start":1679745592127,"status":200,"target":"api","timestamp":1679745592127}
|
78 |
| -//! |
79 |
| -//! log::info!(target: "file", |
80 |
| -//! method = "GET", |
81 |
| -//! path = "/hello", |
82 |
| -//! status = 200_u16, |
83 |
| -//! start = unix_ms(), |
84 |
| -//! elapsed = 10_u64, |
85 |
| -//! kv = log::as_serde!(kv); |
86 |
| -//! "", |
87 |
| -//! ); |
88 |
| -//! // This log will be written to file "app.log": |
89 |
| -//! // {"elapsed":10,"kv":{"uid":"user123","action":"upate_book"},"level":"INFO","message":"","method":"GET","path":"/hello","start":1679745592127,"status":200,"target":"file","timestamp":1679745592127} |
90 | 67 | //! }
|
91 | 68 | //!
|
92 | 69 | //! #[derive(Serialize)]
|
|
0 commit comments