Don't use module level logger functions

This commit is contained in:
Patrick Jentsch 2021-09-16 11:15:31 +02:00
parent 2af74db46f
commit 5469bd0ec1
6 changed files with 48 additions and 39 deletions

View File

@ -1,5 +1,6 @@
from datetime import datetime from datetime import datetime
from flask import abort, flash, redirect, render_template, request, url_for from flask import (abort, current_app, flash, redirect, render_template,
request, url_for)
from flask_login import current_user, login_user, login_required, logout_user from flask_login import current_user, login_user, login_required, logout_user
from sqlalchemy import or_ from sqlalchemy import or_
from . import bp from . import bp
@ -8,7 +9,6 @@ from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
from .. import db from .. import db
from ..email import create_message, send from ..email import create_message, send
from ..models import User from ..models import User
import logging
import os import os
@ -68,7 +68,9 @@ def register():
try: try:
os.makedirs(user.path) os.makedirs(user.path)
except OSError: except OSError:
logging.error('Make dir {} led to an OSError!'.format(user.path)) current_app.logger.error(
'Make dir {} led to an OSError!'.format(user.path)
)
db.session.delete(user) db.session.delete(user)
db.session.commit() db.session.commit()
abort(500) abort(500)

View File

@ -1,4 +1,4 @@
from flask import (abort, flash, make_response, redirect, request, from flask import (abort, current_app, flash, make_response, redirect, request,
render_template, url_for, send_from_directory) render_template, url_for, send_from_directory)
from flask_login import current_user, login_required from flask_login import current_user, login_required
from . import bp from . import bp
@ -11,7 +11,6 @@ from jsonschema import validate
from .. import db from .. import db
from ..models import Corpus, CorpusFile, QueryResult from ..models import Corpus, CorpusFile, QueryResult
import json import json
import logging
import os import os
import shutil import shutil
import glob import glob
@ -34,7 +33,9 @@ def add_corpus():
try: try:
os.makedirs(corpus.path) os.makedirs(corpus.path)
except OSError: except OSError:
logging.error('Make dir {} led to an OSError!'.format(corpus.path)) current_app.logger.error(
'Make dir {} led to an OSError!'.format(corpus.path)
)
db.session.rollback() db.session.rollback()
abort(500) abort(500)
else: else:
@ -61,7 +62,9 @@ def import_corpus():
try: try:
os.makedirs(corpus.path) os.makedirs(corpus.path)
except OSError: except OSError:
logging.error('Make dir {} led to an OSError!'.format(corpus.path)) current_app.logger.error(
'Make dir {} led to an OSError!'.format(corpus.path)
)
db.session.rollback() db.session.rollback()
flash('Internal Server Error', 'error') flash('Internal Server Error', 'error')
return make_response( return make_response(
@ -318,7 +321,9 @@ def add_query_result():
try: try:
os.makedirs(os.path.dirname(query_result.path)) os.makedirs(os.path.dirname(query_result.path))
except OSError: except OSError:
logging.error('Make dir {} led to an OSError!'.format(query_result.path)) # noqa current_app.logger.error(
'Make dir {} led to an OSError!'.format(query_result.path)
)
db.session.rollback() db.session.rollback()
flash('Internal Server Error', 'error') flash('Internal Server Error', 'error')
return make_response( return make_response(

View File

@ -7,7 +7,6 @@ from werkzeug.security import generate_password_hash, check_password_hash
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from . import db, login from . import db, login
import base64 import base64
import logging
import os import os
import shutil import shutil
@ -530,7 +529,9 @@ class CorpusFile(db.Model):
try: try:
os.remove(self.path) os.remove(self.path)
except OSError: except OSError:
logging.error('Removing {} led to an OSError!'.format(self.path)) current_app.logger.error(
'Removing {} led to an OSError!'.format(self.path)
)
pass pass
db.session.delete(self) db.session.delete(self)
self.corpus.status = 'unprepared' self.corpus.status = 'unprepared'

View File

@ -1,5 +1,5 @@
from flask import (abort, flash, make_response, render_template, request, from flask import (abort, current_app, flash, make_response, render_template,
url_for) request, url_for)
from flask_login import current_user, login_required from flask_login import current_user, login_required
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from . import bp from . import bp
@ -8,7 +8,6 @@ from .. import db
from .forms import AddJobForms from .forms import AddJobForms
from ..models import Job, JobInput from ..models import Job, JobInput
import json import json
import logging
import os import os
@ -56,7 +55,9 @@ def service(service):
try: try:
os.makedirs(job.path) os.makedirs(job.path)
except OSError: except OSError:
logging.error('Make dir {} led to an OSError!'.format(job.path)) current_app.logger.error(
'Make dir {} led to an OSError!'.format(job.path)
)
db.session.rollback() db.session.rollback()
flash('Internal Server Error', 'error') flash('Internal Server Error', 'error')
return make_response( return make_response(

View File

@ -1,7 +1,6 @@
from flask import current_app from flask import current_app
from ..models import Corpus from ..models import Corpus
import docker import docker
import logging
import os import os
import shutil import shutil
@ -78,7 +77,7 @@ class CheckCorporaMixin:
restart_policy=restart_policy restart_policy=restart_policy
) )
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Create "{}" service raised '.format(name) 'Create "{}" service raised '.format(name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -91,20 +90,20 @@ class CheckCorporaMixin:
try: try:
service = self.docker.services.get(service_name) service = self.docker.services.get(service_name)
except docker.errors.NotFound: except docker.errors.NotFound:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.NotFound" The service does not exist. ' + '"docker.errors.NotFound" The service does not exist. '
+ '(corpus.status: {} -> failed)'.format(corpus.status) + '(corpus.status: {} -> failed)'.format(corpus.status)
) )
corpus.status = 'failed' corpus.status = 'failed'
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
) )
except docker.errors.InvalidVersion: except docker.errors.InvalidVersion:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.InvalidVersion" One of the arguments is ' + '"docker.errors.InvalidVersion" One of the arguments is '
+ 'not supported with the current API version.' + 'not supported with the current API version.'
@ -121,7 +120,7 @@ class CheckCorporaMixin:
try: try:
service.remove() service.remove()
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Remove "{}" service raised '.format(service_name) 'Remove "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' # noqa + '"docker.errors.APIError" The server returned an error. ' # noqa
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -162,7 +161,7 @@ class CheckCorporaMixin:
except docker.errors.NotFound: except docker.errors.NotFound:
pass pass
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" container raised '.format(name) 'Get "{}" container raised '.format(name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -172,7 +171,7 @@ class CheckCorporaMixin:
try: try:
container.remove(force=True) container.remove(force=True)
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Remove "{}" container raised '.format(name) 'Remove "{}" container raised '.format(name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -184,21 +183,21 @@ class CheckCorporaMixin:
network=network) network=network)
except docker.errors.ContainerError: except docker.errors.ContainerError:
# This case should not occur, because detach is True. # This case should not occur, because detach is True.
logging.error( current_app.logger.error(
'Run "{}" container raised '.format(name) 'Run "{}" container raised '.format(name)
+ '"docker.errors.ContainerError" The container exits with a ' + '"docker.errors.ContainerError" The container exits with a '
+ 'non-zero exit code and detach is False.' + 'non-zero exit code and detach is False.'
) )
corpus.status = 'failed' corpus.status = 'failed'
except docker.errors.ImageNotFound: except docker.errors.ImageNotFound:
logging.error( current_app.logger.error(
'Run "{}" container raised '.format(name) 'Run "{}" container raised '.format(name)
+ '"docker.errors.ImageNotFound" The specified image does not ' + '"docker.errors.ImageNotFound" The specified image does not '
+ 'exist.' + 'exist.'
) )
corpus.status = 'failed' corpus.status = 'failed'
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Run "{}" container raised '.format(name) 'Run "{}" container raised '.format(name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -211,10 +210,12 @@ class CheckCorporaMixin:
try: try:
self.docker.containers.get(container_name) self.docker.containers.get(container_name)
except docker.errors.NotFound: except docker.errors.NotFound:
logging.error('Could not find "{}" but the corpus state is "analysing".') # noqa current_app.logger.error(
'Could not find "{}" but the corpus state is "analysing".'
)
corpus.status = 'prepared' corpus.status = 'prepared'
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" container raised '.format(container_name) 'Get "{}" container raised '.format(container_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -228,7 +229,7 @@ class CheckCorporaMixin:
except docker.errors.NotFound: except docker.errors.NotFound:
pass pass
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" container raised '.format(container_name) 'Get "{}" container raised '.format(container_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -238,7 +239,7 @@ class CheckCorporaMixin:
try: try:
container.remove(force=True) container.remove(force=True)
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Remove "{}" container raised '.format(container_name) 'Remove "{}" container raised '.format(container_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)

