Skip to content

Commit

Permalink
improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Nov 21, 2024
1 parent d5ac2f3 commit c50cc03
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
17 changes: 16 additions & 1 deletion mysql/docker-entrypoint-initdb.d/02_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,19 @@ SET
name=replace(name,'-',' '),
normalized_name=replace(name,'-',' ')
WHERE name LIKE 'anonymoususer%'
;
;
INSERT INTO `report_sighting` (`reporting_id`, `reported_id`, `manual_detect`)
VALUES
(1, 2, 0),
(1, 3, 0),
(1, 5, 0),
(2, 3, 0),
(3, 4, 0),
(4, 5, 1),
(5, 6, 0),
(6, 7, 0),
(7, 8, 1),
(8, 9, 0),
(9, 10, 1),
(10, 11, 0)
;
2 changes: 1 addition & 1 deletion src/core/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
settings.DATABASE_URL,
pool_timeout=settings.POOL_TIMEOUT,
pool_recycle=settings.POOL_RECYCLE,
echo=(settings.ENV != "PRD"),
# echo=(settings.ENV != "PRD"),
pool_pre_ping=True,
)

Expand Down
2 changes: 1 addition & 1 deletion src/core/fastapi/middleware/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def dispatch(self, request: Request, call_next):
for key, value in request.query_params.items()
]

logger.debug(
logger.info(
{
"url": request.url.path,
"params": query_params_list,
Expand Down
64 changes: 33 additions & 31 deletions src/core/logging_config.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import json
import logging
import sys

# # log formatting
formatter = logging.Formatter(
json.dumps(
{
"ts": "%(asctime)s",
"name": "%(name)s",
"function": "%(funcName)s",
"level": "%(levelname)s",
"msg": json.dumps("%(message)s"),


# Configure JSON logging
class JsonFormatter(logging.Formatter):
def format(self, record):
log_record = {
"ts": self.formatTime(record, self.datefmt),
"lvl": record.levelname,
"module": record.module,
"func": record.funcName,
"line": record.lineno,
"msg": record.getMessage(),
}
)
)
if record.exc_info:
log_record["exception"] = self.formatException(record.exc_info)
return json.dumps(log_record)


class IgnoreSQLWarnings(logging.Filter):
def filter(self, record):
ignore_messages = ["Unknown table", "Duplicate entry"]
# Check if any of the ignore messages are in the log record message
if any(msg in record.getMessage() for msg in ignore_messages):
return False # Don't log
return True # Log

stream_handler = logging.StreamHandler(sys.stdout)

stream_handler.setFormatter(formatter)
# Set up the logger
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())

handlers = [stream_handler]
logging.basicConfig(level=logging.INFO, handlers=[handler])

logging.basicConfig(level=logging.DEBUG, handlers=handlers)

# set imported loggers to warning
logging.getLogger("urllib3").setLevel(logging.DEBUG)
logging.getLogger("uvicorn").setLevel(logging.DEBUG)
logging.getLogger("aiomysql").setLevel(logging.ERROR)
logging.getLogger("aiokafka").setLevel(logging.WARNING)

# if settings.ENV == "PRD":
# uvicorn_error = logging.getLogger("uvicorn.error")
# uvicorn_error.disabled = True
# uvicorn_access = logging.getLogger("uvicorn.access")
# uvicorn_access.disabled = True

# # https://github.com/aio-libs/aiomysql/issues/103
# # https://github.com/coleifer/peewee/issues/2229
# warnings.filterwarnings("ignore", ".*Duplicate entry.*")
logging.getLogger("asyncmy").addFilter(IgnoreSQLWarnings())
# logging.getLogger("urllib3").setLevel(logging.DEBUG)
# logging.getLogger("uvicorn").setLevel(logging.DEBUG)
# logging.getLogger("aiomysql").setLevel(logging.ERROR)
# logging.getLogger("aiokafka").setLevel(logging.WARNING)

0 comments on commit c50cc03

Please sign in to comment.