11"""Centralized logging configuration for the odoo-data-flow application."""
22
33import logging
4- import sys
54from typing import Optional
65
6+ from rich .logging import RichHandler
7+
78# Get the root logger for the application package
89log = logging .getLogger ("odoo_data_flow" )
910
@@ -12,7 +13,8 @@ def setup_logging(verbose: bool = False, log_file: Optional[str] = None) -> None
1213 """Configures the root logger for the application.
1314
1415 This function sets up handlers to print logs to the console and optionally
15- to a specified file.
16+ to a specified file. It uses the 'rich' library to provide colorful,
17+ easy-to-read console output.
1618
1719 Args:
1820 verbose: If True, the logging level is set to DEBUG.
@@ -28,20 +30,23 @@ def setup_logging(verbose: bool = False, log_file: Optional[str] = None) -> None
2830 if log .hasHandlers ():
2931 log .handlers .clear ()
3032
31- # Create a formatter to be used by all handlers
32- formatter = logging . Formatter ( "%(asctime)s - %(levelname)s - %(message)s" )
33-
34- # Always create a handler to print to the console
35- console_handler = logging . StreamHandler ( sys . stdout )
36- console_handler . setFormatter ( formatter )
33+ # Create a rich handler for beautiful, colorful console output
34+ console_handler = RichHandler (
35+ rich_tracebacks = True ,
36+ markup = True ,
37+ log_time_format = "[%X]" ,
38+ )
3739 log .addHandler (console_handler )
3840
39- # If a log file is specified, create a file handler as well
41+ # If a log file is specified, create a standard file handler as well.
42+ # We use a standard handler here to ensure the log file contains plain text
43+ # without any color codes.
4044 if log_file :
4145 try :
42- file_handler = logging .FileHandler (log_file )
46+ file_handler = logging .FileHandler (log_file , mode = "a" )
47+ formatter = logging .Formatter ("%(asctime)s - %(levelname)s - %(message)s" )
4348 file_handler .setFormatter (formatter )
4449 log .addHandler (file_handler )
45- log .info (f"Logging to file: { log_file } " )
50+ log .info (f"Logging to file: [bold cyan] { log_file } [/bold cyan] " )
4651 except Exception as e :
4752 log .error (f"Failed to set up log file at { log_file } : { e } " )
0 commit comments