Gencoding_Ke/Genius3/raw-feature-extractor/log_utils.py

27 lines
722 B
Python
Raw Permalink Normal View History

2024-03-03 14:34:47 +08:00
import logging
import os
def setup_logger(name, log_file, level=logging.INFO):
"""Function setup as many loggers as you want"""
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
# Also add a stream handler for console output
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.addHandler(stream_handler)
# If the file already exists, clear its contents to start fresh
if os.path.exists(log_file):
open(log_file, 'w').close()
return logger