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

python logging 学习笔记

时间:2014-12-11 10:06:31      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   sp   for   on   

https://docs.python.org/3/howto/logging.html#logging-basic-tutorial

更多更好的例子:

https://docs.python.org/3/howto/logging-cookbook.html#logging-cookbook

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
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger(‘‘).addHandler(console)

# Now, we can log to the root logger, or any other logger. First the root...
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.)

When you run this, on the console you will see

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.

and in the file you will see something like

10-22 22:19 root         INFO     Jackdaws love my big sphinx of quartz.
10-22 22:19 myapp.area1  DEBUG    Quick zephyrs blow, vexing daft Jim.
10-22 22:19 myapp.area1  INFO     How quickly daft jumping zebras vex.
10-22 22:19 myapp.area2  WARNING  Jail zesty vixen who grabbed pay from quack.
10-22 22:19 myapp.area2  ERROR    The five boxing wizards jump quickly.

 

python logging 学习笔记

标签:style   blog   http   io   ar   color   sp   for   on   

原文地址:http://www.cnblogs.com/snow-backup/p/4156908.html

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