今天是python的logging库,这是一个典型的日志库,用法简单实在。html
首先是最简单的python
import logging logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 输出 # this is warning # this is error # this is critical # 之因此这里没有前两条是由于logging默认的日志级别是warning,只有大于等于这个日志级别的日志才会输出 # 这边须要从新键入代码,不然前面的日志库的对象是不会被清空的,会出现仍是输出上面3条,要从新载入IPython # 这时候咱们才会有以下的结果 import logging logging.basicConfig(level=logging.DEBUG) logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 输出 # this is debug # this is info # this is warning # this is error # this is critical
接下来是logging中basicConfig的一些参数使用api
# filename 输出到文件的名字 # filemode 输出文件的格式,a,w,简单的两个 # format 基本语句的格式 # datefmt 日期的格式 # level 日志的级别 # stream 初始化的流 import logging logging.basicConfig(filename="first.log",level=logging.DEBUG,filemode="a",format="%(asctime)s %(name)s %(levelname)s %(message)s",datefmt="%Y/%m/%d %H:%M:%S") # format解释,如下为几个经常使用的format的关键字 %(key_words)s 都是基于这样形式包裹的 # asctime 时间 # created 毫秒值 # filename 日志在的地方 # funcName 日志所在函数的名字 # levelname 日志的级别 # levelno 日志的级别,用数字表示 # lineno 日志所在的行(运行文件的位置) # module 模块名字 # msecs 毫秒值 # message 信息 # name 当前logger的名字 # pathname 日志运行文件的全路径 # process 进程id # processName 进程名字(若是有的话) # thread 线程id # threadName 线程名字 # datefmt简单如下几个,须要更复杂的查python api去 # %Y 年, %m 月, %d 日 ,%H 小时, %M 分钟 %S 秒 logging.debug("this is debug") logging.info("this is info") logging.warning("this is warning") logging.error("this is error") logging.critical("this is critical") # 结果出如今first.log上 # 2017/02/21 15:58:13 root DEBUG this is debug # 2017/02/21 15:58:13 root INFO this is info # 2017/02/21 15:58:13 root WARNING this is warning # 2017/02/21 15:58:13 root ERROR this is error # 2017/02/21 15:58:13 root CRITICAL this is critical
接下来就是稍微复杂的logger,handler,formatter函数
import logging logger = logging.getLogger("sample") # 注意全部的handler都要和logger的日志级别相同,不然会出现冗余现象哦,即打印一条出现不少条 logger.setLevel(logging.DEBUG) sh = logging.StreamHandler() # 控制台输出流的handler sh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(name) %(asctime)s %(levelname)s %(message)s") sh.setFormatter(formatter) fh = logging.FileHandler("sample.log",mode="a",encoding="utf-8",delay=0.5) # 文件流handler fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s %(asctime)s %(message)s") fh.setFormatter(formatter) logger.addHandler(sh) # 一个logger能够有多个的handler logger.addHandler(fh) logger.debug("this is debug") logger.info("this is info") logger.error("this is error") logger.warning("this is warning") logger.critical("this is critical") # 输出 # sample 2017-02-21 15:53:51,546 DEBUG this is debug # sample 2017-02-21 15:53:51,550 INFO this is info # sample 2017-02-21 15:53:51,552 ERROR this is error # sample 2017-02-21 15:53:51,553 WARNING this is warning # sample 2017-02-21 15:53:51,555 CRITICAL this is critical
最后的更复杂的日志,就本身查看官方文档了,例如日志的配置仍是回滚日志等等,都很简单this