ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Python输出日志

Python输出日志

原创 Linux操作系统 作者:jieforest 时间:2012-08-06 13:11:55 0 删除 编辑
Python provides a very powerful logging library in its standard library. A lot of programmers use print statements for debugging (myself included), but you can also use logging to do this. It’s actually cleaner to use logging as you won’t have to go through all your code to remove the print statements. In this tutorial we’ll cover the following topics:

•        Creating a simple logger

•        How to log from multiple modules

•        Log formatting

•        Log configuration

By the end of this tutorial, you should be able to confidently create your own logs for your applications. Let’s get started!

Creating a Simple Logger

Creating a log with the logging module is easy and straight-forward. It’s easiest to just look at a piece of code and then explain it, so here’s some code for you to read:

CODE:

import logging

# add filemode="w" to overwrite
logging.basicConfig(filename="sample.log", level=logging.INFO)

logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/301743/viewspace-739941/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Mongoose介绍
请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    441
  • 访问量
    602347