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

Commit 98915d9

Browse files
committed
feat: logging
1 parent 800d0d0 commit 98915d9

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dioxus-logger"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "A logging utility to provide a standard interface whether you're targetting web, desktop, or mobile."
66
repository = "https://github.com/DogeDark/dioxus-logger"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
**Current & Planned Features**
2929
- [x] stdio logging (Web, Desktop) - Mobile to come
30+
- [x] Custom log format - Basic Implementation Finished
3031
- [ ] Sending logs over HTTP to an API
3132
- [ ] Logging to a file
3233
- [ ] Logging to [Sentry](https://sentry.io/)?
3334
- [ ] Timestamps
34-
- [ ] Custom log format
3535
- [ ] Feature flags for faster compilation
3636

3737
**This library is under development. Expect breaking changes.**

src/lib.rs

+49-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
use log::{LevelFilter, SetLoggerError};
2-
//use std::path::PathBuf;
32

4-
/*pub struct HttpConfig {
5-
method: reqwest::Method,
6-
url: String,
7-
authorization_token: String,
8-
}
9-
10-
pub enum OutputType {
11-
Json,
12-
Stdio,
13-
}
14-
15-
pub struct FileSystemConfig {
16-
file_name: String,
17-
output_type: OutputType,
18-
output_path: PathBuf,
19-
}*/
3+
/// The log level. E.g. ``INFO``
4+
const LEVEL: &str = "{LEVEL}";
5+
/// The file's path or crate name. E.g. ``dioxus-testing``
6+
const PATH: &str = "{PATH}";
7+
/// The arguments passed through the log macro.
8+
/// E.g. the args from ``info!("hello")`` will be ``"hello"``
9+
const ARGS: &str = "{ARGS}";
2010

2111
/// The primary logging struct that contains all runtime configuration.
2212
pub struct DioxusLogger {
23-
//http_config: Option<HttpConfig>,
24-
//file_system_config: Option<FileSystemConfig>,
2513
level_filter: LevelFilter,
14+
format: &'static str,
2615
}
2716

2817
impl DioxusLogger {
29-
pub fn new(
30-
level_filter: LevelFilter,
31-
//file_system_config: Option<FileSystemConfig>,
32-
//http_config: Option<HttpConfig>,
33-
) -> Self {
18+
/// Create a new [`DioxusLogger`] struct to configure and build.
19+
pub fn new(level_filter: LevelFilter) -> Self {
3420
Self {
35-
//http_config,
36-
//file_system_config,
3721
level_filter,
22+
format: "[{LEVEL}] {PATH} - {ARGS}]",
3823
}
3924
}
25+
26+
/// Builds and initializes the logger with [`log`]
27+
pub fn build(self) -> Result<(), SetLoggerError> {
28+
let level = self.level_filter.clone();
29+
log::set_boxed_logger(Box::new(self)).map(|()| log::set_max_level(level))
30+
}
31+
32+
/// Allows you to define a custom format.
33+
///
34+
/// The available options are `{LEVEL}`, `{PATH}` and `{ARGS}`
35+
///
36+
/// Providing the format of `[{LEVEL}] {PATH} - {ARGS}]` will return something like `[INFO] dioxus_testing - this is my log message`
37+
pub fn use_format(&mut self, format: &'static str) {
38+
self.format = format;
39+
}
4040
}
4141

4242
impl log::Log for DioxusLogger {
@@ -49,7 +49,27 @@ impl log::Log for DioxusLogger {
4949
return;
5050
}
5151

52-
let formatted = format!("[{}] {} - {}", record.level(), record.module_path().unwrap_or(""), record.args());
52+
// This is cursed
53+
let formatted = self
54+
.format
55+
// LEVEL
56+
.replace(LEVEL, record.level().as_str())
57+
.to_owned()
58+
// PATH
59+
.replace(PATH, record.module_path().unwrap_or(""))
60+
.to_owned()
61+
// ARGS
62+
.replace(ARGS, record.args().to_string().as_str());
63+
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+
*/
5373

5474
#[cfg(all(
5575
not(target_family = "wasm"),
@@ -66,7 +86,7 @@ impl log::Log for DioxusLogger {
6686
}
6787

6888
/// Initialize `log` and `dioxus-logger` with a specified filter.
89+
/// For more advanced configuration, build the logger through the [`DioxusLogger`] struct.
6990
pub fn init(level_filter: LevelFilter) -> Result<(), SetLoggerError> {
70-
let logger = DioxusLogger::new(level_filter);
71-
log::set_boxed_logger(Box::new(logger)).map(|()| log::set_max_level(level_filter))
91+
DioxusLogger::new(level_filter).build()
7292
}

0 commit comments

Comments
 (0)