H
H
hawkkoff2014-08-11 22:20:43
Flask
hawkkoff, 2014-08-11 22:20:43

Why does logging in Flask make more than one entry?

Good afternoon

The problem is this. I made it so that when the page is loaded, the logger leaves a record about connecting to the database and the corresponding message when the page is displayed possible / not possible.

I refresh the page exactly 1 time, I get a
bunch of records, although it is
logical that there should be 2 of them . 2014-08-11 23:10:19; __init__.py:39)
DEBUG:service: db connect - ok (2014-08-11 23:10:20; __init__.py:15)
DEBUG:service: db connect - ok (2014-08-11 23:10:20; __init__.py:15)
DEBUG:service: db connect - ok (2014-08-11 23:10:23; __init__.py:15)
DEBUG:service: 1 (2014-08-11 23:10:23; __init__.py:39)

Code

from flask import Flask, render_template, request, abort, Markup
import sqlite3

#Log config
import logging
import logging.config
logging.config.fileConfig('logging.conf',disable_existing_loggers=False)
logger = logging.getLogger(__name__)


conn = sqlite3.connect('service.db')
c = conn.cursor()
c.execute("SELECT * FROM 'meta'")

logger.debug('db connect - ok')


app = Flask(__name__)
app.config.update(
    DEBUG=True,
    TESTING=True
)

@app.route('/service/<servName>/')
def serviceForm(servName):
  try:
    listPage[pageName]
    app.logger.debug("1")
    return render_template(listPage[pageName]['tempForm'], name=pageName, action=request.path)
  except:
    app.logger.debug("2")
    abort(404)


if __name__ == '__main__':
    app.run()


Of course, I shortened the code, so ideological deadlocks are possible, like "What kind of listPage is it?", you can (I think) close your eyes to this, the code conveys the main problem

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
s1dney, 2014-08-12
@s1dney

when debug is present in the launch parameters and the logging level is not explicitly specified when creating an object of the logging class, it is quite expected that any imported module will spam all the details of its work
into debug when creating a logger, you need to specify a level higher than debug to avoid unwanted spam
, and if debug is needed, then you can use handles for sqlite in this case, like this, specify the desired level for it

sqlite_log = logging.getLogger("sqlite")
sqlite_log.setLevel(logging.ERROR)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question