-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging.py
29 lines (25 loc) · 1.06 KB
/
logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys
import logging
from typing import Optional
from .settings import IS_GUNICORN, ENV
# TODO: consider adding custom formatting that automatically adds request context
# to all logs, like who the requesting user is and what URL they're accessing, e.g.
def get_logger(name: Optional[str]) -> logging.Logger:
"""Get a configured logger with the given `name`."""
# If the app is running in gunicorn, inherit all config from gunicorn's loggers.
# Otherwise, configure a logger with reasonable defaults.
logger = logging.getLogger(name)
if IS_GUNICORN:
gunicorn_logger = logging.getLogger("gunicorn.error")
logger.handlers = gunicorn_logger.handlers
logger.setLevel(gunicorn_logger.level)
else:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(
logging.Formatter(
"[%(asctime)s] [%(threadName)s] [%(levelname)s]: %(message)s"
)
)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG if ENV == "dev" else logging.INFO)
return logger