[Blog recommendation] logging module of python Howto (1)
This blog post is from Bkjia. If you have any questions, go to the blog page for an interactive discussion!Blog: http://xdzw608.blog.51cto.com/4812210/1608718
This article comes from the understanding of the source code added to the howto-logging section in py2.7.9 docs. The official do
For ease of debugging, the python logging module is encapsulated to support simultaneous output to the console and file, and log file rollback.(1) myloggingconfig. py
View code
# -*-Coding: UTF-8 -*- ''' Created on 2011-8-24Main purpose:PairProgramGeneral configuration of the loggong mode used in@ Author: jerrykwan ''' Import Logging Import
The log system in the python standard library is supported from Python2.3. You only need to use importlogging. I recently wrote a crawler system and needed the python logging module. so I learned about it.
The log system in the python standard library is supported from Python2.3. You only need to import the
In many applications, a log module is used to record key information about the system's operation, so that it can track the health of the system. In the. NET platform, there are well-known third-party open-source log components log4net,c++, with familiar log4cpp, and in Python we don't need a third-party log component because it has provided us with an easy-to-use and powerful logging module:
I recently wrote a crawler system and needed the python logging module. So I learned about it.The log system in the python standard library is supported from Python2.3. You only need to import the logging module. If you want to develop a log system, you need to output logs to the console and write log files as long as
This article mainly introduces the log module logging in Python, including the log level under Python and the use of commonly used methods in the module, friends can refer to the following
A log module is used in many applications to record key information during the operation of the system so that it can track the health of the system. In the. NET platform, the
introduction logging module is a python built-in standard module, mainly used for output log, you can set the level of output logs, log save path, log files and rollback, etc. It can be said that the logging module consists mainly of 4 parts: *logger Logger , which provides interfaces that application code can use directly NBSP;NBSP; *handler process
Python Logging Module TOC
Objective
Design of logging Module
The logical inheritance structure of logger
Log output to multiple I/O devices
Log format and Content rich
The level of logging is also the classification of the log on the level dimension.
You can filter by the
Solution to memory leakage caused by misuse of the logging module in Python
Solution to memory leakage caused by misuse of the logging module in Python
This article mainly introduces how to solve the memory leakage caused by misuse of the logging module in
filter conditionlogger.debug("test ....")logger.info("test info ....")logger.warning("start to run db backup job ....")logger.error("test error ....")A complete example of output to screen, file, with filter at the same timeImport loggingClassIgnorebackuplogfilter(Logging. Filter):"" Ignores logs with DB backup ""DefFilter(Self, record):#固定写法Return"DB Backup"NotIn record. getMessage()#console Handlerch= Logging
Recently modified the logging related functions in the project and used the logging module in the Python standard library to make some records here. Mainly from official documents and StackOverflow to find some of the content.
Official documents
Technical Blog
Basic usageThe following code shows the most basic usage of
modules I use in Python.
Logging Module Introduction
Python's logging module provides a common logging system, and skilled use of logging modules makes it easy for developers to develop third-party modules or their own Python a
Python logging and pythonlogging1. Simply print logs to the screen
Import logginglogging. debug ('this is debug message') logging.info ('this is info message') logging. warning ('this is warning message'): WARNING: root: This is warning message
By default, logging prints logs to the screen at the Log Level of WARNING;T
Python's logging module provides a common logging system that can be easily used by third-party modules or applications. This module provides different log levels and can record logs in different ways, such as files, HTTP get/post,smtp,socket, etc., and can even implement specific logging methods on their own.The logging
#coding: Utf-8import loggingclass Testlog (object): ' Logging ' def __init__ after encapsulation (self, logger = None): ' "Specifies the file path to save the log, the log level, and the call file to deposit the log into the specified file ' # ' creates a logger Self.logger = logging. GetLogger (Logger) self.logger.setLevel (
Simple to use#!/usr/local/bin/python# -*- coding:utf-8 -*-import logginglogging.debug(‘debug message‘)logging.info(‘info message‘)logging.warn(‘warn message‘)logging.error(‘error message‘)logging.critical(‘critical message‘) Output:
WARNING:root:warn messageERROR:root:error messageCRITICAL:root:critical message
By default, the logging module prints the log to the screen (stdout) with a log lev
Reprint: http://www.cnblogs.com/goodhacker/p/3355660.htmlThe log system in the Python standard library is supported from Python2.3. As long as import logging This module can be used. If you want to Develop a log system, you need to output the log to the console and write to the log file, as long as you use:1ImportLogging23#Create a Logger4 Logger = Logging.getlogger (‘MyLogger‘)5Logger.setlevel (
be recorded in the file. In this example, the first log will not be recorded. if you want to record the debug log, you can change the log level to DEBUG.
If you want to print logs on the screen and file logs at the same time, you need to understand a little complicated knowledge:
The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.
Loggers expose the interface that app
Make sure your Python version > Python 2.3 1 from a small case:Cat howto_logging.py #coding=utf8# file name: howto_logging# this file shows how to use logging# made by vasks, email:[emailprotected]import logging# 创建一个logger,级别:DEBUGlogger = logging.getLogger(‘Err_Logger‘)logger.setLevel(logging.DEBUG)# 创建一个handler,用
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.