79359077

Date: 2025-01-15 16:58:48
Score: 0.5
Natty:
Report link

With the inputs received in the comment, I post the updated code with time rotating logger:

import logging  
import logging.handlers import TimeRotatingFileHandler


def show_help():
    'This is help messages'
    help_msg = '''\n
    The valid options:\n
    [-src_path : <option to provide src_path ]
    [-dest_path : <option to provide dest_path ]
    
    '''
    print(help_msg)
    

def define_logger():
    log_path = "/usr/scripts/logs/my_script.log"
    # Create a custom logger
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    # Create a TimeRotatingFileHandler
    f_handler = TimeRotatingFileHandler(log_path, when="midnight", interval-1, backupCount=7)
    f_handler.setLevel(logging.DEBUG)

    #Create a console handler
    con_handler = logging.StreamHandler()
    con_handler.setLevel(logging.DEBUG)

    #Create a formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    f_handler.setFormatter(formatter)
    con_handler.setFormatter(formatter)         

    # Add the handler to the logger
    logger.addHandler(con_handler)
    logger.addHandler(f_handler)

    return logger, con_handler
    
    
    
if __name__ == '__main__':
    logger, con_handler = define_logger()
    logger.info("simply using logger")
Reasons:
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: A.G.Progm.Enthusiast