0
点赞
收藏
分享

微信扫一扫

python笔记6--日志输出


python笔记6--日志输出

  • ​​python笔记6--日志输出​​
  • ​​功能​​
  • ​​代码​​
  • ​​说明​​

python笔记6–日志输出

功能

  1. 输出基本log信息
  2. 根据level级别输出log信息,方法1
  3. 根据level级别输出log信息,方法2
  4. 将log信息写到文本中
  5. 将log信息写到文本中,同时输出到终端
  6. 将日志输入到/var/log/syslog中

代码

#!/usr/bin/python3

import logging
import sys
import syslog


def test_logging():
'''
func: 输出基本log信息
'''
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')


def test_logging2(level=logging.INFO):
"""
根据level等级进行输出,方法1
"""
# set default logging configuration
logger = logging.getLogger() # initialize logging class
logger.setLevel(level) # default log level
format = logging.Formatter("%(asctime)s [%(levelname)-8s][%(processName)s:%(process)d-%(threadName)s][%(filename)s:%(lineno)d][%(message)s]") # output format
sh = logging.StreamHandler(stream=sys.stdout) # output to standard output
sh.setFormatter(format)
logger.addHandler(sh)
# use logging to generate log ouput
logger.info("this is info")
logger.debug("this is debug")
logger.warning("this is warning")
logging.error("this is error")
logger.critical("this is critical")


def test_logging3(level=logging.INFO):
"""
根据level等级进行输出,方法2
"""
logging.basicConfig(level=level, format='%(asctime)s [%(levelname)-8s][%(processName)s:%(process)d-%(threadName)s][%(filename)s:%(lineno)d][%(message)s]')
logger = logging.getLogger()
logger.info("this is info")
logger.debug("this is debug")
logger.warning("this is warning")
logging.error("this is error")
logger.critical("this is critical")


def test_logging4(level=logging.INFO):
"""
将log信息输入到test.log文件中
"""
logger = logging.getLogger()
logger.setLevel(level)
format = logging.Formatter('%(asctime)s [%(levelname)-8s][%(processName)s:%(process)d-%(threadName)s][%(filename)s:%(lineno)d][%(message)s]')
hander=logging.FileHandler('test.log')
hander.setFormatter(format)
logger.addHandler(hander)
logger.info("this is info")
logger.debug("this is debug")
logger.warning("this is warning")
logging.error("this is error")
logger.critical("this is critical")


def test_logging5(level=logging.INFO):
"""
将log信息输入到test.log文件中,同时输出到控制台
"""
logger = syslog.S
logger.setLevel(level)
format = logging.Formatter('%(asctime)s [%(levelname)-8s][%(processName)s:%(process)d-%(threadName)s][%(filename)s:%(lineno)d][%(message)s]')

hander=logging.FileHandler('test.log')
hander.setFormatter(format)
logger.addHandler(hander)

console=logging.StreamHandler()
console.setLevel(level)
logger.addHandler(console)

logger.debug("this is debug")
logger.info("this is info")
logger.warning("this is warning")
logging.error("this is error")
logger.critical("this is critical")


def test_logging6():
"""
将log信息输入到syslog中
"""
syslog.syslog('This is test of syslog')


if __name__ == '__main__':
# test_logging()
# test_logging2(logging.INFO)
# test_logging3(logging.DEBUG)
# test_logging4(logging.DEBUG)
# test_logging5(logging.DEBUG)
test_logging6()

注意:
若果需要显示具体行等信息,可以通过 traceback.print_exc() 来实现

import traceback
print(traceback.print_exc())

说明

笔者测试系统为Ubuntu1910,Python3.7.


举报

相关推荐

0 条评论