The log file path is changed to an absolute path during initialization to avoid log writing failures caused by errors in win32Services when pythonServices is used instead of the. Py file.

Use: import myloger.py

#logger = logging.getLogger()

#logger.info(‘aaaaaaq’)

# or import logging

logging.info(‘5555555555’)

Log. Yaml configuration file:

# version must be 1 # logger configuration file # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # logging yaml content ############################################# version: 1 disable_existing_loggers: True formatters: simple: format: "%(asctime)s - %(filename)s - %(levelname)s - %(message)s" simple1: format: "%(asctime)s - %(thread)d - [%(levelname)s] %(pathname)s - %(module)s - %(funcName)s: %(message)s" #filters: handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout info_file_timedRotatingFileHandler: class: logging.handlers.TimedRotatingFileHandler level: INFO formatter: simple filename: info.log when: 'midnight' backupCount: 7 encoding: utf8 error_file_timedRotatingFileHandler: class: logging.handlers.TimedRotatingFileHandler level: ERROR formatter: simple filename: error.log when: 'midnight' backupCount: 7 encoding: utf8 warn_file_timedRotatingFileHandler: class: logging.handlers.TimedRotatingFileHandler level: WARN formatter: simple filename: warn.log when: 'midnight' backupCount: 7 encoding: utf8 loggers: console: level: DEBUG handlers: [console,warn_file_timedRotatingFileHandler] propagate: 0 # The default is 1, which means that the message is passed to the parent logger's handler for processing qualname: The qualname option is read when logging.getLogger() is used in CSL # code. Thread: False handlers: thread: current: info thread: False [console,info_file_timedRotatingFileHandler] warn: #logger = logging.getLogger('warn') level: WARNING handlers: [console,warn_file_timedRotatingFileHandler] propagate: 0 # Default is 1, propagate the message to the parent logger's handler for processing qualName: cslinfo error: #logger = logging.getLogger('error') level: ERROR handlers: [console,error_file_timedRotatingFileHandler] propagate: Qualname: cslerror root: # Direct logging.info('atest') False handlers: [console,info_file_timedRotatingFileHandler]Copy the code

2, myloger. Py

import yaml import logging.config import os #import inspect def setup_logging(yamlcfg_path='./config/log.yaml', default_level=logging.INFO): "" Setup Logging configuration "" # Change to absolute path to avoid error in win32Services when pythonServices is used instead of the.py file path. Cur_path =os.path.abspath(os.curdir)# How to call pythonServices in Win32Servers logf_file=os.path.join(cur_path,'logs','info.log') log_path=os.path.abspath(os.path.split(logf_file)[0]) this_file = inspect.getfile(inspect.currentframe()) log_path = os.path.abspath(os.path.dirname(this_file))''' log_path1 = os.path.dirname(os.path.abspath(__file__)) log_path=os.path.join(log_path1,'logs') print(log_path) if os.path.exists(log_path): pass else: os.makedirs(log_path) path = yamlcfg_path if os.path.exists(path): with open(path, 'rt',encoding='utf-8') as f: config = yaml.safe_load(f.read()) hdl=config['handlers'] #print(hdl['console']) for k,v in hdl.items(): if isinstance(v,dict): #print(k+':') if 'filename' in config['handlers'][k].keys(): fname=config['handlers'][k]['filename'] #fname.replace('\\','/') fname=fname.lstrip('./').lstrip('.\\') config['handlers'][k]['filename']=os.path.join(log_path,fname) #print(config['handlers'][k]['filename']) logging.config.dictConfig(config) else: logging.basicConfig(level=default_level) print('the input yaml path doesn\'t exist') #if __name__ == '__main__': Curr_path =os.path. dirname(os.path.abspath(__file__)) cfg_path=os.path.join(curr_path,'config/logging.yaml') Setup_logging (yamlcfg_path=cfg_path) # obtain the current folder path #os.path.dirname(os.path.abspath(__file__)) #logging.info('55555555 ') #logging.info('55555555 ') #logging.info(' aaaaAAq ') #logging.info('55555555 ')Copy the code