Error() Method Of Logger Class With Example

Method Name:



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.



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:")



# Set a standard log prefix

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



logr.warning("A sample error message")



Log level-numeric code:


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




Copyright 2023 ©