首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >logging addHandler(console)

logging addHandler(console)

作者头像
狼啸风云
修改2022-09-03 19:07:30
6040
修改2022-09-03 19:07:30
举报
import logging
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='/temp/myapp.log',
                    filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
# 
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use

#设置格式
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')

# tell the handler to use this format
#告诉handler使用这个格式
console.setFormatter(formatter)

# add the handler to the root logger
#为root logger添加handler
logging.getLogger('').addHandler(console)

# Now, we can log to the root logger, or any other logger. First the root...
#默认使用的是root logger
logging.info('Jackdaws love my big sphinx of quartz.')

# Now, define a couple of other loggers which might represent areas in your
# application:
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')


Output:
----------------------------------------------------------------------
root        : INFO     Jackdaws love my big sphinx of quartz.
myapp.area1 : INFO     How quickly daft jumping zebras vex.
myapp.area2 : WARNING  Jail zesty vixen who grabbed pay from quack.
myapp.area2 : ERROR    The five boxing wizards jump quickly.
----------------------------------------------------------------------
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档