# MIT License# Copyright (c) 2025 aeeeeeepimportloggingfromtypingimportOptional,Any,Union# Global flag to force print logs instead of using the loggerglobalFORCEFORCE:bool=False
[docs]defcreate_logger(name:str='objwatch',output:Optional[str]=None,level:Union[int,str]=logging.DEBUG,simple:bool=False)->None:""" Create and configure a logger. Args: name (str): Name of the logger. output (Optional[str]): Path to a file for writing logs. level (Union[int, str]): Logging level (e.g., logging.DEBUG, logging.INFO, "force"). simple (bool): Enable simple logging mode with a basic format. """iflevel=="force":globalFORCE# noqa: F824FORCE=Truereturnlogger=logging.getLogger(name)ifnotlogger.hasHandlers():# Define the log message format based on the simplicity flagifsimple:formatter=logging.Formatter('%(levelname)s: %(message)s')else:formatter=logging.Formatter('[%(asctime)s] [%(levelname)s] %(name)s: %(message)s',datefmt='%Y-%m-%d %H:%M:%S')logger.setLevel(level)# Create and add a stream handler to the loggerstream_handler=logging.StreamHandler()stream_handler.setFormatter(formatter)logger.addHandler(stream_handler)# If an output file is specified, create and add a file handlerifoutput:file_handler=logging.FileHandler(output)file_handler.setFormatter(formatter)logger.addHandler(file_handler)# Prevent log messages from being propagated to the root loggerlogger.propagate=False
# Initialize the logger for 'objwatch'logger=logging.getLogger('objwatch')
[docs]defget_logger()->logging.Logger:""" Retrieve the configured logger. Returns: logging.Logger: The logger instance. """returnlogger
[docs]deflog_info(msg:str,*args:Any,**kwargs:Any)->None:""" Log an informational message or print it if FORCE is enabled. Args: msg (str): The message to log. *args (Any): Variable length argument list. **kwargs (Any): Arbitrary keyword arguments. """globalFORCE# noqa: F824ifFORCE:print(msg,flush=True)else:logger.info(msg,*args,**kwargs)
[docs]deflog_debug(msg:str,*args:Any,**kwargs:Any)->None:""" Log a debug message or print it if FORCE is enabled. Args: msg (str): The message to log. *args (Any): Variable length argument list. **kwargs (Any): Arbitrary keyword arguments. """globalFORCE# noqa: F824ifFORCE:print(msg,flush=True)else:logger.debug(msg,*args,**kwargs)
[docs]deflog_warn(msg:str,*args:Any,**kwargs:Any)->None:""" Log a warning message or print it if FORCE is enabled. Args: msg (str): The message to log. *args (Any): Variable length argument list. **kwargs (Any): Arbitrary keyword arguments. """globalFORCE# noqa: F824ifFORCE:print(msg,flush=True)else:logger.warning(msg,*args,**kwargs)
[docs]deflog_error(msg:str,*args:Any,**kwargs:Any)->None:""" Log an error message or print it if FORCE is enabled. Args: msg (str): The message to log. *args (Any): Variable length argument list. **kwargs (Any): Arbitrary keyword arguments. """globalFORCE# noqa: F824ifFORCE:print(msg,flush=True)else:logger.error(msg,*args,**kwargs)