Dolamite Jenkins
asked on
output to a text file a
I want to the logger to output to a text file and not to the screen... How can I do that
import logging, mymodule
logging.basicConfig()
log = logging.getLogger("MyApp")
log.setLevel(logging.DEBUG) #set verbosity to show all messages of severity >= DEBUG
log.info("Starting my app")
try:
mymodule.doIt()
except Exception, e:
log.exception("There was a problem.")
log.info("Ending my app")
ASKER
thanks
ASKER
Pepr When I set up my logger how do I have it record the DEBUG info for every module ?
Frankly, I do not use the logging module, nor I did study deeply the documentation (see http://docs.python.org/howto/logging.html#logging-howto). Because of that I can only guess what is the usual approach. But the following worked for me...
Firstly, you can set the name of the log file in the main application only. Notice that the import of the other modules (here moduleA) is done after that.
myApp.py
The modules also import the logging module, but they do not set the log filename:
moduleA.py
The complication is caused only by the approach when the logger (logging.getLogger()) is calling when the module is imported. Probably more usual approach is the get the logger inside the function that wants to log via the name of the module (I will try and post later).
Firstly, you can set the name of the log file in the main application only. Notice that the import of the other modules (here moduleA) is done after that.
myApp.py
import logging
logging.basicConfig(filename='myApplog.txt')
import moduleA
def main():
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG) #set verbosity to show all messages in the main application
log.info("Starting the application.")
# Calling the module function (causes logging).
moduleA.fn()
log.info("Finishing the application.")
if __name__ == '__main__':
# Start the body of the application.
main()
The modules also import the logging module, but they do not set the log filename:
moduleA.py
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG) #set verbosity to show all messages of severity >= DEBUG
log.info("Starting moduleA")
def fn():
log.info("fn() called")
The complication is caused only by the approach when the logger (logging.getLogger()) is calling when the module is imported. Probably more usual approach is the get the logger inside the function that wants to log via the name of the module (I will try and post later).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you pepr ...
ASKER
Pepr you are the best
You are welcome. And stop kidding :)) You are also the best. You only do not know it, yet. ;) (Part of the reason is that you still have too much hair :)
Try:
Open in new window