Skip to content

Commit

Permalink
Merge pull request #139 from Pet-projects-CodePET/feauture/logging
Browse files Browse the repository at this point in the history
Add logging
  • Loading branch information
Denis-Shtanskiy authored Apr 3, 2024
2 parents 36f3247 + a508c6d commit 63ba8a3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 30 deletions.
79 changes: 79 additions & 0 deletions src/backend/config/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging.config
import re
from os import getenv
from pathlib import Path
Expand Down Expand Up @@ -212,3 +213,81 @@
"schedule": crontab(hour=1, minute=0),
},
}


# Логирование для любого уровня разработки:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"main_format": {
"format": "%(asctime)s - %(levelname)s - %(name)s - %(module)s - %(message)s",
},
},
"handlers": {
"file": {
"level": "INFO",
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "./data/logs/main.log",
"formatter": "main_format",
},
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "main_format",
},
"mail_admins": {
"level": "ERROR",
"class": "django.utils.log.AdminEmailHandler",
"formatter": "main_format",
"email_backend": "django.core.mail.backends.smtp.EmailBackend",
}, # обработчик отправки емайл сообщений администрации сервера
},
"loggers": {
"django": {
"level": "DEBUG",
"handlers": [
"console",
],
"propagate": True,
},
"django.request": {
"level": "DEBUG",
"handlers": [
"file",
],
"propagate": True,
},
"django.db.backends": {
"level": "DEBUG",
"handlers": [
"console",
],
"propagate": True,
},
"django.security": {
"level": "DEBUG",
"handlers": [
"file",
],
"propagate": True,
},
"django.security.csrf": {
"level": "DEBUG",
"handlers": [
"file",
],
"propagate": True,
},
"customlogger": {
"level": "ERROR",
"handlers": [
"mail_admins",
],
},
},
}


def setup_logging():
logging.config.dictConfig(LOGGING)
5 changes: 5 additions & 0 deletions src/backend/config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
"https://devcodepet.tw1.ru",
"http://localhost:3000",
]

LOGGING["loggers"].pop("django.db.backends", None)
LOGGING["loggers"]["django"]["level"] = ["WARNING"]

setup_logging()
70 changes: 40 additions & 30 deletions src/backend/config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,47 @@

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "verbose",
},
},
"loggers": {
"django": {
"level": "INFO",
"handlers": [
"console",
],
},
"django.db.backends": {
"level": "DEBUG",
"handlers": [
"console",
],
"propagate": False,
},
},
}
# LOGGING = {
# "version": 1,
# "disable_existing_loggers": False,
# "formatters": {
# "verbose": {
# "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
# },
# },
# "handlers": {
# "console": {
# "class": "logging.StreamHandler",
# "formatter": "verbose",
# },
# },
# "loggers": {
# "django": {
# "level": "INFO",
# "handlers": [
# "console",
# ],
# },
# "django.db.backends": {
# "level": "DEBUG",
# "handlers": [
# "console",
# ],
# "propagate": False,
# },
# },
# }

LOGGING["handlers"].pop("file", None)
LOGGING["handlers"].pop("mail_admins", None)
LOGGING["loggers"].pop("django.request", None)
LOGGING["loggers"].pop("django.request", None)
LOGGING["loggers"].pop("django.security", None)
LOGGING["loggers"].pop("django.security.csrf", None)
LOGGING["loggers"].pop("customlogger", None)
LOGGING["loggers"]["django"]["level"] = ["INFO"]

setup_logging()

CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
Expand Down
5 changes: 5 additions & 0 deletions src/backend/config/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
"https://testcodepet.tw1.ru",
"http://localhost:3000",
]

LOGGING["loggers"].pop("django.db.backends", None)
LOGGING["loggers"]["django"]["level"] = ["WARNING"]

setup_logging()

0 comments on commit 63ba8a3

Please sign in to comment.