Skip to content
This repository was archived by the owner on Dec 7, 2024. It is now read-only.

Commit 07b2291

Browse files
committed
feat: timestamp support
1 parent 98915d9 commit 07b2291

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": "all",
3+
}

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ license = "MIT"
1010
keywords = ["dioxus", "log", "logging"]
1111
categories = ["development-tools::debugging"]
1212

13-
#[features]
14-
#stdio = []
15-
#http_logging = ["dep:reqwest"]
13+
[features]
14+
timestamps = ["dep:time"]
1615

1716
[dependencies]
1817
log = { version = "0.4", features = ["std"] }
18+
time = { version = "0.3.17", features = ["std", "wasm-bindgen"], optional = true }
1919
#reqwest = { version = "0.11", features = [] }
2020
web-sys = { version = "0.3.60", features = ["console"] }

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
**Current & Planned Features**
2929
- [x] stdio logging (Web, Desktop) - Mobile to come
3030
- [x] Custom log format - Basic Implementation Finished
31+
- [x] Timestamps
3132
- [ ] Sending logs over HTTP to an API
3233
- [ ] Logging to a file
3334
- [ ] Logging to [Sentry](https://sentry.io/)?
34-
- [ ] Timestamps
35-
- [ ] Feature flags for faster compilation
3635

3736
**This library is under development. Expect breaking changes.**
3837

src/lib.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const PATH: &str = "{PATH}";
77
/// The arguments passed through the log macro.
88
/// E.g. the args from ``info!("hello")`` will be ``"hello"``
99
const ARGS: &str = "{ARGS}";
10+
/// A timestamp of when the log was generated.
11+
const TIMESTAMP: &str = "{TIMESTAMP}";
1012

1113
/// The primary logging struct that contains all runtime configuration.
1214
pub struct DioxusLogger {
@@ -17,9 +19,14 @@ pub struct DioxusLogger {
1719
impl DioxusLogger {
1820
/// Create a new [`DioxusLogger`] struct to configure and build.
1921
pub fn new(level_filter: LevelFilter) -> Self {
22+
let format = "[{LEVEL}] {PATH} - {ARGS}]";
23+
24+
#[cfg(feature = "timestamps")]
25+
let format = "[{TIMESTAMP} | {LEVEL}] {PATH} - {ARGS}]";
26+
2027
Self {
2128
level_filter,
22-
format: "[{LEVEL}] {PATH} - {ARGS}]",
29+
format,
2330
}
2431
}
2532

@@ -30,9 +37,9 @@ impl DioxusLogger {
3037
}
3138

3239
/// Allows you to define a custom format.
33-
///
34-
/// The available options are `{LEVEL}`, `{PATH}` and `{ARGS}`
35-
///
40+
///
41+
/// The available options are `{LEVEL}`, `{PATH}`, `{ARGS}` and `{TIMESTAMP}`
42+
///
3643
/// Providing the format of `[{LEVEL}] {PATH} - {ARGS}]` will return something like `[INFO] dioxus_testing - this is my log message`
3744
pub fn use_format(&mut self, format: &'static str) {
3845
self.format = format;
@@ -61,15 +68,8 @@ impl log::Log for DioxusLogger {
6168
// ARGS
6269
.replace(ARGS, record.args().to_string().as_str());
6370

64-
/*
65-
// The old format, not customizable
66-
let formatted = format!(
67-
"[{}] {} - {}",
68-
record.level(),
69-
record.module_path().unwrap_or(""),
70-
record.args()
71-
);
72-
*/
71+
#[cfg(feature = "timestamps")]
72+
let formatted = format_timestamp(formatted);
7373

7474
#[cfg(all(
7575
not(target_family = "wasm"),
@@ -85,6 +85,13 @@ impl log::Log for DioxusLogger {
8585
fn flush(&self) {}
8686
}
8787

88+
fn format_timestamp(formatted: String) -> String {
89+
let timestamp = time::OffsetDateTime::now_utc();
90+
formatted
91+
.to_owned()
92+
.replace(TIMESTAMP, timestamp.to_string().as_str())
93+
}
94+
8895
/// Initialize `log` and `dioxus-logger` with a specified filter.
8996
/// For more advanced configuration, build the logger through the [`DioxusLogger`] struct.
9097
pub fn init(level_filter: LevelFilter) -> Result<(), SetLoggerError> {

0 commit comments

Comments
 (0)