Python3 Adds Logging Level

I have this code that works great for me.

import logging
import logging.handlers

logger = None


def create_logger():
    global logger
    logger = logging.getLogger('Logger')
    logger.setLevel(logging.DEBUG)
    handler = logging.handlers.RotatingFileHandler("C:/Users/user/Desktop/info.log", maxBytes=1000000, backupCount=20)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)


create_logger()
logger.info("Text info")
logger.debug("Text debug")
logger.warning("Text warning")
logger.error("Text error")
logger.critical("Text critical")

And the result looks great:

2017-12-19 15: 06: 43,021 - Logger - INFO - Text information
2017-12-19 15: 06: 43,021 - Logger - DEBUG - Text debugging 2017-12-19 15: 06: 43,022 - Logger
- WARNING - Text warning
2017-12-19 15: 06: 43,022 - Logger - ERROR - Text Error
2017-12-19 15: 06: 43,022 - Logger - CRITICAL - critical text

Well, I want to add a new level of logging as follows:

logger.message("Text message")  

And the result should be like that

2017-12-19 15: 06: 43,022 - Registrar - MESSAGE - Text message

thank

+7
source share
1 answer

( ):

, , . , , , , , , , . , , , , , / , .

:

enter image description here

, :

logging -module, _levelToName _nameToLevel . , , addLevelName() .

, MESSAGE, 25:

import logging

# Define MESSAGE log level
MESSAGE = 25

# "Register" new loggin level
logging.addLevelName(MESSAGE, 'MESSAGE')  # addLevelName(25, 'MESSAGE')

# Verify
assert logging.getLevelName(MESSAGE) == 'MESSAGE'

, , Logger.log(level, msg) -method :

logging.log(MESSAGE, 'This is a message')

:

 def message(self, msg, *args, **kwargs):
    if self.isEnabledFor(MESSAGE):
        self._log(MESSAGE, msg, args, **kwargs) 

message() -function logging:

 logging.message = message
 # or setattr(logging, 'message', message)

message() -function :

 logging.Logger.message = message
 # or setattr(logging.Logger, 'message', message)

Logger

message(msg) -method, (, info(msg), warning(msg) ..)

message(msg) -method MESSAGE:

class MyLogger(logging.Logger):
    def message(self, msg, *args, **kwargs):
        if self.isEnabledFor(MESSAGE):
            self._log(MESSAGE, msg, args, **kwargs)

, logging.getLogger(name), . , :

, MyLogger logging.Logger :

logging.setLoggerClass(MyLogger)
logger = logging.getLogger('A new logger name')
logger.message('This seems to work')
assert isInstance(logger, MyLogger)

logger loggerDict logging.Manager (EDIT: , . ):

my_logger = MyLogger('Foo')
logging.Logger.manager.loggerDict['Foo'] = my_logger
logger = logging.getLogger('Foo')
logger.message('This is the same instance as my_logger')
assert logger is my_logger

# Use the new logger class
logger.warning('Custom log levels might be a bad idea')
logger.message('Here is a message')
# Log with custom log level:
logger.log(MESSAGE, 'This is a message')

, MESSAGE , . (, 25, )

+14

Source: https://habr.com/ru/post/1691041/


All Articles