Skip to content

Commit 7ed133a

Browse files
committed
add logging to file by demand
1 parent 2f2ef32 commit 7ed133a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

autointent/_logging/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ formatters:
1818
handlers:
1919
stdout:
2020
class: logging.StreamHandler
21-
formatter: json
21+
formatter: simple
2222
stream: ext://sys.stdout
2323
loggers:
2424
root:
25+
level: DEBUG
2526
handlers:
2627
- stdout

autointent/_logging/setup.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import importlib.resources as ires
22
import logging.config
33
import logging.handlers
4+
from pathlib import Path
45

56
import yaml
67

78
from autointent.custom_types import LogLevel
89

910

10-
def setup_logging(level: LogLevel | str) -> None:
11+
def setup_logging(level: LogLevel | str, log_to_filepath: Path | str | None = None) -> None:
1112
config_file = ires.files("autointent._logging").joinpath("config.yaml")
1213
with config_file.open() as f_in:
1314
config = yaml.safe_load(f_in)
1415

1516
level = LogLevel(level)
16-
config["loggers"]["root"]["level"] = level.value
17+
config["handlers"]["stdout"]["level"] = level.value
18+
19+
if log_to_filepath is not None:
20+
config["loggers"]["root"]["handlers"].append("file")
21+
config["handlers"]["file"] = {
22+
"class": "logging.FileHandler",
23+
"level": "DEBUG",
24+
"formatter": "json",
25+
"filename": str(log_to_filepath),
26+
}
27+
1728

1829
logging.config.dictConfig(config)

0 commit comments

Comments
 (0)