How to write a warning to my log file?

M

MarkyMarc

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 :)
 
J

Jay Loden

MarkyMarc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top