Re: [Tutor] logging question

2011-10-20 Thread Albert-Jan Roskam
Maybe something like this? I don't know what data type 'book' is, 
and what Exception would be raised in case someting goes wrong.import logging
logging.basicConfig(filename='example.log',level=logging.ERROR)
for book in results: 
  try:
    result = process(book)
    logging.info('Everything fine with this book')
  except SomeException:
    logging.error('Something wrong with this book')

 
 
logging.debug('This message should go to the log file') logging.info('So should 
this') logging.warning('And this, too') 


Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~



From: Alex Hall mehg...@gmail.com
To: tutor tutor@python.org
Sent: Wednesday, October 19, 2011 9:19 PM
Subject: [Tutor] logging question

Hi all,
I have never done logging before, and I am wondering how it will
change my script. Currently, I have something like this:
for book in results:
try: checkForErrors(book)
except Exception, e:
  print e
  continue

That way I see any errors in a given book, but that book is skipped
and the loop continues. Now, though, checkForErrors() logs exceptions
instead of raising them, so my try/except won't work, right? There is
my question: if a method logs an exception instead of raising it, is
that exception still raised by the logging module? Do I have to make
checkForErrors() return something, and check for that, instead of
using try/except or can I keep my loop how it is? TIA!

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] logging question

2011-10-20 Thread Christian Witts

On 2011/10/19 09:19 PM, Alex Hall wrote:

Hi all,
I have never done logging before, and I am wondering how it will
change my script. Currently, I have something like this:
for book in results:
  try: checkForErrors(book)
  except Exception, e:
   print e
   continue

That way I see any errors in a given book, but that book is skipped
and the loop continues. Now, though, checkForErrors() logs exceptions
instead of raising them, so my try/except won't work, right? There is
my question: if a method logs an exception instead of raising it, is
that exception still raised by the logging module? Do I have to make
checkForErrors() return something, and check for that, instead of
using try/except or can I keep my loop how it is? TIA!



If you have some exception handling and want it to propagate further up 
the chain you can just raise it, for eg.


def checkForErrors(book):
try:
do_something_that_could_raise_exceptions()
except Exception, e:
log_errors(e)
raise

for book in results:
try:
checkForErrors(book)
except Exception, e:
do_your_other_exception_handling()

--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] logging question

2011-10-20 Thread Alex Hall
Thanks, raise should do it. I read later on last night that raise
called with nothing else re-raises the last exception, but never
thought of using it in my situation.

On 10/20/11, Christian Witts cwi...@compuscan.co.za wrote:
 On 2011/10/19 09:19 PM, Alex Hall wrote:
 Hi all,
 I have never done logging before, and I am wondering how it will
 change my script. Currently, I have something like this:
 for book in results:
   try: checkForErrors(book)
   except Exception, e:
print e
continue

 That way I see any errors in a given book, but that book is skipped
 and the loop continues. Now, though, checkForErrors() logs exceptions
 instead of raising them, so my try/except won't work, right? There is
 my question: if a method logs an exception instead of raising it, is
 that exception still raised by the logging module? Do I have to make
 checkForErrors() return something, and check for that, instead of
 using try/except or can I keep my loop how it is? TIA!


 If you have some exception handling and want it to propagate further up
 the chain you can just raise it, for eg.

 def checkForErrors(book):
  try:
  do_something_that_could_raise_exceptions()
  except Exception, e:
  log_errors(e)
  raise

 for book in results:
  try:
  checkForErrors(book)
  except Exception, e:
  do_your_other_exception_handling()

 --

 Christian Witts
 Python Developer

 //



-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] logging question

2011-10-19 Thread Alex Hall
Hi all,
I have never done logging before, and I am wondering how it will
change my script. Currently, I have something like this:
for book in results:
 try: checkForErrors(book)
 except Exception, e:
  print e
  continue

That way I see any errors in a given book, but that book is skipped
and the loop continues. Now, though, checkForErrors() logs exceptions
instead of raising them, so my try/except won't work, right? There is
my question: if a method logs an exception instead of raising it, is
that exception still raised by the logging module? Do I have to make
checkForErrors() return something, and check for that, instead of
using try/except or can I keep my loop how it is? TIA!

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor