Flask logging - Cannot get it to write to a file

后端 未结 7 1216
野趣味
野趣味 2020-12-04 11:30

Ok, here\'s the code where I setup everything:

if __name__ == \'__main__\':
    app.debug = False

    applogger = app.logger

    file_handler = FileHandler         


        
7条回答
  •  情深已故
    2020-12-04 12:07

    Logging Quick start

    -- This code will not work with more than one log file inside a class/or import

    import logging
    import os # for Cwd path 
    path = os.getcwd()
    
    
    logFormatStr = '%(asctime)s  %(levelname)s - %(message)s'
    logging.basicConfig(filename=path + '\logOne.log', format=logFormatStr, level=logging.DEBUG), logging.info('default message')
    

    for Multiple logging file

    creating a instance of logging using logging.getLogger() method---

    1. for each logger file required one instance of logging
    2. we can create multiple log file but not with same instance

    Create new logger instance with name or Hardcore_String ----preferred (name) this will specify exact class from where it call

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    

    type of logging -- INFO, DEBUG, ERROR, CRITICAL, WARNING

    DEBUG----Detailed information, typically of interest only when diagnosing problems.

    INFO----Confirmation that things are working as expected.

    WARNING----An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

    ERROR----Due to a more serious problem, the software has not been able to perform some function.

    CRITICAL----A serious error, indicating that the program itself may be unable to continue running.

    Create New Formatter

    format = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
    

    create new file Handler

    file_handel = logging.FileHandler(path + '\logTwo.log')
    

    set format to FileHandler & add file_handler to logging instance [logger]

    file_handel.setFormatter(format)
    logger.addHandler(file_handel)
    

    add a message to logOne.log file and logTwo.log with respective setlevel

    logger.info("message for logOne")
    logging.debug(" message for logTwo")
    

    for more details

提交回复
热议问题