Logging in Python .
Python logging is as much important for python as flavours in Ice Cream , lies in politics and life without goal .without flavour ice cream means ,it may be tasty for you but not for others ,so a programme should be like that it should be understandable by others .Logging is used for log purpose of a programme ,what actually it is doing .lies in politics means you can not grow your code without logging otherwise ,it is very difficult to track your programme .
So before further moving we can see .where logging can be used .
1: Displaying a information of programme .
2: Issuing a warning .
3: Catching a error .
4: Catching Critical information .
How we can achieve by python logging:
So for achieving we need to know ,level in python ,mostly 5 type of level are used in python DEBUG,INFO,WARNING ,ERROR and CRITICAL .let's elaborate what actually each level means in python .
DEBUG : Debug we used for diagnostic purpose or detailed information .
INFO : Programme is working as expected .
WARNING : For issuing a warning .
ERROR : for catching error .
CRITICAL : capturing critical information ,eg. threshold or KPI value .
when i started reading about logging not practical , i was much more assumed by logging as i was thinking it is very fantastic library , level will automatically assigned to programme as per requirement of programme , i need to just call library .. he he funny .. soon i realised ,it is you who have to find out that which level need to placed at which place .
so let's take a example :
OutPut of Myapp.log
so you have seen ,how much easy logging .
How to single log file into multiple class in python .
follow simple approach ,just make name of log file common in all classes .so it will write same log file sequentially .
how logging works :
logging has basically four component
1: loggers 2:handlers 3: filters 4:formatters.
How loggers works:
loggers object works in two way 1st : set configuration of logger 2nd is message sending .
methods to set configuration:
a: logger.setLevel() : for setting label of logger ,debug is lowest built in severity level , so if you set your level as info ,then logger will handle only INFO ,WARNING ,ERROR ,CRITICAL and will ignore DEBUG label
b: logger.addHandler or logger.removehandler() for adding and removing handler objects .
c: loggers.addFilter() and logger.removeFilter() add and remove filter objects from logger objects
So before further moving we can see .where logging can be used .
1: Displaying a information of programme .
2: Issuing a warning .
3: Catching a error .
4: Catching Critical information .
How we can achieve by python logging:
So for achieving we need to know ,level in python ,mostly 5 type of level are used in python DEBUG,INFO,WARNING ,ERROR and CRITICAL .let's elaborate what actually each level means in python .
DEBUG : Debug we used for diagnostic purpose or detailed information .
INFO : Programme is working as expected .
WARNING : For issuing a warning .
ERROR : for catching error .
CRITICAL : capturing critical information ,eg. threshold or KPI value .
when i started reading about logging not practical , i was much more assumed by logging as i was thinking it is very fantastic library , level will automatically assigned to programme as per requirement of programme , i need to just call library .. he he funny .. soon i realised ,it is you who have to find out that which level need to placed at which place .
so let's take a example :
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(levelname)s %(message)s',
filename='myapp.log', filemode='w') # level i have setted logging.DEBUG config = {'user': username, 'password': password, 'host': serverip, 'database': database, 'raise_on_warnings': True, }try: cnx = mysql.connector.connect(**config) print(serverip + 'DB is connected ') logging.debug('Mysql is connected') #levelling as debug cursor = cnx.cursor() try: cursor.execute(query) except mysql.connector.Error as err: print(err) logging.error(err) # now capture every error in log . print(query + 'is executed') logging.info(query + "is exectued ") # telling query is success fully is executed cnx.commit() cursor.close() logging.info('Cursor Successfully closed') # telling cursor is executed cnx.close() logging.info('My sql connection Successfully closed') print(serverip + 'connection is closed')except mysql.connector.errorcode as e: print(e.msg) logging.error(e.msg) # catching error in error level cursor.close() logging.info('Cursor is closed') # getting information cursor is closed cnx.close() logging.info('My sql connection is closed') # mysql connection is closed
OutPut of Myapp.log
2018-04-25 15:55:31,957 INFO Mysql is connected 2018-04-25 15:55:34,055 INFO delete from external_alarm is exectued 2018-04-25 15:55:34,612 INFO Cursor Successfully closed 2018-04-25 15:55:34,617 INFO My sql connection Successfully closed 2018-04-25 15:55:34,648 INFO Mysql is connected 2018-04-25 15:55:44,832 ERROR 1261: Row 1 doesn't contain data for all columns 2018-04-25 15:55:44,834 query is exectued 2018-04-25 15:55:44,966 INFO Cursor Successfully closed 2018-04-25 15:55:44,967 INFO My sql connection Successfully closed
so you have seen ,how much easy logging .
How to single log file into multiple class in python .
follow simple approach ,just make name of log file common in all classes .so it will write same log file sequentially .
how logging works :
logging has basically four component
1: loggers 2:handlers 3: filters 4:formatters.
How loggers works:
loggers object works in two way 1st : set configuration of logger 2nd is message sending .
methods to set configuration:
a: logger.setLevel() : for setting label of logger ,debug is lowest built in severity level , so if you set your level as info ,then logger will handle only INFO ,WARNING ,ERROR ,CRITICAL and will ignore DEBUG label
my_logger.setLevel(logging.DEBUG)
b: logger.addHandler or logger.removehandler() for adding and removing handler objects .
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME)
my_logger.addHandler(handler)
c: loggers.addFilter() and logger.removeFilter() add and remove filter objects from logger objects
Comments
Post a Comment