Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 flask How to output stderr and exceptions to log file
#1
i have flask application which simple logger setup

  logger = logging.getLogger('werkzeug')
    logger.setLevel(logging.INFO)
    handler = RotatingFileHandler("/logs/logger.log", maxBytes=20480000, backupCount=50)
    error_handler = RotatingFileHandler("/logs/error_logger.log", maxBytes=20480000, backupCount=50)
    error_handler.setLevel(logging.ERROR)
    logger.addHandler(handler)
    logger.addHandler(error_handler)
the problem is that when i have for example error like this is dons't writen to log:

 Traceback (most recent call last):
      File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
        pydev_imports.execfile(file, globals, locals)  # execute the script
      File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
        exec(compile(contents+"\n", file, 'exec'), glob, loc)
      File "C:/git/myapp.py", line 125, in <module>
        logger.info("In read_file - create_log_history - Copying log from %s to %s" % (notice_msg))
    TypeError: not enough arguments for format string
how can i set up the logger to write Traceback errors to log ?
Quote
#2
there is exc_info parameter for logger
logger.error('my error', exc_info=1)
which with any true-ish value will log traceback as well
Quote
#3
the problem is i want flask to write to the log without me scatter errors logs all over
Quote
#4
Flask has build in support for Logging.
Using dictConfig can eg setup like this.
Now will flask.log be in the static folder.
from flask import Flask, render_template, jsonify, request
from logging.config import dictConfig

dictConfig({
    'version': 1,
    'handlers': {
        'file.handler': {
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'static/flask.log',
            'maxBytes': 10000000,
            'backupCount': 5,
            'level': 'DEBUG',
        },
    },
    'loggers': {
        'werkzeug': {
            'level': 'DEBUG',
            'handlers': ['file.handler'],
        },
    },
})

app = Flask(__name__)
Quote
#5
Thanks, but this is not what i was asking, i need to find way to output Traceback to the log , without me scatter around log's
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Read owl file using python flask Gayathri 1 424 Nov-20-2019, 12:56 PM
Last Post: ChislaineWijdeven
  Create .exe file for Python flask website. vintysaw 4 2,611 Nov-18-2019, 07:56 AM
Last Post: tonycstech
  can i merge stderr and stdout? Skaperen 2 297 Sep-29-2019, 03:39 AM
Last Post: Skaperen
  Flask generating a file for download darktitan 0 948 Dec-30-2018, 02:02 PM
Last Post: darktitan
  requests - handling exceptions Truman 2 844 Nov-13-2018, 11:54 PM
Last Post: Truman
  CRUD performing but output not shown in table format in flask lunchcook 2 1,519 Oct-23-2018, 03:42 PM
Last Post: lunchcook
  Flask: Error output to the browser instead of error_log nikos 1 837 Sep-28-2018, 12:49 PM
Last Post: thomasp
  [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! KirkmanJ 2 3,501 Jun-25-2018, 02:24 PM
Last Post: KirkmanJ
  How to perform predictions on a data in csv file using Flask? manjusavanth 0 2,024 May-23-2018, 09:03 AM
Last Post: manjusavanth
  show csv file in flask template.html rr28rizal 5 21,118 Nov-12-2017, 01:53 PM
Last Post: rr28rizal

Forum Jump:


Users browsing this thread: 1 Guest(s)