Skip to content

Commit 9833baa

Browse files
committed
Add 《精通rust》env_logger
1 parent f0d2def commit 9833baa

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed

README.org

+7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ Examples for [[https://github.com/yewstack/yew][yew]].
2222

2323
* 《深入浅出rust》(dive into rust)
2424
Based on examples from [[https://book.douban.com/subject/30312231/][《深入浅出rust》]].
25+
2526
| code |
2627
| [[file:dive-into-rust/src/lib.rs][第一部分:基础知识]] |
2728
| [[file:dive-into-rust/src/lib.rs][第二部分:内存安全]] |
2829
| [[file:dive-into-rust/src/lib.rs][第三部分:高级抽象]] |
2930
| [[file:dive-into-rust/src/lib.rs][第四部分:线程安全]] |
31+
32+
* 《精通rust》(Mastering rust)
33+
Examples from[[https://book.douban.com/subject/35290878/][《精通rust(第2版)》]].
34+
35+
| chapter | code |
36+
| 11.5.2 | [[file:mastering-rust/ch11_log/env_logger_demo/src/main.rs][env_logger]] |

mastering-rust/ch11_log/env_logger_demo/Cargo.lock

+149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "env_logger_demo"
3+
version = "0.1.0"
4+
authors = ["ShuLiang <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
env_logger = "0.8.4"
11+
user_auth = { path = "../user_auth" }
12+
log = { version = "0.4.14", features = ["release_max_level_error", "max_level_trace"] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Run the demo with `RUST_LOG=user_auth=info, env_logger_demo=info cargo run`
2+
//! and `RUST_LOG=user_auth=info, env_logger_demo=info cargo run --release` to
3+
//! see the difference of log output which is configured in `../Cargo.toml`.
4+
//! Set env var is another way to get the same result.
5+
6+
use log::debug;
7+
8+
use user_auth::User;
9+
10+
fn main() {
11+
std::env::set_var("RUST_LOG", "user_auth=info, env_logger_demo=info cargo run");
12+
env_logger::init();
13+
debug!("env logger demo started");
14+
let user = User::new("bob", "super_sekret");
15+
user.sign_in("super_secret");
16+
user.sign_in("super_sekret");
17+
}

mastering-rust/ch11_log/user_auth/Cargo.lock

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "user_auth"
3+
version = "0.1.0"
4+
authors = ["ShuLiang <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
log = "0.4.14"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use log::{error, info};
2+
3+
pub struct User {
4+
name: String,
5+
pass: String,
6+
}
7+
8+
impl User {
9+
pub fn new(name: &str, pass: &str) -> Self {
10+
User {
11+
name: name.to_string(),
12+
pass: pass.to_string(),
13+
}
14+
}
15+
16+
pub fn sign_in(&self, pass: &str) {
17+
if pass != self.pass {
18+
info!("Signing in user: {}", self.name);
19+
} else {
20+
error!("Login failed for user: {}", self.name);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)