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

python中logging日志基本用法

时间:2019-01-08 21:56:04      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:exist   odi   _id   .config   ==   port   owb   _for   max   

低配版

import logging

logging.debug(debug message)    # 调试模式
logging.info(info message)    # 正常运转模式
logging.warning(warning message)  # 警告模式
logging.error(error message)      # 错误模式
logging.critical(critical message)    # 致命的 崩溃模式

while 1:
    try:
        num = input(>>>)
        num = int(num)

    except ValueError:
        logging.warning(输入非数字警告)

标准版

import logging
# 1.产生logger对象
logger = logging.getLogger()

# 2 产生其他对象(屏幕对象,文件对象)
sh = logging.StreamHandler()
fh1 = logging.FileHandler(staff.log, encoding=utf-8)
fh2 = logging.FileHandler(boss.log, encoding=utf-8)

# 3,设置显示格式
formater = logging.Formatter(%(asctime)s - %(name)s - %(levelname)s - %(message)s)
formater1 = logging.Formatter(%(asctime)s - %(name)s - %(levelname)s - %(message)s)
formater2 = logging.Formatter(%(asctime)s - %(name)s - %(levelname)s - %(message)s)

# 4,给对象绑定格式
sh.setFormatter(formater)
fh1.setFormatter(formater1)
fh2.setFormatter(formater2)

# 5 给logger对象绑定其他对象
logger.addHandler(sh)
logger.addHandler(fh1)
logger.addHandler(fh2)

# 6 设置显示级别
# 其他对象的级别要高于logger的级别
logger.setLevel(10)
sh.setLevel(20)
fh1.setLevel(20)
fh2.setLevel(30)


logging.debug(debug message)    # 调试模式
logging.info(info message)    # 正常运转模式
logging.warning(warning message)  # 警告模式
logging.error(error message)      # 错误模式
logging.critical(crit

高配版

import os
import logging.config

# 定义三种日志输出格式 开始

# 标准版 格式
standard_format = [%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]                   [%(levelname)s][%(message)s] #其中name为getlogger指定的名字
# 简单版 格式
simple_format = [%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s

# boss版格式(lowb版)
id_simple_format = [%(levelname)s][%(asctime)s] %(message)s

# 定义日志输出格式 结束

file_path = os.path.dirname(__file__)

logfile_name = file_path + \staff.log  # log文件名

# log配置字典

LOGGING_DIC = {
    version: 1,  # 版本
    disable_existing_loggers: False,  # 可否重复使用之前的logger对象
    formatters: {
        standard: {
            format: standard_format
        },
        simple: {
            format: simple_format
        },
        boss_formatter: {
            format: id_simple_format
        },
    },
    filters: {},
    handlers: {
        #打印到终端的日志
        stream: {
            level: DEBUG,
            class: logging.StreamHandler,  # 打印到屏幕
            formatter: simple
        },
        #打印到文件的日志,收集info及以上的日志  文件句柄
        file: {
            level: 20,
            class: logging.handlers.RotatingFileHandler,  # 保存到文件
            formatter: standard,  # 标准
            filename: logfile_name,  # 日志文件
            maxBytes: 300,  # 日志大小 300 bit
            backupCount: 5,  #轮转文件数
            encoding: utf-8,  # 日志文件的编码,再也不用担心中文log乱码了
        },
    },
    loggers: {
        # logging.getLogger(__name__)拿到的logger配置
        ‘‘: {
            handlers: [stream, file],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            level: DEBUG,  # 总级别
            propagate: True,  # 向上(更高level的logger)传递
        },
    },
}
# 字典中第一层的所有key都是固定不可变的。

logging.config.dictConfig(LOGGING_DIC)
logger = logging.getLogger()  # 这个logger对象是通过自己个性化配置的logger对象
logger.info(运转正常)



# def load_my_logging_cfg():
#     logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
#     logger = logging.getLogger(__name__)  # 生成一个log实例
#     logger.info(‘It works!‘)  # 记录该文件的运行状态
#
# if __name__ == ‘__main__‘:
#     load_my_logging_cfg()

 

python中logging日志基本用法

标签:exist   odi   _id   .config   ==   port   owb   _for   max   

原文地址:https://www.cnblogs.com/chen55555/p/10241526.html

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