python中的日志模块logging
1、日志级别5个:
警告Warning 一般信息Info 调试 Debug 错误Error 致命Critical
2、禁用日志方法
logging.disable(logging.DEBUG)
3、将日志写入文件
logging.basicConfig(filename='log.txt', level=logging.CRITICAL,
format=' %(asctime)s - %(levelname)s - %(message)s')
4、格式化输出日志信息
注意事项:
1、日志输出的文件时,涉及写入日志文件的日志配置应该放到日志配置的首行,否则会受到前面日志配置的干扰。
#!/usr/bin/env python
# coding=utf-8 import logging
import os logging.basicConfig(filename='log.txt', level=logging.CRITICAL,
format=' %(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') # 禁用日志模块的调试信息
# logging.disable(logging.DEBUG) logging.debug('Start of program') # 判断日志文件是否存在
if os.path.exists('log.txt'):
print "Have found the log.txt"
logging.critical("good")
else:
logging.critical("/home/log.txt is not existed!!!") def factorial(n):
logging.debug('Start of factorial(%s%%)' % (n))
total = 1
for i in range(1, n + 1):
total *= i
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.debug('End of factorial(%s%%)' % (n))
return total print(factorial(5))
logging.debug('End of program')
python中的日志模块logging的更多相关文章
- Python中的日志管理Logging模块
1.基本的用法 import logging logging.debug('This is debug message') logging.info('This is info message') l ...
- python 重要的日志模块logging
一,logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- python重要的日志模块logging
一,logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 1.可以通过设置 ...
- python基础:日志模块logging,nnlog
python里面用来打印日志的模块,就是logging模块,logging模块可以在控制台打印日志,也可以写入文件中.也可以两个操作都执行 1.控制台输入 import logging#导入模块 lo ...
- Python强大的日志模块logging
前言 日志是对于软件执行所发生的事件的一种追踪记录方式.日常使用过程中对代码执行的错误和问题会进行查看日志来分析定位问题所在.平常编写代码以及调试也经常用到.通常的新手的做法是直接print打印,但是 ...
- 【python】【logging】python日志模块logging常用功能
logging模块:应用程序的灵活事件日志系统,可以打印并自定义日志内容 logging.getLogger 创建一个log对象 >>> log1=logging.getLogger ...
- 『无为则无心』Python日志 — 64、Python日志模块logging介绍
目录 1.日志的作用 2.为什么需要写日志 3.Python中的日志处理 (1)logging模块介绍 (2)logging模块的四大组件 (3)logging日志级别 1.日志的作用 从事与软件相关 ...
- python日志模块logging
python日志模块logging 1. 基础用法 python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.INFO.WARNING.ERROR.CRITICAL五种( ...
- 日志模块logging使用心得
在应用程序使用中,日志输出对应用维护人员.开发人员判断程序的问题起重要作用. 那么在python中如何定义程序的日志输出? 推荐使用日志模块logging 需求:实现日志内容输出在文件中和控制器中 i ...
随机推荐
- uva-10167-枚举
题意:生日蛋糕上面有2N草莓,怎么切能够将蛋糕和草莓平分成俩份,直接枚举,A和B,草莓不能落在直线上 #include <iostream> #include <stdio.h> ...
- 练手nginx反向代理和负载均衡apache实战
先说下原理性的 什么是反向代理 用户访问域名 域名的指向到nginx nginx把请求转发到apache apache处理后 返回给用户 整套的逻辑 对于用户来说 就是访问域名 然后返回 没 ...
- leetcode268
public class Solution { public int MissingNumber(int[] nums) { var list = nums.OrderBy(x => x).To ...
- 奇技淫巧:在spring官网上下载历史版本的spring插件,springsource-tool-suite
转自:https://blog.csdn.net/PacosonSWJTU/article/details/80959689 目前spring官网(http://spring.io/tools/sts ...
- Django之 Model Field Options
以下这些选项都是可选择的,非固定要求. 1)null,注意在CharField或者TextField里避免使用null,因为其存储的值是空字符串而不是NULL 2)blank该字段是否可以为空.如果为 ...
- python classmethod 和 staticmethod的区别
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner 1. ...
- 设计模式学习笔记(1)Iterator
Iterator 模式 public interface Iterator { public boolean hasNext(); public Object next(); } public int ...
- whlie and for
public class TestWhileAndFor { /**测试 while和for循环练习 * 100 以内的奇数和偶数的和 * @author Administrator * */ pub ...
- fm 讲解加代码
转自: 博客 http://blog.csdn.net/google19890102/article/details/45532745/ github https://github.com/zhaoz ...
- poi转geohash
import geohashimport sysfor line in sys.stdin: fields = line.strip().split('\t') hostid,POS_TIME,POS ...