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

  logger = logging.getLogger('werkzeug')
    handler = RotatingFileHandler("/logs/logger.log", maxBytes=20480000, backupCount=50)
    error_handler = RotatingFileHandler("/logs/error_logger.log", maxBytes=20480000, backupCount=50)
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\", 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\", line 18, in execfile
        exec(compile(contents+"\n", file, 'exec'), glob, loc)
      File "C:/git/", line 125, in <module>"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 ?
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
the problem is i want flask to write to the log without me scatter errors logs all over
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

    '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__)
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  show csv file in flask template.html rr28rizal 8 32,461 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  Read owl file using python flask Gayathri 1 1,988 Nov-20-2019, 12:56 PM
Last Post: ChislaineWijdeven
  Create .exe file for Python flask website. vintysaw 4 16,489 Nov-18-2019, 07:56 AM
Last Post: tonycstech
  can i merge stderr and stdout? Skaperen 2 1,444 Sep-29-2019, 03:39 AM
Last Post: Skaperen
  Flask generating a file for download darktitan 0 3,041 Dec-30-2018, 02:02 PM
Last Post: darktitan
  requests - handling exceptions Truman 2 2,415 Nov-13-2018, 11:54 PM
Last Post: Truman
  CRUD performing but output not shown in table format in flask lunchcook 2 3,695 Oct-23-2018, 03:42 PM
Last Post: lunchcook
  Flask: Error output to the browser instead of error_log nikos 1 2,392 Sep-28-2018, 12:49 PM
Last Post: thomasp
  [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! KirkmanJ 2 6,280 Jun-25-2018, 02:24 PM
Last Post: KirkmanJ
  How to perform predictions on a data in csv file using Flask? manjusavanth 0 4,134 May-23-2018, 09:03 AM
Last Post: manjusavanth

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020