Python Tutorial
import logging logging.debug('Debugging info'); logging.info('Informational details'); logging.warning('Warning message'); logging.error('Error message'); logging.critical('Critical error');Output:
$ python logging_test.py WARNING:root:Warning message ERROR:root:Error message CRITICAL:root:Critical error
logging.getLogger(__name__)here __name__ is the module’s name in python package namespace and this getLogger method returns class 'logging.Logger'.
import logging Log = logging.getLogger("test"); class Arithmetic: def __init__(self, a, b): self.a = a; self.b = b; def div(self): # Log message can be added in any methods based on message priority. Log.debug('Debugging info'); Log.info('Informational details'); Log.warning('Warning message'); Log.error('Error message'); Log.critical('Critical error'); return self.a/self.b; arth = Arithmetic(10, 2); result = arth.div(); print(result);Output:
$ python3 logging_test.py Warning message Error message Critical error 5.0need to use python3 interpreter, otherwise error will be printed like 'No handlers could be found for logger "test"' for python interpreter but result 5 also printed.
Logger.debug(debug_message, *args, **kwargs)¶debug_message is the message format string, and the args are merged into debug_message using the string formatter or can also use keywords in the format string with a single dictionary argument.
Logger.info(information_message)information_message can also be like debug_message formatter with arguments.
Logger.warning()
Logger.error(error_message)
Logger.critical(msg, *args, **kwargs)
Logger.log(lvl, msg, *args, **kwargs)
Logger.exception(msg, *args, **kwargs)
format(record)record is the dictionary and used as the operand to a string formatting operation. format method returns the string.
filter(record)Returns nonzero if need to log specified records, returns zero if not needed to log specified records.
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page