python打日志 logging用法
import logging
#设置日志输出格式 asctime:时间, filename:文件名, lineno:行号,
# levelname:日志级别, message:日志内容
#datemt=%a星期 %d日期 %b月份 %Y年份 %H:%M:%S时间
logging.basicConfig(
level="DEBUG", #设置数据输出级别
filename='xxx.log', #设置日志输出文件
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a %Y %b %d %H:%M:%S', #设置时间格式
filemode='w' # 默认为a追加的模式,改写为w覆盖模式
)
def process():
logging.warning(e)
logging.warning("connect db ocruser fail")
logging.info(sql)
logging.info("sql:" + sql)
logging.info("user count is " + str(len(user)))
logging.info多个参数值
import logging
logging.basicConfig(
level="DEBUG",
filename='./log/test.log',
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a %Y %b %d %H:%M:%S',
filemode='w'
)
def logprint():
a = "aa"
b = "bb"
c = 10
logging.info(a + b + str(c))
if __name__ == "__main__":
logprint()
log/test.log:
Sun 2021 Jul 11 16:00:57 test.py[line:15] INFO aabb10
vim ./log/test.log