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
#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 ?
Reply
#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
Reply
#3
the problem is i want flask to write to the log without me scatter errors logs all over
Reply
#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__)
Reply
#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
Reply


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

Forum Jump:

User Panel Messages

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