By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

How to write a warning to my log file?

P: n/a
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 :-)

Jul 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.