Skip to content

Commit 3966a91

Browse files
feat: Add LOG_LEVEL env variable
1 parent 077866f commit 3966a91

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ UVICORN_PORT = 8000
4848
# LOG_ROTATION_INTERVAL=1
4949
# LOG_ROTATION_UNIT="H" # "S", "M", "H", "D", "W0"-"W6", "midnight"
5050
# LOG_MAX_BYTES=10485760 # 10 MB
51+
# LOG_LEVEL="INFO"
5152
# ECHO_SQL_QUERIES=False
5253
# VITE_BASE_API="https://example.com/"
53-
# JWT_ACCESS_TOKEN_EXPIRE_MINUTES = 1440
54+
# JWT_ACCESS_TOKEN_EXPIRE_MINUTES=1440
5455

5556
# due to high amount of data, this job is only available for postgresql and timescaledb
5657
# ENABLE_RECORDING_NODES_STATS = False

app/utils/logger.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ECHO_SQL_QUERIES,
1111
LOG_BACKUP_COUNT,
1212
LOG_FILE_PATH,
13+
LOG_LEVEL,
1314
LOG_MAX_BYTES,
1415
LOG_ROTATION_ENABLED,
1516
LOG_ROTATION_INTERVAL,
@@ -41,6 +42,9 @@ def formatMessage(self, record: logging.LogRecord) -> str:
4142
'%(levelprefix)s %(asctime)s - %(client_addr)s - "%(request_line)s" %(status_code)s'
4243
)
4344

45+
LOGGING_CONFIG["loggers"]["uvicorn"]["level"] = LOG_LEVEL
46+
LOGGING_CONFIG["loggers"]["uvicorn.error"]["level"] = LOG_LEVEL
47+
LOGGING_CONFIG["loggers"]["uvicorn.access"]["level"] = LOG_LEVEL
4448

4549
if SAVE_LOGS_TO_FILE:
4650
if LOG_ROTATION_ENABLED:

config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
LOG_ROTATION_INTERVAL = config("LOG_ROTATION_INTERVAL", cast=int, default=1)
7171
LOG_ROTATION_UNIT = config("LOG_ROTATION_UNIT", default="H")
7272
LOG_MAX_BYTES = config("LOG_MAX_BYTES", cast=int, default=10485760) # default: 10 MB
73+
VALID_LOG_LEVELS = ("CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG")
74+
LOG_LEVEL = config("LOG_LEVEL", default="INFO").upper()
75+
if LOG_LEVEL not in VALID_LOG_LEVELS:
76+
LOG_LEVEL = "INFO"
7377

7478
# USERNAME: PASSWORD
7579
SUDOERS = (

main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ipaddress
2-
import logging
32
import os
43
import socket
54
import ssl
@@ -14,6 +13,7 @@
1413
from app.utils.logger import LOGGING_CONFIG
1514
from config import (
1615
DEBUG,
16+
LOG_LEVEL,
1717
UVICORN_HOST,
1818
UVICORN_LOOP,
1919
UVICORN_PORT,
@@ -134,10 +134,9 @@ def validate_cert_and_key(cert_file_path, key_file_path):
134134
bind_args["uds"] = None
135135
bind_args["host"] = "0.0.0.0"
136136

137-
log_level = logging.DEBUG if DEBUG else logging.INFO
138-
LOGGING_CONFIG["loggers"]["uvicorn"]["level"] = log_level
139-
LOGGING_CONFIG["loggers"]["uvicorn.error"]["level"] = log_level
140-
LOGGING_CONFIG["loggers"]["uvicorn.access"]["level"] = log_level
137+
effective_log_level = LOG_LEVEL
138+
for logger_name in ("uvicorn", "uvicorn.error", "uvicorn.access"):
139+
LOGGING_CONFIG["loggers"][logger_name]["level"] = effective_log_level
141140

142141
try:
143142
uvicorn.run(
@@ -146,6 +145,7 @@ def validate_cert_and_key(cert_file_path, key_file_path):
146145
workers=1,
147146
reload=DEBUG,
148147
log_config=LOGGING_CONFIG,
148+
log_level=effective_log_level.lower(),
149149
loop=UVICORN_LOOP,
150150
)
151151
except FileNotFoundError: # to prevent error on removing unix sock

0 commit comments

Comments
 (0)