Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14215

low level of logging in python does not work

$
0
0

The question was solved by changing tz = get_localzone() to tz = pytz.utc

I am trying to do logging in Python. I need to swap the local time zone to the New York timezone. class CustomFormatter was developed to do so. When the logging level is INFO or higher, it works fine, but when it's DEBUG it does not work.

import loggingfrom datetime import datetimefrom tzlocal import get_localzoneimport pytz# class CustomFormatter is to change timezoneclass CustomFormatter(logging.Formatter):    def converter(self, timestamp):        dt = datetime.fromtimestamp(timestamp, tz=get_localzone())        return dt.astimezone(pytz.timezone('America/New_York'))    def formatTime(self, record, datefmt=None):        dt = self.converter(record.created)        if datefmt:            s = dt.strftime(datefmt)        else:            try:                s = dt.isoformat(timespec='milliseconds')            except TypeError:                s = dt.isoformat()        return sdef logging_config(log_filename, log_level,                    log_format = '%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s',                    date_format='%Y%m%d_%H:%M:%S'):    # Create a logger object    logger = logging.getLogger()    # Configure the logging level    logger.setLevel(log_level)    # Create a file handler    file_handler = logging.FileHandler(log_filename)    # Create a stream handler (to display logs in the notebook)    stream_handler = logging.StreamHandler()    # Create the custom formatter    formatter = CustomFormatter(log_format, datefmt=date_format)    # Set the formatter for both handlers    file_handler.setFormatter(formatter)    stream_handler.setFormatter(formatter)    # Set the logging level for both handlers    file_handler.setLevel(log_level)    stream_handler.setLevel(log_level)    # Add both handlers to the logger    logger.addHandler(file_handler)    logger.addHandler(stream_handler)    return loggerdef logging_test(logger):    # Log messages    logger.debug('This is a debug message')    logger.info('This is an info message')    logger.warning('This is a warning message')    logger.error('This is an error message')    logger.critical('This is a critical message')    return 0if __name__ == '__main__':    log_filename = './log_test.log'    print('log_filename*', log_filename)    log_format = '%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s'    logger = logging_config(log_filename = log_filename,                            log_format = log_format,                                                          log_level = logging.DEBUG, #INFO,                            date_format="%Y%m%d_%H%M%S")    logging_test(logger)

When I use log_level = logging.DEBUG it does not work for me and produces only:

$ ipython local_functions.pylog_filename* ./log_test.log(base)

when I use logging.INFO or higher levels it works as expected:

log_filename* ./log_test.log20240104_211453.256 - INFO - This is an info message20240104_211453.261 - WARNING - This is a warning message20240104_211453.261 - ERROR - This is an error message20240104_211453.262 - CRITICAL - This is a critical message

What might be the problem?


Viewing all articles
Browse latest Browse all 14215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>