Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.78 KB

File metadata and controls

58 lines (43 loc) · 1.78 KB

log.py

import logging, os, re
from logging.handlers import TimedRotatingFileHandler

BASEPATH = os.path.realpath(os.path.dirname(__file__))
LOGPATH = BASEPATH + os.sep + 'log'
LOGFILE = LOGPATH + os.sep + 'app.log'

# set exist_ok para to True avoid 'FileExistsError: [Errno 17] File exists:' error
os.makedirs(LOGPATH,mode=0o644,exist_ok=True)  

def Write_to_Log():
    log_fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
        # create formatter
    formatter = logging.Formatter(log_fmt)
    formatter.datefmt = '%d/%b/%Y %H:%M:%S'
        
    # create log_file_handler
    log_file_handler = TimedRotatingFileHandler(
                       filename=LOGFILE, when="midnight", interval=1, backupCount=30)
    log_file_handler.suffix = "%Y-%m-%d.log"
    log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}.log$")
    log_file_handler.setFormatter(formatter)
    log_file_handler.setLevel(logging.DEBUG)
    # create logger named log 
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    # add log_file_handler to logger
    log.addHandler(log_file_handler)
    return log

log = Write_to_Log()

log.info('This will get logged')
log.debug('This will get logged')
log.critical('This will get logged')
log.warning('This will get logged')
log.error('This will get logged')

simple logging

logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s] - [%(threadName)5s] - [%(filename)s-line:%(lineno)d] [%(levelname)s] %(message)s',filename='/tmp/agent.log',filemode='a')
logging.debug('This will get logged')
logging.info('This will get logged')

使用配置文件日志

https://python3-cookbook.readthedocs.io/zh_CN/latest/c13/p11_add_logging_to_simple_scripts.html

https://docs.python.org/zh-cn/3/howto/logging.html