Python中日志的格式化输出

import logging
logfile = ‘e:\\a.txt‘
# logging.basicConfig(filename=logfile,level=logging.INFO)
# logging.basicConfig(format=‘%(time.asctime)s %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘)
logging.basicConfig(level=logging.INFO,
                    #format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, #返回值:Thu, 26 May 2016 15:09:31 t11.py[line:92] INFO
                    format=‘%(asctime)s %(levelname)s %(message)s‘,
                    #datefmt=‘%a, %d %b %Y %H:%M:%S‘,
                    #datefmt=‘%Y/%m/%d %I:%M:%S %p‘, #返回2016/05/26 03:12:56 PM
                    datefmt=‘%Y-%m-%d %H:%M:%S‘, #返回2016/05/26 03:12:56 PM
                    filename=logfile#,
                    #filemode=‘a‘ #默认为a
                   ) 

logging.info(‘username valid passed.\r‘) #logging会自动在每行log后面添加"\000"换行,windows下未自动换行

#logging输出结果:
#2016-05-26 15:22:29 INFO liu1 valid passed.
#2016-05-26 15:22:37 INFO liu1 valid passed.

参考:http://blog.chinaunix.net/uid-26000296-id-4372063.html

     http://www.cnblogs.com/alex3714/articles/5161349.html

日志级别等级:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

format参数中可能用到的格式化串:
%(name)s             Logger的名字
%(levelno)s          数字形式的日志级别
%(levelname)s     文本形式的日志级别
%(pathname)s     调用日志输出函数的模块的完整路径名,可能没有
%(filename)s        调用日志输出函数的模块的文件名
%(module)s          调用日志输出函数的模块名
%(funcName)s     调用日志输出函数的函数名
%(lineno)d           调用日志输出函数的语句所在的代码行
%(created)f          当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d    输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s                字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d                 线程ID。可能没有
%(threadName)s        线程名。可能没有
%(process)d              进程ID。可能没有
%(message)s            用户输出的消息

参考链接:https://www.cnblogs.com/dreamer-fish/p/5460929.html

原文地址:https://www.cnblogs.com/xiohao/p/11027457.html

时间: 2024-08-27 08:55:29

Python中日志的格式化输出的相关文章

python中神奇的格式化输出

python一共有两种格式化输出语法, 一种是类似于C语言printf的方式,称为 Formatting Expression >>> '%s %d-%d' % ('hello', 7, 1) 'hello 7-1' 另一种是类似于C#的方式,称为String Formatting Method Calls >>> '{0} {1}:{2}'.format('hello', '1', '7') 'hello 1:7' 第一种方式可以指定浮点数的精度,例如 >>

18:再议python中的print——格式化输出

如果要在输出结果前面有个提示语句怎么办 18.1 十进制数值输出 %d表示十进制输出,%S表示要输出的变量,他们中间不能有逗号(,). 18.2 字符输出 68以16进制输出(%x)是什么? 68以8进制输出(%o)是什么? 68以2进制输出(%b)是什么? 68以字符输出(%c)是什么?

Python的输入指令、格式化输出、基本运算符

Python的输入指令.格式化输出.基本运算符 Python的输入指令input name = input('Could I know your name please?') 在Python3版本下,输入的所有内容都视为字符串,所以此时name的类型是字符串.如果输入年龄,需要进行转换 age = int(input('Could I know your age please?')) 在Python2版本下,使用input()输入的内容不会被自动转成字符串,所以需要在输入时指定数据类型. 而Py

python中时间日期格式化符号

python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I 12小时制小时数(01-12) %M 分钟数(00=59) %S 秒(00-59) %a 本地简化星期名称 %A 本地完整星期名称 %b 本地简化的月份名称 %B 本地完整的月份名称 %c 本地相应的日期表示和时间表示 %j 年内的一天(001-366) %p 本地A.M.或P.M

python中得到shell命令输出的方法

python中得到shell命令输出的方法: 1. import subprocess output = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE,shell=True).commun icate() print output[0] 2. import commands return_code, output = commands.getstatusoutput('ls -l') 3. import os process = os.p

[ Python入门教程 ] Python中日志记录模块logging使用实例

python中的logging模块用于记录日志.用户可以根据程序实现需要自定义日志输出位置.日志级别以及日志格式. 将日志内容输出到屏幕 一个最简单的logging模块使用样例,直接打印显示日志内容到屏幕. import logging logging.critical("critical log") logging.error("error log") logging.warning("warning log") logging.info(&q

Python 数字系列-数字格式化输出

数字的格式化输出 问题 你需要将数字格式化后输出,并控制数字的位数.对齐.千位分隔符和其他的细节. 解决方案 格式化输出单个数字的时候,可以使用内置的 format() 函数,比如: 1 >>> x = 1234.56789 2 3 >>> # Two decimal places of accuracy 4 >>> format(x, '0.2f') #无空格,小数保留2位 5 '1234.57' 6 7 >>> # Right j

Python笔记---DAY3:格式化输出、for循环、列表操作

1.引号使用: 多行变量用三引号 python单双引号相同,当命令内容中有引号时注意最外层引号的使用 2.格式化输出: name=input("name:") age=int(input("age:")) job=input("job:") salary=input("salary:") number=input("number:") if salary.isdigit(): 判断输入是否为数字,其中.的作

python注释、输入格式化输出输出及数据类型

一.python的注释 1.python中的注释 注释:对代码的解释或者评论,注释仅仅是给人看的,python本身不识别. 2.注释的分类 2.1. 单行注释 # 2.2. 三引号(可以是三个单也可以是三个双) 二.输入 1.python3中的输入 关键字:input() python3中的input获取的用户输入统一全部存成字符串类型 1.2.python2中的输入 1.2.1.input() 需要你人为的声明input输入的是什么数据类型 错误示范: 正确示范:   1.2.2.raw_in