0

I have this block of code here that is expected to output the text "Write to log." to the '{name}_Output.txt' file in the 'output-logs/{name}' directory every time the ../write_to_log page is accessed. The text shows up properly in console and the log file is generated in the correct directory location, but nothing is being written to the file itself. Any ideas would be appreciated.

from flask import Flask, request, jsonify, render_template
import os

app = Flask(__name__)

def output_log(test_name):
    def setup_logging_print(self, message, *args, **kws):
        self.log(55, message, *args, **kws) 
    logging.addLevelName(55,'CUSTOM_LOG_LEVEL') # https://stackoverflow.com/a/16955098/1461558
    logging.Logger.print = setup_logging_print
    logging.basicConfig(level = 55, format = '%(message)s', file = "output-logs/"+test_name+"/"+test_name+"_Output.txt")
    return logging.getLogger(__name__)

@app.route('/write_to_log')
def get_patient_fill_data():
    test_name = "demo"
    l = output_log(test_name)
    l.print("Write to log.")
    return "Write to log."


if __name__ == "__main__":
    from waitress import serve
    import logging # for logging to screen AND file
    serve(app, host="0.0.0.0", port=8443, url_scheme='https') 

1 Answers1

0

Import the logging module from Python and setup the logger properly. Double check the official Python Logs: Logging HOW To - How to Log to a file

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
Marcel
  • 199
  • 1
  • 18