11import inspect
2+ import sys
23import warnings
34from collections .abc import Callable
45from functools import wraps
@@ -18,6 +19,7 @@ def setup_logging(
1819 level : Literal ["DEBUG" , "INFO" , "WARNING" , "ERROR" , "CRITICAL" ]
1920 | None = None ,
2021 file : PathType | None = None ,
22+ use_rich : bool = True ,
2123 ** kwargs ,
2224) -> None : # pragma: no cover
2325 """Sets up global logging using loguru and rich.
@@ -28,6 +30,8 @@ def setup_logging(
2830 @type file: Optional[str]
2931 @param file: Path to the log file. If provided, logs will be saved
3032 to this file.
33+ @type use_rich: bool
34+ @param use_rich: If True, uses rich for logging. Defaults to True.
3135 @type kwargs: Any
3236 @param kwargs: Additional keyword arguments to pass to
3337 C{RichHandler}.
@@ -42,40 +46,47 @@ def setup_logging(
4246 )
4347
4448 logger .remove ()
45-
46- theme = Theme (
47- {
48- "logging.level.debug" : "magenta" ,
49- "logging.level.info" : "green" ,
50- "logging.level.warning" : "yellow" ,
51- "logging.level.error" : "bold red" ,
52- "logging.level.critical" : "bold white on red" ,
53- },
54- inherit = True ,
55- )
56- console = Console (theme = theme )
57- logger .add (
58- RichHandler (
59- console = console ,
60- rich_tracebacks = True ,
61- tracebacks_show_locals = False ,
62- show_time = False ,
63- ** kwargs ,
64- ),
65- level = level ,
66- # NOTE: Needs to be a constant function to avoid
67- # duplicate logging of exceptions, see
68- # https://github.com/Delgan/loguru/issues/1172
69- format = lambda _ : "{message}" ,
70- backtrace = False ,
71- filter = lambda record : "file_only" not in record ["extra" ],
72- )
49+ if use_rich :
50+ theme = Theme (
51+ {
52+ "logging.level.debug" : "magenta" ,
53+ "logging.level.info" : "green" ,
54+ "logging.level.warning" : "yellow" ,
55+ "logging.level.error" : "bold red" ,
56+ "logging.level.critical" : "bold white on red" ,
57+ },
58+ inherit = True ,
59+ )
60+ console = Console (theme = theme )
61+ logger .add (
62+ RichHandler (
63+ console = console ,
64+ rich_tracebacks = True ,
65+ tracebacks_show_locals = False ,
66+ show_time = False ,
67+ ** kwargs ,
68+ ),
69+ level = level ,
70+ # NOTE: Needs to be a constant function to avoid
71+ # duplicate logging of exceptions, see
72+ # https://github.com/Delgan/loguru/issues/1172
73+ format = lambda _ : "{message}" ,
74+ backtrace = False ,
75+ filter = lambda record : "file_only" not in record ["extra" ],
76+ )
77+ else :
78+ logger .add (
79+ sys .stderr ,
80+ level = level ,
81+ format = "[{level}] {message} | {function}:{line}" ,
82+ filter = lambda record : "file_only" not in record ["extra" ],
83+ )
7384
7485 if file is not None :
7586 logger .add (
7687 file ,
7788 level = level ,
78- format = "<green> {time:YYYY-MM-DD HH:mm:ss}</green> [<level> {level}</level> ] {message}" ,
89+ format = "{time:YYYY-MM-DD HH:mm:ss} [ {level}] {message} | {function}:{line }" ,
7990 rotation = None ,
8091 )
8192
0 commit comments