简体   繁体   English

如何在 Python 中显示日志记录统计信息

[英]How to show logging stats in Python

I want to know the summary of all the log in Python using the logging module.我想知道使用日志记录模块的Python中所有日志的摘要。 For example this stat from Scrapy.例如,来自 Scrapy 的统计数据。

{'crawlera/request': 699,
 'crawlera/request/method/GET': 671,
 'crawlera/request/method/POST': 28,
 'crawlera/response': 699,
 'crawlera/response/status/200': 364,
 'crawlera/response/status/301': 335,
 'downloader/request_bytes': 384460,
 'downloader/request_count': 699,
 'downloader/request_method_count/GET': 671,
 'downloader/request_method_count/POST': 28,
 'downloader/response_bytes': 3804333,
 'downloader/response_count': 699,
 'downloader/response_status_count/200': 364,
 'downloader/response_status_count/301': 335,
 'elapsed_time_seconds': 991.665077,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2020, 5, 5, 13, 50, 33, 105097),
 'item_scraped_count': 335,
 'log_count/DEBUG': 1036,
 'log_count/INFO': 46,
 'request_depth_max': 2,
 'response_received_count': 364,
 'scheduler/dequeued': 699,
 'scheduler/dequeued/memory': 699,
 'scheduler/enqueued': 699,
 'scheduler/enqueued/memory': 699,
 'start_time': datetime.datetime(2020, 5, 5, 13, 34, 1, 440020)}

How can I do that?我怎样才能做到这一点?

Tested on Python 3.6v在 Python 3.6v 上测试

This is one way you can create logs.这是您可以创建日志的一种方式。

default log level is 0默认日志级别为 0

LEVEL VALUE等级价值

NonSet = 0未设置 = 0

DEBUG = 10调试 = 10

INFO = 20信息 = 20

WARNING = 30警告 = 30

ERROR = 40错误 = 40

CRITICAL= 50关键= 50

Set Log level using variable logger_level as per your need.根据需要使用变量 logger_level 设置日志级别。

import logging 

def initiateLogger(logger_level):
    # create and configure logger
    file_name = r'logs.log'
    logging.basicConfig(filename=file_name, format='%(asctime)s  %(levelname)s--%(message)s', filemode='w')

    # Creating an object
    logger = logging.getLogger()

    # set logging level
    logger.setLevel(logger_level)
    logger.info(f'logging level:{logger_level}')

#call func
initiateLogger('INFO')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM