Skip to content

Commit

Permalink
Merge pull request #1519 from pbiering/log-exception-conditional
Browse files Browse the repository at this point in the history
Log exception conditional on debug level
  • Loading branch information
pbiering authored Jun 9, 2024
2 parents ac14b01 + 695c5d8 commit 518de6b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,12 @@ Log bad PUT request content (for further diagnostics)

Default: `False'

##### backtrace_on_debug

Log backtrace on level=debug

Default: `True'

#### headers

In this section additional HTTP headers that are sent to clients can be
Expand Down
3 changes: 3 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
# Log bad PUT request content
#bad_put_request_content = False

# Log backtrace on level=debug
# backtrace_on_debug = True


[headers]

Expand Down
2 changes: 1 addition & 1 deletion radicale/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _get_application_instance(config_path: str, wsgi_errors: types.ErrorStream
configuration = config.load(config.parse_compound_paths(
config.DEFAULT_CONFIG_PATH,
config_path))
log.set_level(cast(str, configuration.get("logging", "level")))
log.set_level(cast(str, configuration.get("logging", "level")), configuration.get("logging", "backtrace_on_debug"))
# Log configuration after logger is configured
default_config_active = True
for source, miss in configuration.sources():
Expand Down
4 changes: 2 additions & 2 deletions radicale/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def exit_signal_handler(signal_number: int,
# Preliminary configure logging
with contextlib.suppress(ValueError):
log.set_level(config.DEFAULT_CONFIG_SCHEMA["logging"]["level"]["type"](
vars(args_ns).get("c:logging:level", "")))
vars(args_ns).get("c:logging:level", "")), True)

# Update Radicale configuration according to arguments
arguments_config: types.MUTABLE_CONFIG = {}
Expand All @@ -165,7 +165,7 @@ def exit_signal_handler(signal_number: int,
sys.exit(1)

# Configure logging
log.set_level(cast(str, configuration.get("logging", "level")))
log.set_level(cast(str, configuration.get("logging", "level")), configuration.get("logging", "backtrace_on_debug"))

# Log configuration after logger is configured
default_config_active = True
Expand Down
4 changes: 4 additions & 0 deletions radicale/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def _convert_to_bool(value: Any) -> bool:
"value": "False",
"help": "log bad PUT request content",
"type": bool}),
("backtrace_on_debug", {
"value": "True",
"help": "log backtrace on level=debug",
"type": bool}),
("mask_passwords", {
"value": "True",
"help": "mask passwords in logs",
Expand Down
12 changes: 9 additions & 3 deletions radicale/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,23 @@ def setup() -> None:
register_stream = handler.register_stream
log_record_factory = IdentLogRecordFactory(logging.getLogRecordFactory())
logging.setLogRecordFactory(log_record_factory)
set_level(logging.WARNING)
set_level(logging.INFO, True)
if format_name != sane_format_name:
logger.error("Invalid RADICALE_LOG_FORMAT: %r", format_name)


def set_level(level: Union[int, str]) -> None:
def set_level(level: Union[int, str], backtrace_on_debug: bool) -> None:
"""Set logging level for global logger."""
if isinstance(level, str):
level = getattr(logging, level.upper())
assert isinstance(level, int)
logger.setLevel(level)
logger.removeFilter(REMOVE_TRACEBACK_FILTER)
if level > logging.DEBUG:
logger.info("Logging of backtrace is disabled in this loglevel")
logger.addFilter(REMOVE_TRACEBACK_FILTER)
else:
if not backtrace_on_debug:
logger.debug("Logging of backtrace is disabled by option in this loglevel")
logger.addFilter(REMOVE_TRACEBACK_FILTER)
else:
logger.removeFilter(REMOVE_TRACEBACK_FILTER)

0 comments on commit 518de6b

Please sign in to comment.