Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Only log DB queries when enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Apr 3, 2015
1 parent b448ef0 commit 461b82b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion docs/deployment/@relengapi/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ This is effective for development, but certainly not for production.
The database for the relengapi core is named ``relengapi``.
Other blueprints may require additional URIs.

If you ever need to see what SQLAlchemy is doing with the connection pool, it is useful to enable the logging. To do that make sure you have the ``SQLALCHEMY_DB_LOG`` is flag to set to True.
If you ever need to see what SQLAlchemy is doing with the connection pool, it is useful to enable verbose query logging.
To do so, set ``SQLALCHEMY_DB_LOG = True``.
Note that this output is *very* verbose and may severely impact site performance.

.. _Deployment-Authentication:

Expand Down
11 changes: 6 additions & 5 deletions relengapi/lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
from sqlalchemy.pool import Pool


# db logging
logger = logging.getLogger(__name__)
logging.basicConfig()


class _QueryProperty(object):
Expand Down Expand Up @@ -84,9 +82,12 @@ def _get_db_config(self, dbname):

@synchronized(threading.Lock())
def engine(self, dbname):
# Get the log level for db logs
if self.app.config.get('SQLALCHEMY_DB_LOG'):
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
# Set the log level for db logs
sqla_logger = logging.getLogger('sqlalchemy.engine')
if self.app.config.get('SQLALCHEMY_DB_LOG', False):
sqla_logger.setLevel(logging.INFO)
else:
sqla_logger.setLevel(logging.WARNING)

if dbname not in self._engines:
uri = self._get_db_config(dbname)
Expand Down
5 changes: 2 additions & 3 deletions settings_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
# configuration is present, is to use '*.db' files in the directory containing
# the RelengAPI source code.

# set the flag True for logging SQLAlchemy engine activities
# default it is set to True
SQLALCHEMY_DB_LOG = True
# set to True to log SQLAlchemy engine activities (very verbose!)
# SQLALCHEMY_DB_LOG = False

# ===== Authentication and Authorization =====

Expand Down

0 comments on commit 461b82b

Please sign in to comment.