logging module

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 1 19:31:26 EDT 2018


On Sun, 01 Jul 2018 06:05:48 -0700, Sharan Basappa wrote:

> Folks,
> 
> I am trying to use logging module and somehow I cannot make it work.
> 
> A simple code that I am trying is below. The commented code line 5,6 are
> other options I have tried but don't work
> 
> #importing module
> import logging
>  
> #Create and configure logger
> #logging.basicConfig(filename="D:/Projects/Initiatives/machine
> learning/programs/newfile.log",
> #logging.basicConfig(filename="newfile.log",
> logging.basicConfig(format='%(asctime)s %(message)s')

This is not the actual code you are using, because this gives a 
SyntaxError. You are missing a closing parentheses.

Please don't retype your code from memory. Copy and paste the actual 
code. It is hard enough to work out what code does when we can see it, it 
is next to impossible to work out what it does when we're given 
*different* code and have to guess how many changes have been made.


> #Creating an object
> logger=logging.getLogger()
>  
> #Setting the threshold of logger to DEBUG 
> logger.setLevel(logging.DEBUG)
>  
> #Test messages
> logger.debug("Harmless debug Message")
> logger.info("Just an information")
> logger.warning("Its a Warning")
> logger.error("Did you try to divide by zero")
> logger.critical("Internet is down")
> 
> PS: I am running this under Enthought Canopy
> 
> The following is the output
> %run "D:/Projects/Initiatives/machine learning/programs/debug_4.py"

Are you sure that is the OUTPUT?


It looks more like the input -- the command you are running. Have you 
looked inside the file 

D:/Projects/Initiatives/machine learning/programs/newfile.log

where output is going?


By the way, as a total beginner to this, I don't think you need the extra 
complexity and discipline of using log files. Don't try catching errors 
and logging them. Let the errors fail. While you are learning, the BEST 
thing to do is to learn to read the tracebacks and fix the bug in your 
code, not to try to cover them up and bury the failures inside a log file.

If you need informational messages, just use print.

You can come back to logging when you have more experience.

(I've been programming with Python for 20+ years, and I still use print 
for simple scripts and when learning a new library.)




-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson



More information about the Python-list mailing list