Skip to content

Commit 5ff6580

Browse files
committed
fix sql echo logging levels
1 parent 51a26c8 commit 5ff6580

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

api/src/feeds/impl/feeds_api_impl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def _get_gtfs_feed(
137137
query = get_gtfs_feeds_query(
138138
db_session=db_session, stable_id=stable_id, include_options_for_joinedload=include_options_for_joinedload
139139
)
140-
self.logger.debug("Query: %s", str(query.statement.compile(compile_kwargs={"literal_binds": True})))
141140
results = query.all()
142141
if len(results) == 0:
143142
return None

api/src/shared/database/database.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from sqlalchemy.orm import sessionmaker
1212
import logging
1313

14+
from utils.logger import get_env_logging_level
15+
1416

1517
def generate_unique_id() -> str:
1618
"""
@@ -99,14 +101,15 @@ def with_db_session(func=None, db_url: str | None = None):
99101
exception occurs, and closed in either case.
100102
- The session is then passed to the decorated function as the 'db_session' keyword argument.
101103
- If 'db_session' is already provided, it simply calls the decorated function with the existing session.
104+
- The echoed SQL queries will be logged if the environment variable LOGGING_LEVEL is set to DEBUG.
102105
"""
103106
if func is None:
104107
return lambda f: with_db_session(f, db_url=db_url)
105108

106109
def wrapper(*args, **kwargs):
107110
db_session = kwargs.get("db_session")
108111
if db_session is None:
109-
db = Database(feeds_database_url=db_url)
112+
db = Database(echo_sql=get_env_logging_level() == 'DEBUG', feeds_database_url=db_url)
110113
with db.start_db_session() as session:
111114
kwargs["db_session"] = session
112115
return func(*args, **kwargs)

api/src/utils/logger.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import json
33
import logging
4+
import os
45
from dataclasses import dataclass
56
from typing import Final, Optional
67

@@ -127,6 +128,12 @@ async def async_emit(self, record):
127128
self.logger.info(json.dumps(log_record.__dict__))
128129

129130

131+
def get_env_logging_level():
132+
"""
133+
Get the logging level from the environment via OS variable LOGGING_LEVEL. Returns INFO if not set.
134+
"""
135+
return os.getenv("LOGGING_LEVEL", "INFO")
136+
130137
class Logger:
131138
"""
132139
Util class for logging information, errors or warnings
@@ -143,7 +150,7 @@ def __init__(self, name):
143150

144151
self.logger = logging.getLogger(name)
145152
self.logger.addHandler(console_handler)
146-
self.logger.setLevel(logging.DEBUG)
153+
self.logger.setLevel(get_env_logging_level())
147154

148155
def get_logger(self):
149156
"""

0 commit comments

Comments
 (0)