MarkyMarc wrote:
> Hi All,
> 
> A small newbie Q.
> 
> I have made a nice log function in me program. The program writes some
> data to me mysql database.
> When I write to the database I get som warnings back.
> 
> Have do I write these to me log file?
> I know I have to use the the warnings api. But I can not figure out
> how to use it.
> Is there anyone take might give me a small example.
> 
> THX all :-)

It looks like what you need to do is override the warnings module's showwarning 
function:

showwarning(message, category, filename, lineno[, file])
    Write a warning to a file. The default implementation calls 
    formatwarning(message, category, filename, lineno) and writes 
    the resulting string to file, which defaults to sys.stderr. 
    You may replace this function with an alternative implementation 
    by assigning to warnings.showwarning.


Example:

#!/usr/bin/python

import warnings

def mywarn(message, category, filename, lineno, file="warnings.log"):
    handle = open(file, 'a')
    handle.write(warnings.formatwarning(message, category, filename, lineno))
    handle.close()

warnings.showwarning = mywarn

#now test it
warnings.warn('test warn message')

-Jay
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to