码迷,mamicode.com
首页 > 编程语言 > 详细

Python logger /logging

时间:2018-05-19 19:11:20      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:%s   pass   pre   平台   roc   div   filename   ons   col   

# !/user/bin/python
# -*- coding: utf-8 -*-
‘‘‘
subprocess : 需要在linux平台上测试 shell
logging
‘‘‘

import logging
# 将日志输出在文件里
# logging.basicConfig(filename="app.log", level=logging.DEBUG)
logging.basicConfig(filename="app.log",
                    level=logging.WARNING,
                    format=%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s,
                    datefmt=%m/%d/%Y %I:%M:%S %p)  # 在日志上加上时间. %p代表pm.  TODO 为什么没打出行数?
logging.debug("test debug")
logging.info("test info")
logging.error("test error")
logging.warning("User [alex] attempted wrong password more than 3 times")


# 同时将日志打印在屏幕上并输出在文件里
# step 1, create logger
logger = logging.getLogger("TEST-LOG")
logger.setLevel(logging.DEBUG)

# step2, create console handler and set level to debug
ch=logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# step3, create file handler and set level to warning
fh = logging.FileHandler("process.log")
fh.setLevel(logging.ERROR)

# step3, define format
fh_formatter = logging.Formatter(%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s)
ch_formatter = logging.Formatter(%(asctime)s - %(name)s - %(levelname)s - %(message)s)

fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter)

# step4, connect handlers to logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.warning("ddddd")

 

Python logger /logging

标签:%s   pass   pre   平台   roc   div   filename   ons   col   

原文地址:https://www.cnblogs.com/cheese320/p/9061275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!