Return to Snippet

Revision: 70225
at December 13, 2015 00:55 by ManishChandraReddy


Updated Code
The main entry point of the application
    
    logger = logging.getLogger("exampleApp") #Creates the logger object
    logger.setLevel(logging.INFO) #Logger level set to INFO
 
    # create the logging file handler
    fh = logging.FileHandler("new_snake.log") #Default FileHandler created. Could have created anything else like SMTP, Stream etc.
 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #Formatter object for the handler.
    fh.setFormatter(formatter) #Adding formatter to the Handler object
 
    # add handler to logger object
    logger.addHandler(fh) #Adding the Handler object to the Logger
 
    logger.info("Program started")
    result = otherMod2.add(7, 8) #To see that the inside logs differently
    logger.info("Done!")


##For Other functions with different names

# otherMod2.py
import logging
 
module_logger = logging.getLogger("exampleApp.otherMod2")
 
#----------------------------------------------------------------------
def add(x, y):
    """"""
    logger = logging.getLogger("exampleApp.otherMod2.add")
    logger.info("added %s and %s to get %s" % (x, y, x+y))
    return x+y

Revision: 70224
at December 13, 2015 00:47 by ManishChandraReddy


Updated Code
The main entry point of the application
    
    logger = logging.getLogger("exampleApp") #Creates the logger object
    logger.setLevel(logging.INFO) #Logger level set to INFO
 
    # create the logging file handler
    fh = logging.FileHandler("new_snake.log") #Default FileHandler created. Could have created anything else like SMTP, Stream etc.
 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #Formatter object for the handler.
    fh.setFormatter(formatter) #Adding formatter to the Handler object
 
    # add handler to logger object
    logger.addHandler(fh) #Adding the Handler object to the Logger
 
    logger.info("Program started")
    result = otherMod2.add(7, 8) #To see that the inside logs differently
    logger.info("Done!")

Revision: 70223
at December 13, 2015 00:44 by ManishChandraReddy


Initial Code
The main entry point of the application
    """
    logger = logging.getLogger("exampleApp") #Creates the logger object
    logger.setLevel(logging.INFO) #Logger level set to INFO
 
    # create the logging file handler
    fh = logging.FileHandler("new_snake.log") #Default FileHandler created. Could have created anything else like SMTP, Stream etc.
 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #Formatter object for the handler.
    fh.setFormatter(formatter) #Adding formatter to the Handler object
 
    # add handler to logger object
    logger.addHandler(fh) #Adding the Handler object to the Logger
 
    logger.info("Program started")
    result = otherMod2.add(7, 8) #To see that the inside logs differently
    logger.info("Done!")

Initial URL
http://www.blog.pythonlibrary.org/2012/08/02/python-101-an-intro-to-logging/

Initial Description
<h1>Understanding logger objects in Python Logging module</h1>

***

[Full link ](https://docs.python.org/2/howto/logging.html#loggers)

```import logging``` to import logging module.

Hierarchy:
```logging ->handler->formatter```

Logging is User API. Handling handles the logging instance's requests to log. Also has its own level. Formatter is the final gate to writing the logs down. Helps format. 

`logging.BasicConfig` to start a basic logger.

Initial Title
Logging object in python

Initial Tags

                                

Initial Language
Python