I'm trying to migrate from elaborate, if(debug):
flagged, print statements for debugging to using the builtin logging
module. I really like that I can log output both to a log file, and to stdout
(for example, see this question). I'm interested in doing the reverse; can I include stdout information into the logger?
Edit: I dont want to "Redirect" stdout to the log, I want to include it in the log... in addition to going to stdout.
For example,
print "Loading data"
sources = getSources(params)
logging.debug("> From %d sources" % (len(sources)))
data = loadData(sources)
def getSources(params):
logging.info(">> Using parameters : %s" % (params))
...
return sources
Let's say that logging
is sending to both stdout
and to a log file. It's great that "Loading Data" is always printed, and I can choose how much of the next two statements are:
Loading Data
> From ___ sources
>> Using parameters : ___
But how can I also get "Loading Data" logged to the log file?
The two options I've considered are:
1) Writing two statements which is annoying and clumsy:
print "Loading Data"
logging.info("Loading Data")
or, 2) using critical
to log for all levels, but this seems abusive...
logging.critical("Loading Data")