I am trying to use the Python logging library. Instead of printing the messages in the console, I want to write them to a file. Based on the documentation , I'm a little unsure of how to do this. First I imported the logging in and then I created my logging object. Note: I am a little unsure of the name parameter when doing this. Is this suppose to be a filename that has a full filepath? A random name that I just make up?
foldname = "chk_logs"
logFileName = os.path.join(path.abspath(path.curdir),foldname + "\\Fields_Log_" + timeStamp + ".txt") #logging
if os.path.isdir(foldname)==False:
os.mkdir(foldname)
logFile = open(logFileName, "w")
format="%(asctime)s [%(levelname)s] %(message)s"
logging.basicConfig(fileName=logFile, format=format, level=10)
logger = logging.getLogger('name')
So, suppose I want to write a message to the logFile from my logger.
logger.debug("Test")
This essentially does nothing, even after I use logFile.close(). If I try this:
logFile.write(str(logger.debug("Test")))
it basically write 'None' in the .txt file. There must be a simple way to do this; what am I doing wrong?
fileName=logFileNameand don't handle the file yourself.logger.debug("Test")will write to log file, no need to do anything besides that. BTWlogger.{debug,info,warning,error}methods don't return, that's why you seeNonein file.