Error() method of logger class with Example

Method Name:

error

 

Method Signature:

error(logMessage, *args, **kwargs)

 

Method Overview:

  • error() method of logger class in the logging module of Python, writes a log message of severity logging.ERROR to the sys.stderr which is usually mapped to the console.

 

  • Developers log with logging.ERROR to record and notify failure events during the execution of a Python Program.

 

  • For example the events - Authentication failed, session timed out, found invalid path to a resource are all belong to error category and logged with the intention to convey the error message to the system managers and users immediately.

 

  • Logging of an event of severity logging.ERROR does not convey that the program is behaving erroneously.

 

  • Typically after seeing an error message users and administrators take actions like logging in to a system with right credentials after a failed attempt with wrong credentials, passing a right command-line switch to a program and so on.

 

Example:

import logging

 

# Get a logger with the name of current module

logr = logging.getLogger(__name__)

 

# Verify the current log level - which is by default logging.WARNING

print("Log level-numeric code:")

print(logr.getEffectiveLevel())

 

# Set a standard log prefix

formatString = '%(asctime)s %(filename)s => %(message)s'

logging.basicConfig(format=formatString)

 

logr.warning("A sample error message")

 

Output: 

Log level-numeric code:

30

2017-03-25 12:47:34,961 logr4.py => A sample error message

 

 

  


Copyright 2024 © pythontic.com