You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

1. Python

1.1. Initialisation

1.1.1. Class "static" member

Logger should be defined as a static  member of a class. Suggested name is "logger" . An example:

import logging
 
class B:
        # class logger 
        logger = logging.getLogger(__name__ + "." +__qualname__)


1.1.2. A global variable

Logger can be defined as a global variable  (i.e. out of a class scope).  This is not recommended way , because names may clash, if two or more modules will use the same name of variable. 


import logging

# global variable 
logger = logging.getLogger(__name__)

class B:
		# class content
		pass     

1.2. Printing information

The recommended way to access logger from within a class  is __class__.logger 

__class__.logger.debug('This is a debug message')
__class__.logger.info('This is an info message')
__class__.logger.warning('This is a warning message')
__class__.logger.error('This is an error message')
__class__.logging.critical('This is a critical message')

1.3. Setting logging level

Logging levels

# LOGGING LEVELS:

  • logging.DEBUG
  • logging.INFO
  • logging.WARNING
  • logging.ERROR
  • logging.CRITICAL


User can configure logging level for:

  • all classes and modules  of given application (via root logger)

    import logging
    
    # setting ALL loggers
    logging.basicConfig(level=logging.ERROR)
  • particular class  (via class logger )

    import logging 
    
    #setting particular logger
    logging.getLogger('fully.qualified.class.name').setLevel(logging.INFO)
  • module (via logger defined as global variable)

    import logging 
    
    #setting particular logger
    logging.getLogger('fully.qualified.module.name').setLevel(logging.INFO)

    Fully Qualified Name

    A name is fully qualified when it "is complete in the sense that it includes

        (a) all names in the hierarchic sequence above the given element and

        (b) the name of the given element itself."

    https://en.wikipedia.org/wiki/Fully_qualified_name

  • No labels