Skip to content

Commit 62a9617

Browse files
committed
episode 1a logging
1 parent 4836428 commit 62a9617

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

1a_logging/python/foo/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import logging
2+
3+
from . import bar
4+
5+
log = logging.getLogger(__name__)
6+
7+
8+
def run():
9+
log.warn("warn")
10+
log.info("info")
11+
log.debug("debug")
12+
bar.run()

1a_logging/python/foo/bar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
3+
log = logging.getLogger(__name__)
4+
5+
6+
def run():
7+
log.warn("warn")
8+
log.info("info")
9+
log.debug("debug")

1a_logging/python/log.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
import logging.config
3+
import yaml
4+
5+
import foo
6+
7+
if __name__ == "__main__":
8+
with open('logconf.yaml', 'r') as f:
9+
log_cfg = yaml.safe_load(f.read())
10+
logging.config.dictConfig(log_cfg)
11+
logging.warning('warn')
12+
logging.error("error")
13+
logging.info('info')
14+
logging.debug("debug")
15+
foo.run()

1a_logging/python/logconf.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: 1
2+
3+
formatters:
4+
simple:
5+
format: "%(asctime)s [%(levelname)s] [%(name)s] - %(message)s"
6+
datefmt: "%Y-%m-%dT%H:%M:%S"
7+
extended:
8+
format: "%(asctime)s [%(levelname)s] [%(name)s:%(lineno)s] - %(message)s"
9+
datefmt: "%Y-%m-%dT%H:%M:%S"
10+
11+
handlers:
12+
console:
13+
class: logging.StreamHandler
14+
level: DEBUG
15+
formatter: simple
16+
17+
file_handler:
18+
class: logging.FileHandler
19+
level: DEBUG
20+
filename: test.log
21+
formatter: extended
22+
23+
loggers:
24+
foo:
25+
level: INFO
26+
handlers: [console]
27+
propagate: no
28+
foo.bar:
29+
level: DEBUG
30+
handlers: [console, file_handler]
31+
propagate: no
32+
root:
33+
level: INFO
34+
handlers: [console]

1a_logging/rust/logex/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "logex"
3+
version = "0.1.0"
4+
authors = ["Bedroom Builds"]
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+
chrono = "0.4.19"
11+
env_logger = "0.8.3"
12+
log = "0.4.14"

1a_logging/rust/logex/src/main.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use chrono::Local;
2+
use env_logger::Builder;
3+
use log::LevelFilter;
4+
use std::io::Write;
5+
6+
mod foo {
7+
mod bar {
8+
pub fn run() {
9+
log::warn!("warn");
10+
log::info!("info");
11+
log::debug!("debug");
12+
}
13+
}
14+
15+
pub fn run() {
16+
log::warn!("warn");
17+
log::info!("info");
18+
log::debug!("debug");
19+
bar::run();
20+
}
21+
}
22+
23+
fn main() {
24+
Builder::new()
25+
.format(|buf, record| {
26+
writeln!(
27+
buf,
28+
"{} [{}] [{}] - {}",
29+
Local::now().format("%Y-%m-%dT%H:%M:%S"),
30+
record.level(),
31+
record.module_path().unwrap_or("-"),
32+
record.args()
33+
)
34+
})
35+
// run with LOGEX_LOG="warn,logex::foo=info,logex::foo::bar=debug"
36+
.parse_env("LOGEX_LOG")
37+
// filter_level wins over environment
38+
.filter_level(LevelFilter::Info)
39+
.init();
40+
41+
log::warn!("warn");
42+
log::error!("error");
43+
log::info!("info");
44+
log::debug!("debug");
45+
foo::run();
46+
}

0 commit comments

Comments
 (0)