1 Commits
v2.03 ... v2.04

Author SHA1 Message Date
RobbieHan
00f7112b67 logging 2018-12-15 15:34:27 +08:00
3 changed files with 77 additions and 3 deletions

View File

@@ -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!")

View File

@@ -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
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',
}
}
}

View File

@@ -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<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT}),
]
]