From 19aebcc3271e2bd9228681d1a8928a837467b247 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 26 Jan 2022 21:43:31 +0100 Subject: [PATCH 1/5] Use app logger instead of root logger --- app.py | 2 +- ki/actions/seed.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index b2cc263..dfba6ee 100644 --- a/app.py +++ b/app.py @@ -40,6 +40,6 @@ CORS(app) db = SQLAlchemy(app) migrate = Migrate(app, db, compare_type=True) -logging.debug("Hello from KI") +app.logger.info("Hello from KI") from ki import module # noqa diff --git a/ki/actions/seed.py b/ki/actions/seed.py index 2ccf157..170d58f 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -3,7 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-or-later import csv -import logging from app import app, db from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSearchtopic, \ @@ -13,7 +12,7 @@ from ki.models import Address, Contact, ContactType, Language, Skill, Profile, P def seed_contacttypes(): contacttypes_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/contacttypes.csv" - logging.info("importing contacttypes") + app.logger.info("importing contacttypes") with open(contacttypes_seed_file_path) as file: csv_reader = csv.DictReader(file) From 689a5ba33eccb7e183c75285a000348572276ab6 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 26 Jan 2022 23:17:41 +0100 Subject: [PATCH 2/5] Keep alembic from configuring loggers --- migrations/alembic.ini | 50 ------------------------------------------ migrations/env.py | 3 --- 2 files changed, 53 deletions(-) delete mode 100644 migrations/alembic.ini diff --git a/migrations/alembic.ini b/migrations/alembic.ini deleted file mode 100644 index ec9d45c..0000000 --- a/migrations/alembic.ini +++ /dev/null @@ -1,50 +0,0 @@ -# A generic, single database configuration. - -[alembic] -# template used to generate migration files -# file_template = %%(rev)s_%%(slug)s - -# set to 'true' to run the environment during -# the 'revision' command, regardless of autogenerate -# revision_environment = false - - -# Logging configuration -[loggers] -keys = root,sqlalchemy,alembic,flask_migrate - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = WARN -handlers = console -qualname = - -[logger_sqlalchemy] -level = WARN -handlers = -qualname = sqlalchemy.engine - -[logger_alembic] -level = INFO -handlers = -qualname = alembic - -[logger_flask_migrate] -level = INFO -handlers = -qualname = flask_migrate - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = %(levelname)-5.5s [%(name)s] %(message)s -datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py index 68feded..67e5d0f 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -11,9 +11,6 @@ from alembic import context # access to the values within the .ini file in use. config = context.config -# Interpret the config file for Python logging. -# This line sets up loggers basically. -fileConfig(config.config_file_name) logger = logging.getLogger('alembic.env') # add your model's MetaData object here From 1360b4c7380c9dae71affd893bac10d7fceb5f13 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 26 Jan 2022 23:19:06 +0100 Subject: [PATCH 3/5] Give the root logger a nicer format --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index dfba6ee..ff1edd3 100644 --- a/app.py +++ b/app.py @@ -20,7 +20,7 @@ app = Flask(__name__) loglevel = os.getenv("KI_LOGLEVEL", logging.WARNING) loglevel = int(loglevel) app.logger.setLevel(loglevel) -logging.basicConfig(level=loglevel) +logging.basicConfig(level=loglevel, format='[%(asctime)s] %(levelname)s [%(name)s] %(message)s') # configure root logger as fallback set_library_log_detail_level(BASIC) ldap3_logger.addHandler(default_handler) From 695c88e1591d3455fc68c10c77920d22b4815d40 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 26 Jan 2022 23:19:26 +0100 Subject: [PATCH 4/5] Prevent duplicate log entries --- app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app.py b/app.py index ff1edd3..3ef2a67 100644 --- a/app.py +++ b/app.py @@ -20,6 +20,7 @@ app = Flask(__name__) loglevel = os.getenv("KI_LOGLEVEL", logging.WARNING) loglevel = int(loglevel) app.logger.setLevel(loglevel) +app.logger.propagate = False # do not forward messages to the root logger logging.basicConfig(level=loglevel, format='[%(asctime)s] %(levelname)s [%(name)s] %(message)s') # configure root logger as fallback set_library_log_detail_level(BASIC) From f7e058d3871e798953eb02708e526c84dc0430cf Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 26 Jan 2022 23:34:38 +0100 Subject: [PATCH 5/5] Get rid of more duplicate logging --- app.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 3ef2a67..657ded5 100644 --- a/app.py +++ b/app.py @@ -8,7 +8,6 @@ import os from dotenv import load_dotenv, find_dotenv from flask import Flask from flask_cors import CORS -from flask.logging import default_handler from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy from ldap3.utils.log import logger as ldap3_logger @@ -17,14 +16,15 @@ from ldap3.utils.log import set_library_log_detail_level, BASIC load_dotenv(find_dotenv()) app = Flask(__name__) + +# Configure logging loglevel = os.getenv("KI_LOGLEVEL", logging.WARNING) loglevel = int(loglevel) app.logger.setLevel(loglevel) app.logger.propagate = False # do not forward messages to the root logger logging.basicConfig(level=loglevel, format='[%(asctime)s] %(levelname)s [%(name)s] %(message)s') # configure root logger as fallback - -set_library_log_detail_level(BASIC) -ldap3_logger.addHandler(default_handler) +logging.getLogger('werkzeug').propagate = False # werkzeug has its own ColorStreamHandler +set_library_log_detail_level(BASIC) # ldap3 has different verbosity levels internally app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("SQLALCHEMY_DATABASE_URI") app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False