日志
1. 日志(1)
import logging
logging.basicConfig(level=logging.INFO,filename='runlog.log',filemode='w',
format='%(asctime)s %(filename)s[line:%(lineno)d]%(levelname)s%(message)s')
logging.info('I love my mother')
2. 日志(2)
log.conf 文件:
[loggers]
keys=root,infoLogger
[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler
[logger_infoLogger]
handlers=consoleHandler,fileHandler
qualname=infoLogger
propagate=0
[handlers]
keys=consoleHandler,fileHandler
[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=form01
args=('runlog.log', 'a')
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
[formatter_form02]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
Xx.py
import logging.config
CON_LOG='log.conf'
logging.config.fileConfig(CON_LOG)
logging=logging.getLogger()
logging.info('ok')
3. 自定义日志(3)
# 日志文件
if os.path.isfile('banMa.log'):
with open('banMa.log','w') as f:
f.truncate()
def write_log(str):
rtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
with open('banMa.log','a') as file:
file.write('[%s] %s %s -->%s\n'% (sys._getframe().f_lineno,rtime,__file__,str))