Logs- A System file that consist of messages, such as who logged into the system, Time stamp, Error that occurred. Basically if you have any issues, you can look into the logs to identify the issues or for a hint of what causes an error.
It is also useful for exception, The information from exception might be captured in logs. It helps you to understand at which line error occurred.
Logging Module in Python
The python standard module is logging – http://bit.lypy-lo
It has following properties
1- Priority Level Ranking and Matching functions.
1) debug()
2) info()
3) warning()
4) critical()
5) error()
Note – logging.warn has been deprecated since Python 3.3 and you should use logging.warning.
2) Message to be saved in Logs.
3) Redirecting the messages to your terminal through Handlers. The terminal may be database, FTP location and other places.
4) Output Formatters.
5) Condition filters which gives output based on input.
6) Loggers object as connection with the module.
Lets see the error logging with Examples.
import logging
logging.info('Airport is blocked')
logging.warning('Smoking is injurious to Health')
logging.debug('Smoke can cause issues in Airplane')
#Output
WARNING:root:Smoking is injurious to Health
You can search in your log file about the debug or a warning for any dates to identify the issues.
Default Priority Levels and How to change it :
The default primary level is warning and debug has the lowest priority.
Important to Note here that the debug() and info() messages did not get logged.
The reason is, by default, the logging module logs the messages with a severity level of WARNING or above.
You can change that by configuring the logging module to log events of all levels if you want.
You can define your own severity levels by changing configurations, but it is generally not recommended and can cause issues.
Example
import logging
logging.debug('For debug message')
logging.info('for an info message')
logging.warning('For a warning message')
logging.error('For an error message')
logging.critical('For a critical message')
Output
WARNING:root:For a warning message ERROR:root:For an error message CRITICAL:root:For a critical message
Calling a debug function immediately locked the warn function first because of its high priority and displays the warn message first.
Therefore Calling a lower priority always start the flow from high to Low priority functions. Remember Same as energy which flows from higher to lower potential
Lets see with the Examples below.
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.WARNING)
logger.warning('Icy Situations ')
# high priority
logger.error('Ice on Airport ')
#Low priority
logger.debug('Ice on Airport Runways ') #Lowest Priority.
Output
WARNING:Airport:Icy Situations ERROR:Airport:Ice on Airport
How to Create Logger with a Name
Generally It is standard to create a logger with a name. Example here displays the same below:
Important Note : Sometimes basicConfig() won’t affect the root handler if it’s already setup:
Lets see example follows, even after changing the level to debug, The output remains warning.
import logging
logging.basicConfig(level = logging.DEBUG)
logger = logging.getLogger('Airport')
logger.debug('Ice on Airport ')
logger.warning('Ice on Airport ')
#Output
WARNING:Airport:Ice on Airport
Resolution
Make sure to call basicConfig() in advance. so the root logger initially has some setup. I.e.:
Debug is now the priority.
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
logger.debug('Ice on Airport ')
#output
DEBUG:__main__:Ice on Airport
How to direct messages using handlers.
Calling a basicConfig() with a filename and argument creates a file handler and it is available to our logger. There are various handlers through which messages can be sent to mail, places, web servers etc.
You can also Change the formatting of the messages.
import logging
logging.basicConfig(level = 'DEBUG',filename = 'Airport.log')
logger = logging.getLogger('Airport')
logger.debug('Ice on Airport ')
logger.warning('Ice on Airport ')
Output
DEBUG:Airport:Ice on Airport WARNING:Airport:Ice on Airport