Skip to content

Commit

Permalink
feat(io_handler): minimise exception logging in production mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamhexi committed Nov 1, 2024
1 parent a0f8e2c commit e5f8124
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion knowledge_verificator/io_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
from functools import cache
from logging import Logger
from pathlib import Path
import sys
from rich.console import Console
from rich.logging import RichHandler
from rich.markup import escape

from knowledge_verificator.utils.configuration_parser import (
Configuration,
ConfigurationParser,
)

console = Console()


def get_argument_parser() -> ArgumentParser:
"""
Expand Down Expand Up @@ -60,10 +64,29 @@ def config() -> Configuration:
_logging_handler.setLevel(configuration.logging_level)
logger.addHandler(_logging_handler)

if configuration.production_mode:

def handle_exceptions_in_production_mode(
exception_type, exception, traceback
):
"""
An exception handler for production mode, which hides
exception_type (the first argument) and traceback
(the third argument) whilst printing erros in bold red
text.
"""
console.print(
f'[bold red]The error has occured: {escape(str(exception))}. [/bold red]'
)
console.print(
'Closing the application to prevent unexpected consequences.'
)

sys.excepthook = handle_exceptions_in_production_mode

return configuration


console = Console()
logger = Logger('main_logger')

_logging_handler = RichHandler(rich_tracebacks=True)

0 comments on commit e5f8124

Please sign in to comment.