1- # -*- coding: utf-8 -*-
2-
3- ## Standard libraries
1+ # Standard libraries
42import os
53import copy
64import json
75import logging
8- from typing import Union , Dict , Any
6+ from typing import Any
97
10- ## Third-party libraries
8+ # Third-party libraries
119import yaml
1210from loguru import logger
1311from loguru ._logger import Logger
1816else :
1917 from pydantic import validate_arguments as validate_call
2018
21- ## Internal modules
19+ # Internal modules
2220from ._utils import create_dir , deep_merge
2321from ._handlers import InterceptHandler
2422from .rotation import RotationChecker
@@ -67,20 +65,22 @@ class LoggerLoader:
6765 @validate_call
6866 def __init__ (
6967 self ,
70- config : Union [ LoggerConfigPM , Dict [str , Any ], None ] = None ,
68+ config : LoggerConfigPM | dict [str , Any ] | None = None ,
7169 config_file_path : str = _CONFIG_FILE_PATH ,
7270 auto_config_file : bool = True ,
7371 auto_load : bool = False ,
7472 ):
7573 """LoggerLoader constructor method.
7674
7775 Args:
78- config (Union[LoggerConfigPM,
79- dict,
80- None ], optional): New logger config to update loaded config. Defaults to None.
81- config_file_path (str , optional): Logger config file path. Defaults to `LoggerLoader._CONFIG_FILE_PATH`.
82- auto_config_file (bool , optional): Indicates whether to load logger config file or not. Defaults to True.
83- auto_load (bool , optional): Indicates whether to load logger handlers or not. Defaults to False.
76+ config (LoggerConfigPM | dict | None], optional): New logger config to update loaded config.
77+ Defaults to None.
78+ config_file_path (str , optional): Logger config file path. Defaults to
79+ `LoggerLoader._CONFIG_FILE_PATH`.
80+ auto_config_file (bool , optional): Indicates whether to load logger config
81+ file or not. Defaults to True.
82+ auto_load (bool , optional): Indicates whether to load logger
83+ handlers or not. Defaults to False.
8484 """
8585
8686 self .handlers_map = {"default" : 0 }
@@ -125,9 +125,7 @@ def load(self) -> Logger:
125125 return logger
126126
127127 @validate_call
128- def remove_handler (
129- self , handler : Union [str , None ] = None , handler_type : str = "NAME"
130- ):
128+ def remove_handler (self , handler : str | None = None , handler_type : str = "NAME" ):
131129 """Remove all handlers or specific handler by name or id from logger.
132130
133131 Raises:
@@ -162,7 +160,7 @@ def remove_handler(
162160 self .handlers_map .clear ()
163161
164162 @validate_call
165- def update_config (self , config : Union [ LoggerConfigPM , Dict [str , Any ] ]):
163+ def update_config (self , config : LoggerConfigPM | dict [str , Any ]):
166164 """Update logger config with new config.
167165
168166 Args:
@@ -213,13 +211,11 @@ def _load_config_file(self):
213211 # elif self.config_file_path.lower().endswith(".toml"):
214212 # _file_format = "TOML"
215213
216- ## Loading config from file, if it's exits:
214+ # Loading config from file, if it's exits:
217215 if os .path .isfile (self .config_file_path ):
218216 if _file_format == "YAML" :
219217 try :
220- with open (
221- self .config_file_path , "r" , encoding = "utf-8"
222- ) as _config_file :
218+ with open (self .config_file_path , encoding = "utf-8" ) as _config_file :
223219 _new_config_dict = yaml .safe_load (_config_file ) or {}
224220 if "logger" not in _new_config_dict :
225221 logger .warning (
@@ -242,9 +238,7 @@ def _load_config_file(self):
242238 raise
243239 elif _file_format == "JSON" :
244240 try :
245- with open (
246- self .config_file_path , "r" , encoding = "utf-8"
247- ) as _config_file :
241+ with open (self .config_file_path , encoding = "utf-8" ) as _config_file :
248242 _new_config_dict = json .load (_config_file ) or {}
249243 if "logger" not in _new_config_dict :
250244 logger .warning (
@@ -296,7 +290,7 @@ def _load_config_file(self):
296290 def _check_env (self ):
297291 """Check environment variables for logger config."""
298292
299- ## Checking environment for DEBUG option:
293+ # Checking environment for DEBUG option:
300294 _is_debug = False
301295 _ENV = str (os .getenv ("ENV" )).strip ().lower ()
302296 _DEBUG = str (os .getenv ("DEBUG" )).strip ().lower ()
@@ -314,7 +308,7 @@ def _check_env(self):
314308 self .config .file .logs_dir = os .getenv ("BEANS_LOGGING_LOGS_DIR" )
315309
316310 # if self.config.stream.use_color:
317- # ## Checking terminal could support xterm colors:
311+ # # Checking terminal could support xterm colors:
318312 # _TERM = str(os.getenv("TERM")).strip()
319313 # if not "xterm" in _TERM:
320314 # self.config.stream.use_color = False
@@ -536,7 +530,7 @@ def _load_intercept_handlers(self):
536530
537531 _intercept_handler = InterceptHandler ()
538532
539- ## Intercepting all logs from standard (root logger) logging:
533+ # Intercepting all logs from standard (root logger) logging:
540534 logging .basicConfig (handlers = [_intercept_handler ], level = 0 , force = True )
541535
542536 _intercepted_modules = set ()
@@ -579,10 +573,10 @@ def _load_intercept_handlers(self):
579573 f"Intercepted modules: { list (_intercepted_modules )} ; Muted modules: { list (_muted_modules )} ;"
580574 )
581575
582- ### ATTRIBUTES ## #
583- ## handlers_map ##
576+ # ATTRIBUTES #
577+ # handlers_map
584578 @property
585- def handlers_map (self ) -> Dict [str , int ]:
579+ def handlers_map (self ) -> dict [str , int ]:
586580 try :
587581 return self .__handlers_map
588582 except AttributeError :
@@ -591,17 +585,17 @@ def handlers_map(self) -> Dict[str, int]:
591585 return self .__handlers_map
592586
593587 @handlers_map .setter
594- def handlers_map (self , handlers_map : Dict [str , int ]):
588+ def handlers_map (self , handlers_map : dict [str , int ]):
595589 if not isinstance (handlers_map , dict ):
596590 raise TypeError (
597591 f"`handlers_map` attribute type { type (handlers_map )} is invalid, must be <dict>!."
598592 )
599593
600594 self .__handlers_map = copy .deepcopy (handlers_map )
601595
602- ## handlers_map ##
596+ # handlers_map
603597
604- ## config ##
598+ # config
605599 @property
606600 def config (self ) -> LoggerConfigPM :
607601 try :
@@ -620,9 +614,9 @@ def config(self, config: LoggerConfigPM):
620614
621615 self .__config = copy .deepcopy (config )
622616
623- ## config ##
617+ # config
624618
625- ## config_file_path ##
619+ # config_file_path
626620 @property
627621 def config_file_path (self ) -> str :
628622 try :
@@ -648,17 +642,19 @@ def config_file_path(self, config_file_path: str):
648642 ):
649643 if not config_file_path .lower ().endswith (".toml" ):
650644 raise NotImplementedError (
651- f"`config_file_path` attribute value '{ config_file_path } ' is invalid, TOML file format is not supported yet!"
645+ f"`config_file_path` attribute value '{ config_file_path } ' is invalid, "
646+ f"TOML file format is not supported yet!"
652647 )
653648
654649 raise ValueError (
655- f"`config_file_path` attribute value '{ config_file_path } ' is invalid, file must be '.yml', '.yaml' or '.json' format!"
650+ f"`config_file_path` attribute value '{ config_file_path } ' is invalid, "
651+ f"file must be '.yml', '.yaml' or '.json' format!"
656652 )
657653
658654 if not os .path .isabs (config_file_path ):
659655 config_file_path = os .path .join (os .getcwd (), config_file_path )
660656
661657 self .__config_file_path = config_file_path
662658
663- ## config_file_path ##
664- ### ATTRIBUTES ## #
659+ # config_file_path
660+ # ATTRIBUTES #
0 commit comments