background description:
deploy using gunicorn + django + nginx,
info.log and error.log,warn.log are automatically created in the specified directory
Log information can be displayed normally on the screen
No previous log file write information
use runserver to start the service, and the log can be written normally
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "%(levelname)s %(asctime)s %(module)s %(message)s"
},
"error": {
"format": "%(levelname)s %(asctime)s %(pathname)s %(module)s %(message)s"
},
"simple": {
"format": "%(levelname)s %(asctime)s %(message)s"
},
"collect": {
"format": "%(message)s"
}
},
"filters": {
-sharp deubg=True
"require_debug_true": {
"()": "django.utils.log.RequireDebugTrue",
},
},
"handlers": {
"console": {
"level": "DEBUG",
"filters": ["require_debug_true"],
"class": "logging.StreamHandler",
"formatter": "simple"
},
"default": {
"level": "INFO",
"class": "logging.handlers.RotatingFileHandler",
"filename": os.path.join(BASE_LOG_DIR, "info.log"),
"maxBytes": 1024 * 1024 * 50,
"backupCount": 5,
"formatter": "standard",
"encoding": "utf-8",
},
"warn": {
"level": "WARN",
"class": "logging.handlers.RotatingFileHandler",
"filename": os.path.join(BASE_LOG_DIR, "warn.log"),
"maxBytes": 1024 * 1024 * 50,
"backupCount": 5,
"formatter": "standard",
"encoding": "utf-8",
},
"error": {
"level": "ERROR",
"class": "logging.handlers.RotatingFileHandler",
"filename": os.path.join(BASE_LOG_DIR, "error.log"),
"maxBytes": 1024 * 1024 * 50,
"backupCount": 5,
"formatter": "error",
"encoding": "utf-8",
},
},
"loggers": {
"": {
"handlers": ["default", "warn", "error"],
"level": "DEBUG",
"propagate": True,
},
"collect": {
"handlers": ["console", "default", "warn", "error"],
"level": "INFO",
}
},
}
setting.py
from logger import LOGGING
views.py
import logging
logger = logging.getLogger(__name__)
class RandomImagination(APIView):
def post(self, request):
logger.info(u":{} id:{}".format(request.path, user_id, result_dict["msg"]))
return Response({"status":100})