Python Logging - Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 มิ.ย. 2024
  • I'm sure many of you are guilty of using the standard print debugging method, and you just have a bunch of print statements scattered all over the place. There is nothing inherently wrong with doing that, and in smaller programs it's completely fine, but once you get into larger programs you'll definitely want to have a persistent log. Watch the video to learn more!
    💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: programmingexpert.io/tim and use code "tim" for a discount!
    📄 Resources 📄
    Logging Attributes: docs.python.org/3/library/log...
    ⭐️ Timestamps ⭐️
    00:00 | Why Use Logging?
    01:03 | ProgrammingExpert
    01:32 | Logging Levels
    04:09 | Logging To A File
    07:32 | Logging Variable Values
    08:45 | Logging Exceptions
    10:16 | Custom Loggers
    12:21 | Handles and Formatters
    14:19 | Conclusion
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    👕 Merchandise: teespring.com/stores/tech-wit...
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My TH-cam Gear: www.techwithtim.net/gear/
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    - Tech With Tim
    - Logging
    - Code Log
    - Why Use Logging
    - What is Logging
    ⭐️ Hashtags ⭐️
    #TechWithTim #Logging #Programming

ความคิดเห็น • 116

  • @johnnytoobad7785
    @johnnytoobad7785 2 ปีที่แล้ว +27

    Finally...Someone gives an informative lecture/demo on Python's logging engine. This was excellent and would have saved me a bit of time and confusion a couple of years ago. I use a separate "logger" for each module for large apps. This way I know what module (and in many cases the class and/or function) that generated the message. The only problem with this type of logging is that the module level logger (by definition) is a "global" variable. Recently I implemented a color-coded console-only logger also. Once you understand the various "logging" components (handler, formatter, name) it's really not that hard to do. Excellent Video !

    • @afonsosalbrecht
      @afonsosalbrecht ปีที่แล้ว +1

      seriously?? that's a copy and pasted video from corey schafer

  • @danub5551
    @danub5551 2 ปีที่แล้ว +52

    I was *literally* just searching about logging in python for a project yesterday... just in time Tim 👍

  • @ifthennotagain5195
    @ifthennotagain5195 2 ปีที่แล้ว +1

    I've been following you since under 100k subscribers... somehow you consistently produce content that aligns with what I am in the process of learning. Thank you! (I'm a former gryphon too!)

  • @mccdan666
    @mccdan666 ปีที่แล้ว +5

    OK, now I understand why you have 1.1M subscribers: exceptional content delivered exceptionally well. First time seeing your content, instant subscriber. Keep up the good work.

  • @BrandonJacobson
    @BrandonJacobson 2 ปีที่แล้ว +4

    I haven't really bridged the gap into intermediate level projects yet. I've used logging a couple of times before, but I can def see how it would be more useful in larger, more mature, programs. Thanks for the video!

  • @BryanJenks
    @BryanJenks 2 ปีที่แล้ว +1

    Loguru is my go-to logging module, makes it sooooo easy to define custom levels, multiple logging sinks, and i just parse the data into a logging SQL Server table and it is GLORIOUS

  • @moondevonyt
    @moondevonyt 2 ปีที่แล้ว +1

    no joke, i was just looking for a logging in python video. thanks a lot Tim ❤️

  • @jurajlk9019
    @jurajlk9019 2 ปีที่แล้ว +44

    Dont use f-string when logging. Because, if you use logger.debug("text %s", variable), the text formating will only happen, if the debug level is active. If you use f-string, the string will be formated, even before it is decided wether the msg will be logged, thus losing performance

    • @Anequit
      @Anequit ปีที่แล้ว +1

      Can you post benchmarks of the performance loss?

    • @jdhoehler
      @jdhoehler 10 หลายเดือนก่อน +7

      Was about to comment the same thing. Such a minor point but is considered best practice.

    • @ExDarkx3
      @ExDarkx3 2 หลายเดือนก่อน

      ​@@Anequit they can't, because it depends on what you are evaluating in the fstring. But given that this is a debug level statement, it shouldn't be too expensive. I personally wouldn't sacrifice the readability and still use fstrings tho.

    • @goose_clues
      @goose_clues 2 หลายเดือนก่อน

      If you care about performance, why not use other languages 😂
      Seriously, python is a flexible and high readability language with reasonable performance. Why do you need to sacrifice a readability? Are you throwing a fuckin Ackermann function to fstring?

  • @astronemir
    @astronemir 2 ปีที่แล้ว

    Very valuable and time saving tutorial. Thanks. Fixed up my logging implementation

  • @ayupitsali608
    @ayupitsali608 ปีที่แล้ว +1

    Cheers Tim! Another Python skill in the bag!

  • @BummerSlug
    @BummerSlug 2 ปีที่แล้ว

    Crazy useful! I am done with print statements now, thank you!!

  • @davecapstick8129
    @davecapstick8129 4 หลายเดือนก่อน

    thanks for this, you have explained logging in a really simple and straightforward manner. You have probably saved me a lot of effort in trawling through documentation. Much appreciated

  • @deveshgolwalkar3605
    @deveshgolwalkar3605 หลายเดือนก่อน

    short & crisp explanation. Perfect!

  • @Earnest1G
    @Earnest1G 2 ปีที่แล้ว

    Amazing channel. Has helped me so much when studying to become a software developer. Amazing work really!

  • @mikez762
    @mikez762 2 ปีที่แล้ว

    i have been debugging all day with those print's. thanks for another way

  • @johnnyz1206
    @johnnyz1206 2 ปีที่แล้ว +12

    By the way, log.log is unchanged not because you are not using it but because you overwrite it with the same log text. All log comes from a logger eventually goes to its parent logger (in this case: root logger).And if you don’t want this behavior, you need to set logger.propagate = False.

    • @ShivamPatel-yg3kd
      @ShivamPatel-yg3kd 2 ปีที่แล้ว +1

      Thank you so much, I was literally facing this issue... Thank you, infact in this video the log.log has got updated indeed

    • @user-us7rg4cd6p
      @user-us7rg4cd6p 6 หลายเดือนก่อน

      @@ShivamPatel-yg3kd Thank you so much

  • @moumniable
    @moumniable 2 ปีที่แล้ว

    Perfect timing ! Thanks 😀

  • @khazartalibov9048
    @khazartalibov9048 2 ปีที่แล้ว +3

    Nice.We would glad to see about Golang (logging, unit testing and other advanced topics )

  • @ABEL-cd2sp
    @ABEL-cd2sp 2 ปีที่แล้ว

    Listen i won't say that i intended to use print as my main debugging tool but it just naturally happened because i didn't realize that debugging already did most of the work for you, heck at first i didn't even really know what debugging was.
    Great tutorial however I'll end up using it quite a bit in the long term and it saves me some work so nice.

  • @nikolakhs
    @nikolakhs 2 ปีที่แล้ว

    For real this video what at the perfect time, last night i was searching for that and i understood nothing until this time i saw this video . Thanks

  • @teclote
    @teclote 3 หลายเดือนก่อน

    Outstanding, works great first time, lots of good ideas.

  • @thomasbates9189
    @thomasbates9189 10 หลายเดือนก่อน

    Very helpful! Thank you!

  • @hemashankartalupuri6121
    @hemashankartalupuri6121 9 หลายเดือนก่อน

    best thing that i found today about logging-python

  • @vivekan97
    @vivekan97 2 ปีที่แล้ว +2

    Hey Tim ! great content as always. can you do tutorial on how to write unit test for Flask applications ?

  • @user-cc8kb
    @user-cc8kb 2 ปีที่แล้ว

    Great tutorial! Thanks :)

  • @enzanto
    @enzanto 2 ปีที่แล้ว

    lol, reading all the comments about people searching for logging and praising your video, like it came out the same day they commented.. then i realized the video came out 24 hrs ago, and i too searched for logging the same day the video came out xD

  • @siddheshnaik4956
    @siddheshnaik4956 ปีที่แล้ว

    Thanks a lot , very informative !!

  • @krishnans2006
    @krishnans2006 2 ปีที่แล้ว +5

    Amazing video! I'd like to add, however, that most linters recommend using the 'args' of the logging levels to embed variables. For example:
    logging.info(f"the value of x is {x}") -> wrong
    logging.info("the value of x is %d" % x) -> wrong
    logging.info("the value of x is %d", x) -> correct approach
    Do note that all three ways shown above do work - it's just that the third method is recommended for use.

    • @GabrielAcosta00
      @GabrielAcosta00 2 ปีที่แล้ว

      I come to comment on this +1

    • @dimond8270
      @dimond8270 2 ปีที่แล้ว +1

      can i ask why? ive never heard of a correct way of formatting strings?

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว

      The second version is fine, too. Note that the docs state that no formatting substitution is done on the message string if no formatting arguments are supplied.
      Compare, say, printf(3), which always does formatting substitution even when it doesn’t make sense. Python is a bit smarter.

  • @nanayaw7159
    @nanayaw7159 2 ปีที่แล้ว

    Great tutorial

  • @oneh0urtrades
    @oneh0urtrades 6 หลายเดือนก่อน

    thank you it was a helpful video

  • @user-sh8kh7sk8p
    @user-sh8kh7sk8p 4 หลายเดือนก่อน

    I do miss one thing though; this video shows how to use logging in one file, but what I think most people are having an issue with is multiple loggers.
    An idea for a "logging -part 2" could be, how do we use (different) loggers across a module?
    In the video above there's not really a reason to use "logger", you can use "logging" directly thus I think it would really be great to have an example where (and how) you would use various loggers

  • @BombasticTom492
    @BombasticTom492 2 ปีที่แล้ว

    legend finally made it

  • @ParaSteve1985
    @ParaSteve1985 ปีที่แล้ว

    Everything i needed in just 15 Minutes! Perfect

  • @aayushrajbhar1029
    @aayushrajbhar1029 2 ปีที่แล้ว

    Very useful 👍☺️

  • @Azsha0680
    @Azsha0680 2 ปีที่แล้ว

    Hi, nice content ! thx a lot ! starting in Python and your video really help. I'm just wondering about your interface. I saw you use PyCharm in your other video and i'm use to the 'VBA' type of interface. What are you using now and what do you suggest using. Thx in advance !

  • @timwebster85
    @timwebster85 ปีที่แล้ว

    Epic! thanks

  • @secretions2991
    @secretions2991 2 ปีที่แล้ว

    It's so perfect timing

  • @mikelancaster8924
    @mikelancaster8924 2 ปีที่แล้ว

    Thanks so much!

  • @vanoelizbarashvili6655
    @vanoelizbarashvili6655 2 ปีที่แล้ว

    very useful thanks

  • @avnerduchovni6675
    @avnerduchovni6675 ปีที่แล้ว

    Thanks for the good content

  • @salimzavedkarim230
    @salimzavedkarim230 ปีที่แล้ว

    You're the man!

  • @ImYala
    @ImYala 2 ปีที่แล้ว

    Hey Tim.. I have been learning react and django to make full stack web applications. Do you have any suggestions on what to do when you get stuck and can't seem to find the solution with google?

  • @gzbin365
    @gzbin365 2 ปีที่แล้ว

    really useful

  • @gugugaga5867
    @gugugaga5867 2 ปีที่แล้ว

    Hey Tim when are you going to open startup?

  • @nahakuu
    @nahakuu 2 ปีที่แล้ว +1

    I have noticed, when using the logging.exception() call it adds the exc_info on end of the preformated text.
    This is problem as I created the format to be Json and this exc_info is always put outside of the JSON which breaks the format.
    Can I somehow use the logging formatting to store this exc_info in my json format?
    I tried to use exc_info but that spits out multiple lines, like text what I can see on screen when error occurs. But I want just the Traceback what is return in function logging.exception(), as there it seems to be always the exact point of failure rather then all the dependencies what I have no power over.
    Thank you

  • @noufalkaku
    @noufalkaku 2 ปีที่แล้ว

    Hi Tim, may I know the autocomplete package your are using?

  • @eapenjiss7945
    @eapenjiss7945 ปีที่แล้ว

    Is it possible to give a short description of the log file at the beginning of each log file, whenever it is created?

  • @mosa36
    @mosa36 ปีที่แล้ว

    Could we get more information on db handler? I'd love to learn how to use it :)

  • @adityaalmighty1
    @adityaalmighty1 2 ปีที่แล้ว

    Hi! Any idea when is Kite going to be up?

  • @trollingspecialist554
    @trollingspecialist554 ปีที่แล้ว

    thank you
    thank you
    thank you

  • @haroldabella2041
    @haroldabella2041 2 ปีที่แล้ว

    What if an existing value exists on the logger? Is it going to skip logging the existing value or will it add the same value?

  • @gbesiu
    @gbesiu 2 ปีที่แล้ว

    Could you do something about linear programming please?

  • @amrithpurandhar9882
    @amrithpurandhar9882 2 ปีที่แล้ว

    DSA chapter is it covered in programming expert ?

  • @MichaelVash7886
    @MichaelVash7886 2 ปีที่แล้ว

    Got a list of scripts that I need to turn logging on for. Need to add some logging functions to class object I have made for it

  • @PGhai
    @PGhai ปีที่แล้ว

    how do you log in file as well as on screen ?

  • @skarface5978
    @skarface5978 2 ปีที่แล้ว

    Hello, I'm new to programming and I don't know anything about it. I would like to know what you recommend

  • @Lunolux
    @Lunolux 2 ปีที่แล้ว

    finnaly found, why the "X" user can"t launch my script => it's because this user has not some library installed
    this logging think is very useful thx man.

  • @user-xq6ol2un7z
    @user-xq6ol2un7z 2 ปีที่แล้ว

    What do u use as sublime theme and colour scheme 🌚

  • @JohnintheDesert
    @JohnintheDesert 2 ปีที่แล้ว

    What is your favorite IDE?

  • @shreyb1409
    @shreyb1409 9 หลายเดือนก่อน

    my test.log file was empty till the time i did not add the logging.basicConfig, can someone explain why?

  • @gskluzacek
    @gskluzacek 2 ปีที่แล้ว +2

    Tim, as a couple other people have pointed out in the comments, but it bares repeating, you should never use f-strings, simple concatenation or .format when logging messages. Instead, you should use %s or similar in the log message as a placeholder, and then pass the variable that you want to merge into the message as an argument to the logging method.
    For example:
    in_val = 105
    logging.info(“Tip amount entered: %s, exceeds 100 percent”, in_val)
    This way, the system doesn’t have to do the extra work of formatting the log message, if for example the log level is set to error. When you pass the variable as an argument to the logging function this way, the system will defer the formatting of the message until it absolutely knows that the log message is going to be output.

  • @Richard_GIS
    @Richard_GIS 2 ปีที่แล้ว

    Didn't know about the exception thing

  • @keeslauwers6824
    @keeslauwers6824 2 ปีที่แล้ว +1

    do you read my mind
    I am dealing with an error for the last 3 day
    and I print to debug
    print("this is painful")

  • @NishantCosmos
    @NishantCosmos 2 ปีที่แล้ว +1

    i'm new to debugging professionally, idk why to do logging

  • @wilk85
    @wilk85 2 ปีที่แล้ว

    which IDE/code editor you are using?

    • @fortuneosho8137
      @fortuneosho8137 2 ปีที่แล้ว

      Sublime text I think

    • @wilk85
      @wilk85 2 ปีที่แล้ว

      @@fortuneosho8137 thanks

  • @jonk-r7352
    @jonk-r7352 2 ปีที่แล้ว

    What ide?

  • @Lunolux
    @Lunolux 2 ปีที่แล้ว

    doesn't know about that, i was creating my "own log file" ^^" for my little python programmation
    this gonna maybe help me, to know whatt the name of the user my script are lauch
    thx

  • @FMH201
    @FMH201 2 ปีที่แล้ว

    Thank you for the video. I really hope you would get rid of the background music.

  • @BeigeAlertHamburg
    @BeigeAlertHamburg 2 ปีที่แล้ว

    Logging in FastAPI gives me headaches. Dublicate lines, and no rollover in Windows.

  • @vinayakram5133
    @vinayakram5133 2 ปีที่แล้ว

    💪💪💪

  • @armkreuz82
    @armkreuz82 2 ปีที่แล้ว

    Damm, I just spent the last 2 days builing my logger template for work.

  • @realityveil6151
    @realityveil6151 2 ปีที่แล้ว

    Sure, the logger is cool, but print isn't going to make my script reach out to a remote LDAP server and execute arbitrary code. So I think I'll just keep using print.

  • @makadi86
    @makadi86 10 หลายเดือนก่อน

    what is this ide?

  • @dimitriosdesmos4699
    @dimitriosdesmos4699 2 ปีที่แล้ว

    The greatest problem I face as a programmer is not ERRORs, ..it is erroneous results. ..and for that the LOGGING module is not helpful.....you need to trace every step of the program ...Errors are not hard to find....getting a 5, instead of a 55 though....can take you 2 days to figure out sometimes...because there is no real error associated with it. Thats what takes time , not things like 1/0...or adding strings to integers.

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 ปีที่แล้ว

    The logging module just gives you more control over message output, that’s all. If you use sys.stderr.write, then you have to do your own implementation of verbosity levels, for example. The standard Python module has already pre-invented this whole wheel (and much else besides), so you don’t have to.

  • @jeff_tech
    @jeff_tech 2 ปีที่แล้ว

    Nice Video! The music was a little bit too loud tho

    • @waqaspathan3337
      @waqaspathan3337 2 ปีที่แล้ว

      i didnt even realize music was playing til you pointed it out

    • @jeff_tech
      @jeff_tech 2 ปีที่แล้ว

      @@waqaspathan3337 Interesting. It’s probably just me

  • @CodeNight-dm2hv
    @CodeNight-dm2hv 2 ปีที่แล้ว +2

    I was looking to put a logging system in my app today! How did you know?

  • @numberonep5404
    @numberonep5404 2 ปีที่แล้ว

    Yep yep thats the stuff

  • @twqwhy6763
    @twqwhy6763 2 ปีที่แล้ว

    I'm sorry, TIm, i almost never say how much your content helps. Thank you, i'm sorry that we are moneyless-bums and you have to rely onto a separate revenue stream.

  • @ArgumentumAdHominem
    @ArgumentumAdHominem 2 ปีที่แล้ว

    I've never encountered the need to use a logger in 1 person projects, no matter large or small. However, if you are working as a team, having a common standard for logging is extremely useful

  • @enos5192
    @enos5192 2 ปีที่แล้ว

    Looking for the past 1 year

  • @aarav_
    @aarav_ 2 ปีที่แล้ว

    7:32

  • @eduardopasseto2387
    @eduardopasseto2387 ปีที่แล้ว

    WARNING: Do not use logging to replace common messages on your system! Use print instead. You will be flooded with unwanted messages from third-party packages! The use of logging in these cases is considered bad practice!

    • @arikiri_698
      @arikiri_698 8 หลายเดือนก่อน

      It’s literally a built-in package but ok

  • @user-uc6eh5nx2u
    @user-uc6eh5nx2u 9 หลายเดือนก่อน

    hi

  • @fhujf
    @fhujf 2 ปีที่แล้ว

    "Stop using print debugging" - what sort of madness is this? :D

  • @augustlumanlan2777
    @augustlumanlan2777 2 ปีที่แล้ว

    4th

  • @RichardLucas
    @RichardLucas 3 หลายเดือนก่อน

    No, I'll use logging, but no one is going to stop using print() for debugging. It would be stupid. I realize you have to have an angle, but it's still stupid.

  • @flobbie87
    @flobbie87 2 ปีที่แล้ว

    You stop using print debugging!

  • @chrysos
    @chrysos 2 ปีที่แล้ว

    1.

    • @suola_
      @suola_ 2 ปีที่แล้ว +3

      ratio

  • @jaredbrooks2226
    @jaredbrooks2226 ปีที่แล้ว +2

    Jesus loves you. Start a relationship with Him today before its too late.

    • @gaunterodimm6370
      @gaunterodimm6370 3 หลายเดือนก่อน +1

      I started a relationship with h8m last night. Nnow my balls are empty. Halleluja

  • @Geogak
    @Geogak 2 ปีที่แล้ว

    Yes, please stop teaching print()

  • @renaudcalmont
    @renaudcalmont 2 ปีที่แล้ว

    Those people, trying to use Python like a full-fledged language instead of the hacky script beast it really is, are hilarious