Skip to content

Commit

Permalink
fix: πŸš‘ loguru logs always colorized (#48)
Browse files Browse the repository at this point in the history
+ new param "force_colorize" defaulted to None instead of
  having it obscured and always to True
  • Loading branch information
lucas-labs authored Dec 16, 2024
1 parent 4c360da commit ab5368f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def format_record(record: Any) -> str:
'level': LogLevel.DEBUG,
# 'format': format_record,
'access_log': True,
'sinks': [{'sink': 'example/logs/app.log', 'rotation': '1 week', 'format': format_record}],
'sinks': [{'sink': 'examples/logs/app.log', 'rotation': '1 week', 'format': format_record}],
},
middleware=[Middleware(PureAsgiMiddleware), pest_middleware],
cors={
Expand Down
9 changes: 9 additions & 0 deletions pest/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ class LoggingOptions(TypedDict, total=False):
verbose: bool
'''enables all loggers'''
sinks: List[SinkOptions]
'''list of sinks (handlers) to be used by loguru'''
force_colorize: bool
'''forces colorized output for the default sink'''
diagnose: bool
'''
Whether the exception trace should display the variables values to eases the debugging.
`False` (the default) is recommended for production environments, to avoid leaking sensitive
data..
'''
10 changes: 7 additions & 3 deletions pest/logging/loguru/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ def setup(cls, **options: Unpack[LoggingOptions]) -> None:
access_log = options.get('access_log', False)
verbose = options.get('verbose', False)
sinks = options.get('sinks', [])
colorize = options.get('force_colorize', None)
diagnose = options.get('diagnose', False)

# if a format string is provided, we replace the default
if type(fmt) is str:
FORMAT = fmt

cls.__config_standard_interception(intercept, verbose)
cls.__configure_shush(shush)
cls.__config_sinks(sinks, level, access_log, fmt)
cls.__config_sinks(sinks, level, access_log, fmt, colorize, diagnose)

@classmethod
def __config_standard_interception(
Expand Down Expand Up @@ -171,15 +173,17 @@ def __config_sinks(
level: LogLevel,
access_log: bool,
fmt: Union[str, Callable[[loguru.Record], str], None],
colorize: bool | None = None,
diagnose: bool = False,
) -> None:
"""configures loguru sinks (handlers)"""
fmt_func = fmt if callable(fmt) else format_record

loguru_logger.configure(handlers=[])
loguru_logger.add(
sys.stdout,
colorize=True,
diagnose=False,
colorize=colorize,
diagnose=diagnose,
format=fmt_func,
level=level,
filter=lambda record: ((True if access_log else no_access(record))),
Expand Down

0 comments on commit ab5368f

Please sign in to comment.