Skip to content

Instantly share code, notes, and snippets.

@finghine
Last active May 28, 2016 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finghine/e7bc4f2be49bfde4e1f798a15de51804 to your computer and use it in GitHub Desktop.
Save finghine/e7bc4f2be49bfde4e1f798a15de51804 to your computer and use it in GitHub Desktop.
python log use
#encoding:utf-8
import logging
# 创建一个logger
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')
fh.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(funcName)15s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)
# 记录一条日志
logger.info('foorbar')
def testfun():
logger.info("funting info")
testfun()
logger.debug("debug message")
logger.info("info message")
logger.info("warn message")
logger.error("error message")
logger.critical("critical message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment