简体   繁体   中英

Not able to write into log file using logger module

ts = str(datetime.datetime.now())
log = "DIF_logfile_" + ts
logging.basicConfig(filename=log, format='%(asctime)s %(message)s', filemode='w')
logger = logging.getLogger()
config.read(sys.argv[1])
mode = logger.error
logger.setLevel(mode)
for tbl in table:
    try:hive_tbl = config.get(tbl, "hive_tbl")
    except:logger.error("Table doesn't exists")
    #except:print "Table doesn't exists"

When the tbl value passed is wrong, I'm not able to write exception to the log file, but if I print its coming in console. I'm passing tbl value from config file.

This code should not even work, these lines :

logger = logging.getLogger()
mode = logger.error
logger.setLevel(mode)

Should be like this:

logger = logging.getLogger()
mode = logging.ERROR
logger.setLevel(mode)

After I changed them it worked. One more mention is that be sure that these lines:

ts = str(datetime.datetime.now())
log = "DIF_logfile_"+ts

Create a valid filename, they did not on my system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM