Skip to content

Commit aa3de0b

Browse files
authored
[ISSUE #2616]🚀Add Level for log setting🔥 (#2617)
1 parent 7c92f82 commit aa3de0b

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

rocketmq-common/src/log.rs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
use std::fmt;
1718
use std::str::FromStr;
1819

19-
use tracing::level_filters::LevelFilter;
20+
use tracing::metadata::LevelFilter;
2021

2122
/// Initializes the logger with the specified configuration.
2223
///
@@ -31,6 +32,87 @@ pub fn init_logger() {
3132
.with_level(true)
3233
.with_line_number(true)
3334
.with_thread_ids(true)
34-
.with_max_level(LevelFilter::from_str(info_level.as_str()).unwrap())
35+
.with_max_level(tracing::Level::from_str(info_level.as_str()).expect("Invalid log level"))
3536
.init();
3637
}
38+
39+
pub fn init_logger_with_level(level: Level) {
40+
tracing_subscriber::fmt()
41+
.with_thread_names(true)
42+
.with_level(true)
43+
.with_line_number(true)
44+
.with_thread_ids(true)
45+
.with_max_level(tracing::Level::from_str(level.as_str()).expect("Invalid log level"))
46+
.init();
47+
}
48+
49+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
50+
pub struct Level(&'static str);
51+
52+
impl Level {
53+
/// Constant representing the ERROR log level.
54+
pub const ERROR: Level = Level("ERROR");
55+
56+
/// Constant representing the WARN log level.
57+
pub const WARN: Level = Level("WARN");
58+
59+
/// Constant representing the INFO log level.
60+
pub const INFO: Level = Level("INFO");
61+
62+
/// Constant representing the DEBUG log level.
63+
pub const DEBUG: Level = Level("DEBUG");
64+
65+
/// Constant representing the TRACE log level.
66+
pub const TRACE: Level = Level("TRACE");
67+
68+
pub fn as_str(&self) -> &'static str {
69+
self.0
70+
}
71+
}
72+
73+
impl From<&'static str> for Level {
74+
fn from(level: &'static str) -> Self {
75+
match level {
76+
"ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE" => Level(level),
77+
_ => panic!("Invalid log level: {}", level),
78+
}
79+
}
80+
}
81+
82+
impl fmt::Display for Level {
83+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84+
f.pad(self.0)
85+
}
86+
}
87+
88+
#[cfg(test)]
89+
mod tests {
90+
use super::*;
91+
92+
#[test]
93+
fn level_as_str_returns_correct_value() {
94+
assert_eq!(Level::ERROR.as_str(), "ERROR");
95+
assert_eq!(Level::WARN.as_str(), "WARN");
96+
assert_eq!(Level::INFO.as_str(), "INFO");
97+
assert_eq!(Level::DEBUG.as_str(), "DEBUG");
98+
assert_eq!(Level::TRACE.as_str(), "TRACE");
99+
}
100+
101+
#[test]
102+
fn level_from_str_creates_correct_level() {
103+
assert_eq!(Level::from("ERROR"), Level::ERROR);
104+
assert_eq!(Level::from("WARN"), Level::WARN);
105+
assert_eq!(Level::from("INFO"), Level::INFO);
106+
assert_eq!(Level::from("DEBUG"), Level::DEBUG);
107+
assert_eq!(Level::from("TRACE"), Level::TRACE);
108+
}
109+
110+
#[test]
111+
fn level_display_formats_correctly() {
112+
assert_eq!(format!("{}", Level::ERROR), "ERROR");
113+
assert_eq!(format!("{}", Level::WARN), "WARN");
114+
assert_eq!(format!("{}", Level::INFO), "INFO");
115+
assert_eq!(format!("{}", Level::DEBUG), "DEBUG");
116+
assert_eq!(format!("{}", Level::TRACE), "TRACE");
117+
}
118+
}

0 commit comments

Comments
 (0)