-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdexter_logging.py
More file actions
59 lines (51 loc) · 1.4 KB
/
dexter_logging.py
File metadata and controls
59 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations
import logging
import sys
from pathlib import Path
from typing import TextIO
def _close_handler(handler: logging.Handler) -> None:
try:
handler.flush()
except Exception:
pass
try:
handler.close()
except Exception:
pass
def configure_root_logging(
*,
format: str,
level: int = logging.INFO,
datefmt: str | None = None,
log_file: Path | str | None = None,
stream: TextIO | None = None,
force: bool = False,
) -> bool:
root = logging.getLogger()
if force:
for handler in list(root.handlers):
root.removeHandler(handler)
_close_handler(handler)
elif root.handlers:
return False
if stream is None:
stream = sys.stdout
if stream is not None and hasattr(stream, "reconfigure"):
try:
stream.reconfigure(encoding="utf-8")
except Exception:
pass
handlers: list[logging.Handler] = []
if log_file is not None:
log_path = Path(log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
handlers.append(logging.FileHandler(log_path, encoding="utf-8", delay=True))
if stream is not None:
handlers.append(logging.StreamHandler(stream))
logging.basicConfig(
format=format,
datefmt=datefmt,
level=level,
handlers=handlers,
)
return True