From 00f7112b67722ddbcf6af268666c9b01fcaa41c0 Mon Sep 17 00:00:00 2001 From: RobbieHan Date: Sat, 15 Dec 2018 15:34:27 +0800 Subject: [PATCH] logging --- apps/cmdb/tests.py | 19 +++++++++++++++ sandboxMP/settings.py | 55 ++++++++++++++++++++++++++++++++++++++++++- sandboxMP/urls.py | 6 +++-- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/apps/cmdb/tests.py b/apps/cmdb/tests.py index 7ce503c..f8783cd 100644 --- a/apps/cmdb/tests.py +++ b/apps/cmdb/tests.py @@ -1,3 +1,22 @@ from django.test import TestCase # Create your tests here. +from django.views.generic.base import View +from django.shortcuts import HttpResponse +import logging +from .models import Code + +info_logger = logging.getLogger('sandbox_info') +error_logger = logging.getLogger('sandbox_error') + + +class TestLoggingView(View): + + def get(self, request): + print('a') + info_logger.info('The system print a letter "a" ') + try: + Code.objects.get(id=100) + except Exception as e: + error_logger.error(e) + return HttpResponse("OK!") \ No newline at end of file diff --git a/sandboxMP/settings.py b/sandboxMP/settings.py index a4f734f..8eecb8b 100644 --- a/sandboxMP/settings.py +++ b/sandboxMP/settings.py @@ -154,10 +154,63 @@ SAFE_URL = [r'^/$', '/media/', '/admin/', '/ckeditor/', + '/test/', ] # session timeout SESSION_COOKIE_AGE = 60 * 20 SESSION_EXPIRE_AT_BROWSER_CLOSE = True -SESSION_SAVE_EVERY_REQUEST = True \ No newline at end of file +SESSION_SAVE_EVERY_REQUEST = True + + +# logging config + +BASE_LOG_DIR = os.path.join(BASE_DIR, 'slogs') + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'standard': { + 'format': '[%(asctime)s][task_id:%(name)s][%(levelname)s]' + '[%(filename)s:%(lineno)d][%(message)s]' + }, + 'simple': { + 'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' + }, + + }, + 'handlers': { + 'default': { + 'level': 'INFO', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': os.path.join(BASE_LOG_DIR, "sandbox_info.log"), + 'maxBytes': 1024 * 1024 * 50, + 'backupCount': 3, + 'formatter': 'simple', + 'encoding': 'utf-8', + }, + 'error': { + 'level': 'ERROR', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': os.path.join(BASE_LOG_DIR, "sandbox_err.log"), + 'backupCount': 5, + 'formatter': 'standard', + 'encoding': 'utf-8', + } + + }, + 'loggers': { + 'sandbox_info': { + 'handlers': ['default'], + 'level': 'INFO', + 'propagate': True, + }, + 'sandbox_error': { + 'handlers': ['error'], + 'level': 'ERROR', + } + } + +} \ No newline at end of file diff --git a/sandboxMP/urls.py b/sandboxMP/urls.py index 8386877..9422131 100644 --- a/sandboxMP/urls.py +++ b/sandboxMP/urls.py @@ -20,7 +20,7 @@ from django.urls import re_path from django.views.static import serve from system.views_user import IndexView, LoginView, LogoutView - +from cmdb.tests import TestLoggingView urlpatterns = [ path('admin/', admin.site.urls), @@ -30,10 +30,12 @@ urlpatterns = [ path('system/', include('system.urls', namespace='system')), path('cmdb/', include('cmdb.urls', namespace='cmdb')), + path('test/', TestLoggingView.as_view()), + ] if settings.DEBUG: urlpatterns += [ re_path(r'^media/(?P.*)$', serve, {"document_root": settings.MEDIA_ROOT}), - ] \ No newline at end of file + ]