码迷,mamicode.com
首页 > 编程语言 > 详细

Python模块-logging模块(一)

时间:2018-02-15 20:15:29      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:dna   mes   config   完整路径   python模块   serve   模块   路径名   evel   

logging模块用来写日志文件

有5个级别,debug(),info(),warning(),error()和critical(),级别最高的为critical()

debug()为调试模式,info()为正常情况下的信息,warning()为警告,error()为错误,critical()为严重问题

普通的打印

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.debug(‘The debug‘)
logging.info(‘The info‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

运行结果

技术分享图片

5个级别中只打印了警告级别及之后的语句

写入日志文件

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(filename=‘log_test.log‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

level=loggin.INFO,把日志级别设置为INFO,只有INFO或比INFO级别更高的日志才会被写到日志文件里

写入日志文件的内容

技术分享图片

只从info级别开始写入内容,debug级别比info低,没有写入

日志格式化

%(name)s        日志写入者的名称

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(name)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

运行结果

技术分享图片

%(levelno)s   数字形式的日志级别,%(levelname)s   文本形式的日志级别

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(levelno)s,%(levelname)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

运行结果

技术分享图片

%(lineno)d      调用日志输出函数的语句所在的代码行

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(lineno)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

运行结果

技术分享图片

%(pathname)s    调用日志输出函数的模块的完整路径名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(pathname)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

%(filename)s    调用日志输出函数的模块的文件名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(filename)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

%(module)s      调用日志输出函数的模块名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(module)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

就是文件名,没有后缀

%(funcName)s    调用日志输出函数的函数名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(funcName)s‘,level=logging.INFO)

def log_test():
    logging.debug(‘This python is running‘)
    logging.info(‘The infomation of python‘)
    logging.warning(‘Request outtime‘)
    logging.error(‘The python run error‘)
    logging.critical(‘The server down‘)

log_test()

 运行结果

技术分享图片

%(created)f     当前时间,用UNIX标准的表示时间的浮点数表示

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(created)f‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

%(relativeCreated)d   输出日志信息自创建以来的毫秒数

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging
import time

logging.basicConfig(format=‘%(relativeCreated)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
time.sleep(2)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

结果是不定的

%(asctime)s  字符串形式的当前时间,默认格式为“2018-02-15 17:59:31,546”,逗号后面的是毫秒

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(asctime)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

运行结果

技术分享图片

%(thread)d  当前线程号,%(threadName)s  当前线程名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(thread)d,%(threadName)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

%(process)d      进程号

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(process)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

%(message)s     用户输出的消息

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(message)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

格式化的日志写入

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(filename=‘log_test.log‘,level=logging.INFO,format=‘%(message)s‘,datefmt=‘%m‘)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

 运行结果

技术分享图片

 

Python模块-logging模块(一)

标签:dna   mes   config   完整路径   python模块   serve   模块   路径名   evel   

原文地址:https://www.cnblogs.com/sch01ar/p/8449752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!