Python Tutorial: Logging Advanced – Loggers, Handlers, and Formatters

///Python Tutorial: Logging Advanced – Loggers, Handlers, and Formatters

Python Tutorial: Logging Advanced – Loggers, Handlers, and Formatters

FavoriteLoadingAdd to favorites

In this Python Tutorial, we will be going over the some more advanced logging concepts. These will include loggers, handlers, and formatters. Let’s get started.

Basics Logging Video –

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-10-26T23:36:22+00:00October 26th, 2019|Python Video Tutorials|47 Comments

47 Comments

  1. Ruchir Sinha October 26, 2019 at 11:36 pm - Reply

    Thanks Corey!! These tutorials are simply great.

  2. Mr. Codeit October 26, 2019 at 11:36 pm - Reply

    how to add filemode = "w" without using the basicConfig

  3. George Smith October 26, 2019 at 11:36 pm - Reply

    i think there will be a problem if you define all your loggers as logger, not in this specific case, but in general

  4. rubebop October 26, 2019 at 11:36 pm - Reply

    Oh man, you are such a talented teacher! Another fantastic tutorial!

  5. demev7488 October 26, 2019 at 11:36 pm - Reply

    Thought I'd share in case anyone in the future runs into this and save some troubleshooting. Non-string format statement with a variable don't seem to log. IE. logging.info('Hello ', world) didn't log vs logging.info('Hello {}'.format(world)) does log.

  6. Panagiotis Deligiannis October 26, 2019 at 11:36 pm - Reply

    This is so fun to watch, Netflix could use it as a series.

    Thank you! Amazing tutorial!

  7. Raj Learn October 26, 2019 at 11:36 pm - Reply

    Thanks,
    Can you explain, do we need to write every module files for the logging, can you explain how to logging data in one file and use it in all the modules with the same log file.

  8. Suyash Dayal October 26, 2019 at 11:36 pm - Reply

    Thank you so much for sharing with videos on logging and unit testing (not to mention, other advanced topics)…

    I am well past the beginner stage and in the process of creating my own projects… And, I am so happy that a great teacher like you are taking up such concepts…

  9. Pankaj Rawat October 26, 2019 at 11:36 pm - Reply

    I appreciate the videos you create and share knowledge. One of best python tutorials available.

  10. san R October 26, 2019 at 11:36 pm - Reply

    What is the difference between "logger.setLevel(logging.DEBUG)" and "filehandler.setLevel(logging.DEBUG)"

  11. Mustafa Khan October 26, 2019 at 11:36 pm - Reply

    import logging
    logging.info('Thank you so much CoreyMS, you are awesome.')

  12. R. Gurkan Akcakaya October 26, 2019 at 11:36 pm - Reply

    Hi I have a problem, I would like to implement logging on a higher level. I have created a logger class and imported that one in to 4 others. The problem now it that one line of log is being written multiple times.
    I have also asked the question on stackoverflow but no answers yet.
    https://stackoverflow.com/questions/55823803/how-can-i-implement-logging-in-python-for-multiple-modules

    Could you maybe create a vid on how to do this please?

    Many thanks for all the videos, they have been really helpful.

  13. Mohammad Shafiullah October 26, 2019 at 11:36 pm - Reply

    Can you share an example for Rotating File handler

  14. A D October 26, 2019 at 11:36 pm - Reply

    Very good tutorial !

  15. Deepak Bhargav October 26, 2019 at 11:36 pm - Reply

    Hey man thank you………

  16. Exedo October 26, 2019 at 11:36 pm - Reply

    Might be a little late on this one… But maybe someone in the future might give me an answer on that. In the first part of the video (before setting specified loggers) the root logger is initialized and is not overridden in the second config call. Why is that? As I understand Python so far, it works from top to bottom. So why doesn't a second call configure the logger?

  17. Eduardo Lucas October 26, 2019 at 11:36 pm - Reply

    Simply brilliant!

  18. K W October 26, 2019 at 11:36 pm - Reply

    Not sure if it is because of using intellij or not but when changing around the logger I am not getting the logger name in the files anymore. So I am not seeing _name_ or root

  19. rakesh sunku October 26, 2019 at 11:36 pm - Reply

    hi,
    very nice explanation.
    I am new to python and trying to build python.
    needed one information, we want log to end at certain point even though our job is still running. can you help on this.
    I tried with shutdown and close it didn't work..

  20. Ian Gitonga October 26, 2019 at 11:36 pm - Reply

    Great tutorial which I've found easy to understand.

  21. Shan Shen October 26, 2019 at 11:36 pm - Reply

    Super clear. Thank you so much!!!

  22. Aurelius Oliveira October 26, 2019 at 11:36 pm - Reply

    Someday I'll show up wearing a T-shirt with your face on that.

  23. Kamal Singhal October 26, 2019 at 11:36 pm - Reply

    Hey! how can I have logs from different modules in the same logfile?

  24. H G October 26, 2019 at 11:36 pm - Reply

    Great video, but it was so complex. Can you make more "easy to understand" video on this one .thanks

  25. Joe Tang October 26, 2019 at 11:36 pm - Reply

    Professional level Python.

  26. Ramya Selvaraj October 26, 2019 at 11:36 pm - Reply

    amazing video 🙂 thanks much

  27. Gary Vonerden October 26, 2019 at 11:36 pm - Reply

    Excellent. Clear and concise !! Thank you.

  28. Priyank Yadav October 26, 2019 at 11:36 pm - Reply

    Clear, Precise and elegantly explained. Loved it Corey

  29. Daniel Macuare October 26, 2019 at 11:36 pm - Reply

    Thanks for sharing these videos. Your channel has been by far the best source I've found to improve my Python skills.

  30. Eduard Lopushansky October 26, 2019 at 11:36 pm - Reply

    Hi Corey,
    Do you know how to integrate a robot.api.logger with python logger? I have a python methods that are called from robotframework keywords. Everything is ok when I run tests from robotframework, but I need to run python unit tests as well.

  31. Leonardo Grinstein October 26, 2019 at 11:36 pm - Reply

    Is there a way to log errors that are not part of a "try/except" line of code? That would be errors you don't expect.

  32. Abdelhafid DAHHANI October 26, 2019 at 11:36 pm - Reply

    Thank you ^_^

  33. Jerome vanatou sinnapane October 26, 2019 at 11:36 pm - Reply

    I do not see the same effect as shown here @11.18 My code is as follows (Employee.py):
    import logging
    logger=logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    formatter=logging.Formatter('%(levelname)s:%(name)s:%(message)s')
    file_handler=logging.FileHandler('C:UsersjeromePycharmProjectsLoggingemployee1.log')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    class Employee:
    def __init__(self,first_name,last_name):
    self.first_name=first_name
    self.last_name=last_name
    logger.info('Object created with first_name:{} and lastname: {}'.format(self.first_name, self.last_name))

    if __name__=='__main__':
    emp1=Employee('f1','l1')
    emp2=Employee('f2','l2')
    —————————————————————————-math.py——————————————————-
    import logging
    import Employee

    logging.basicConfig(filename='C:UsersjeromePycharmProjectsLoggingsample1.log',level='INFO',
    format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')

    def add(a,b):
    return a+b

    a=3
    b=5
    add(a,b)
    logging.info('Addition {} + {}'.format(a, b))

    ———————————————————————————————————-
    Now when I run the math.py ,as per the video – I should get the logging recorded into employee1.log with module name as "Employee" …Could you help me here ….I use python 2.7

    thanks,
    Jerome

  34. Smith Lo October 26, 2019 at 11:36 pm - Reply

    I ran in Spyder and it seems the logger.addHandler keep generate multiple log records each time I execute the python file. I have to pop the logger.handlers.pop() or logger.removeHandler(file_handler) after finish execute the files. anyone have have any issues like this? I want to know what's the best solution for this issue. Thanks.

  35. Nicolas Esatto October 26, 2019 at 11:36 pm - Reply

    is there a good way to print console output to a GUI text edit?

  36. Chouaib Assid October 26, 2019 at 11:36 pm - Reply

    In case of having multiple logging files, i think the best way is to combine Corey's code with the singleton design pattern, that way you'll have only one object at a time.

  37. MouseMikhail October 26, 2019 at 11:36 pm - Reply

    Thanks. Helped me a lot!

  38. Prashant Gandhi October 26, 2019 at 11:36 pm - Reply

    great video … very easy to understand. Thanks !

  39. Brady Huang October 26, 2019 at 11:36 pm - Reply

    Very helpful!! Really learn a lot from this episode!!

  40. Bharath G October 26, 2019 at 11:36 pm - Reply

    for that reason we use if _name_ == '__main__'

  41. pradeep ponduri October 26, 2019 at 11:36 pm - Reply

    Thanks for the video.
    What if I have scripts in two different folders, but I want to log information to one file.

  42. Alex Triphonov October 26, 2019 at 11:36 pm - Reply

    thank you. it's still the best logging tutorial in 2018 😉

  43. satya reddy October 26, 2019 at 11:36 pm - Reply

    Nice explanation………..

  44. Ashok Kumar October 26, 2019 at 11:36 pm - Reply

    can you use dictConfig method and make a video ?

  45. Eshant Gupta October 26, 2019 at 11:36 pm - Reply

    I execute a function in recursive order wherein name of the log file depend on the key:value read from json. I want to maintain file handlers on scope basis in other words when function returns the file handlers that was opened for that call should be alive..

  46. MarvelTv October 26, 2019 at 11:36 pm - Reply

    U r great

  47. Patrick Shapard October 26, 2019 at 11:36 pm - Reply

    Just watched the video. Great presentation Corey. I learned some new things about logging. Thanks Corey.

Leave A Comment

*