/ Published in: Python
Understanding logger objects in Python Logging module
***
[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.
***
[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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
URL: http://www.blog.pythonlibrary.org/2012/08/02/python-101-an-intro-to-logging/