-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5ac2f3
commit c50cc03
Showing
4 changed files
with
51 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |