Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logging only to file

Tags:

python

logging

I have a cronjob running a python script which I added a logging to, but since it's a daily job it is really annoying that I get daily emails of it's function, but I can't seem to find a setting, that will make it log only into the logfile.

#!/usr/bin/python
import logging, logging.handlers
LOGFILENAME = "log.log" 
logging.basicConfig()
log = logging.getLogger("nameoflog")
log.setLevel(logging.DEBUG) 
handler = logging.handlers.WatchedFileHandler(LOGFILENAME)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter("%(asctime)-15s %(levelname)-8s %(name)s %(message)s")) 
log.addHandler(handler)
log.info("something happening")

How is it possible to make logging only write to a file and not to file and STDOUT?

like image 826
fbence Avatar asked Nov 14 '15 17:11

fbence


1 Answers

The problem is that you call logging.basicConfig() which sets up logging to stdout. basicConfig is just a helper method used when you don't want to make more detailed logging calls. Since you setup the methods yourself, you should not call basicConfig.

like image 87
tdelaney Avatar answered Nov 15 '22 22:11

tdelaney