Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Python

Initialisation

Class "static" member

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

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


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. 


Code Block
import logging

# global variable 
logger = logging.getLogger(__name__)

class B:
		# class content
		pass     

Printing information

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

Code Block
__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')

...

User can configure logging level for:

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

    Code Block
    import logging
    
    # setting ALL loggers
    logging.basicConfig(level=logging.ERROR)


  • given particular class  (via via class logger )

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


  • module (via logger defined as global variable)

    Code Block
    import logging 
    
    #setting particular logger
    logging.getLogger(A.B.__qualname__'fully.qualified.module.name').setLevel(logging.INFO)


    Info
    titleFully 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


    Info
    titleAcknowledgement

    This work has been carried out within the framework of the EUROfusion Consortium and has received funding from the Euratom research and training programme 2014-2018 under grant agreement No 633053.The scientific work is published for the realization of the international project co-financed by Polish Ministry of Science and Higher Education in 2019 and 2020 from financial resources of the program entitled "PMW"; Agreement No. 5040/H2020/Euratom/2019/2 and 5142/H2020-Euratom/2020/2”.