Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating a Custom Logger

In this lesson, you’re going to get some hands-on practice. You’re going to create a custom logger as well as some handlers that will represent output destinations.

Then, you’ll attach formatters to those handlers so you can independently format your logging output based on destination. After all that, you’ll attach those handlers to the logger so that you can use it. Basically, you’re going to create a bunch of objects and then wire them together to form a logging system.

Comments & Discussion

Najmeh on Aug. 15, 2019

Thanks for the great video and explanation.

Fahim on Nov. 7, 2019

Hi,

I am getting below error for mentioned code.

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'level'
Logged from file python-logging.py, line 20
Traceback (most recent call last):
  File "python-logging.py", line 20, in <module>
    logger.warning("This is warning")
  File "/usr/lib/python2.7/logging/__init__.py", line 1179, in warning
    self._log(WARNING, msg, args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1286, in _log
    self.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1296, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1335, in callHandlers
    if record.levelno >= hdlr.level:
AttributeError: 'Formatter' object has no attribute 'level'

Here is my complete code

import logging

logger = logging.getLogger(__name__)


c_handler = logging.StreamHandler()
f_handler = logging.FileHandler("File.log")
c_handler.setLevel(logging.WARNING)
f_handler.setLevel(logging.ERROR)

c_format = logging.Formatter('%(name)s - %(level)s - %(messeage)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(level)s - %(messeage)s')

c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)

logger.addHandler(c_handler)
logger.addHandler(f_format)

logger.warning("This is warning")
logger.error("This is error")

Fahim on Nov. 7, 2019

Hi,

I am getting below error for mentioned code.

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'level'
Logged from file python-logging.py, line 20
Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'level'
Logged from file python-logging.py, line 21
Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
KeyError: 'level'
Logged from file python-logging.py, line 21```

Here is my complete code

import logging

logger = logging.getLogger(name)

c_handler = logging.StreamHandler() f_handler = logging.FileHandler(“File.log”) c_handler.setLevel(logging.WARNING) f_handler.setLevel(logging.ERROR)

c_format = logging.Formatter(‘%(name)s - %(level)s - %(messeage)s’) f_format = logging.Formatter(‘%(asctime)s - %(name)s - %(level)s - %(messeage)s’)

c_handler.setFormatter(c_format) f_handler.setFormatter(f_format)

logger.addHandler(c_handler) logger.addHandler(f_handler)

logger.warning(“This is warning”) logger.error(“This is error”)

```

Fahim on Nov. 7, 2019

I figure it out. There was some silly spelling mistakes.

Become a Member to join the conversation.