1 ImportOS2Base_dir=os.path.dirname (Os.path.dirname (__file__))3Base_db=os.path.join (Base_dir,'DB')4Base_log=os.path.join (Base_dir,'Log')5 #define three types of log output formats6standard_format='[% (asctime) s] [% (ThreadName) s:% (thread) d][task_id:% (name) s][% (filename) s:% (Lineno) d]'7 '[% (LevelName) s] [% (message) s]' #where name is the name specified by GetLogger8Simple_format ='[% (LevelName) s] [% (asctime) s][% (filename) s:% (Lineno) d]% (message) s'9Id_simple_format ='[% (LevelName) s] [% (asctime) s]% (message) s'Ten OneLogfile_dir = Os.path.abspath (Os.path.dirname (Os.path.dirname (__file__)))#Directory of log files ALogfile_dir = logfile_dir+'/log' -Logfile_name ='Shopping.log' #log file name - the #if the defined log directory does not exist, create a - if notOs.path.isdir (logfile_dir): - Os.mkdir (Logfile_dir) - + #full path of the log file -Logfile_path =Os.path.join (Logfile_dir, Logfile_name) + A #Log Configuration Dictionary atLogging_dic = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - ' Standard': { - 'format': Standard_format in }, - ' Simple': { to 'format': Simple_format + }, - }, the 'Filters': {}, * 'handlers': { $ #Print to the terminal's logPanax Notoginseng 'Console': { - ' Level':'DEBUG', the 'class':'logging. Streamhandler',#Print to screen + 'Formatter':' Simple' A }, the #Print to file log, collect info and above logs + 'default': { - ' Level':'DEBUG', $ 'class':'Logging.handlers.RotatingFileHandler',#Save to file $ 'Formatter':' Standard', - 'filename': Logfile_path,#log File - 'MaxBytes': 1024*1024*5,#Log size 5M the 'Backupcount': 5, - 'encoding':'Utf-8',#log file encoding, no longer worry about the Chinese log garbledWuyi }, the }, - 'Loggers': { Wu #Logging.getlogger (__name__) Get the logger configuration - "': { About 'handlers': ['default','Console'],#This adds the two handler defined above, that is, the log data is written to the file and printed to the screen $ ' Level':'DEBUG', - 'Propagate': True,#upward (higher level of logger) pass - }, - }, A}
Output:
[2018-07-28 13:19:37,991] [mainthread:43136] [Task_id:bank] [Bank.py:31] [INFO] [Account Alex repayment 1 yuan success]
[2018-07-28 13:25:38,842] [mainthread:41504] [Task_id:user] [User.py:20] [INFO] [Account Karen registered successfully, the initial amount of 15000 yuan]
[2018-07-28 13:26:25,185] [mainthread:41852] [Task_id:user] [User.py:28] [INFO] [Account Karen Landing successful, the remaining amount of 15000 yuan]
[2018-07-28 13:26:36,917] [mainthread:41852] [Task_id:bank] [Bank.py:18] [INFO] [Account Karen successfully transfers $1000 to account Andy]
[2018-07-28 13:26:41,246] [mainthread:41852] [Task_id:bank] [Bank.py:31] [INFO] [Account Karen Repayment 2000 Yuan success]
[2018-07-28 13:26:45,665] [mainthread:41852] [Task_id:bank] [Bank.py:42] [INFO] [Account Karen withdrawals 1000 success, handling fee 50.0 Yuan]
[2018-07-28 13:26:52,368] [mainthread:41852] [Task_id:shop] [Shop.py:15] [INFO] [Account Karen Purchase {' IPhone ': {' price ': 8800, ' Count ': 1}} succeeded]
Python Universal Log Module