View File

@ -5,7 +5,6 @@ from .. import db
from ..models import Job, JobResult from ..models import Job, JobResult
import docker import docker
import json import json
import logging
import os import os
import shutil import shutil
@ -96,7 +95,7 @@ class CheckJobsMixin:
restart_policy=restart_policy restart_policy=restart_policy
) )
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Create "{}" service raised '.format(name) 'Create "{}" service raised '.format(name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -110,21 +109,21 @@ class CheckJobsMixin:
try: try:
service = self.docker.services.get(service_name) service = self.docker.services.get(service_name)
except docker.errors.NotFound: except docker.errors.NotFound:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.NotFound" The service does not exist. ' + '"docker.errors.NotFound" The service does not exist. '
+ '(job.status: {} -> failed)'.format(job.status) + '(job.status: {} -> failed)'.format(job.status)
) )
job.status = 'failed' job.status = 'failed'
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
) )
return return
except docker.errors.InvalidVersion: except docker.errors.InvalidVersion:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.InvalidVersion" One of the arguments is ' + '"docker.errors.InvalidVersion" One of the arguments is '
+ 'not supported with the current API version.' + 'not supported with the current API version.'
@ -141,7 +140,7 @@ class CheckJobsMixin:
try: try:
service.remove() service.remove()
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Remove "{}" service raised '.format(service_name) 'Remove "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' # noqa + '"docker.errors.APIError" The server returned an error. ' # noqa
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -167,14 +166,14 @@ class CheckJobsMixin:
except docker.errors.NotFound: except docker.errors.NotFound:
job.status = 'canceled' job.status = 'canceled'
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
) )
return return
except docker.errors.InvalidVersion: except docker.errors.InvalidVersion:
logging.error( current_app.logger.error(
'Get "{}" service raised '.format(service_name) 'Get "{}" service raised '.format(service_name)
+ '"docker.errors.InvalidVersion" One of the arguments is ' + '"docker.errors.InvalidVersion" One of the arguments is '
+ 'not supported with the current API version.' + 'not supported with the current API version.'
@ -184,7 +183,7 @@ class CheckJobsMixin:
try: try:
service.update(mounts=None) service.update(mounts=None)
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Update "{}" service raised '.format(service_name) 'Update "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)
@ -193,7 +192,7 @@ class CheckJobsMixin:
try: try:
service.remove() service.remove()
except docker.errors.APIError as e: except docker.errors.APIError as e:
logging.error( current_app.logger.error(
'Remove "{}" service raised '.format(service_name) 'Remove "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. ' + '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e) + 'Details: {}'.format(e)