1. MYSQL_CONFIG
# MYSQL CONFIG
MYSQL_CONFIG = {
'ENGINE': 'django.db.backends.mysql',
'HOST': '192.168.*.*',
'PORT': '3309',
'NAME': 'helloworld',
'USER': 'root',
'PASSWD': '123456'
}
# Django中mysql信息设置
DATABASES = {
# 'default': {
# # 'ENGINE': 'django.db.backends.sqlite3',
# # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default': {
'ENGINE': MYSQL_CONFIG.get('ENGINE'),
'HOST': MYSQL_CONFIG.get('HOST'),
'PORT': MYSQL_CONFIG.get('PORT'),
'NAME': MYSQL_CONFIG.get('NAME'),
'USER': MYSQL_CONFIG.get('USER'),
'PASSWORD': MYSQL_CONFIG.get('PASSWD'),
}
}
2. helloworld 项目目录结构
E:\dev\helloworld>tree /f
Folder PATH listing for volume 文档
Volume serial number is 00000137 299D:817A
E:.
│ manage.py
│
├─.idea
│ encodings.xml
│ helloworld.iml
│ misc.xml
│ modules.xml
│ workspace.xml
│
├─hello
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ ├─migrations
│ │ │ __init__.py
│ │ │
│ │ └─__pycache__
│ │ __init__.cpython-36.pyc
│ │
│ └─__pycache__
│ admin.cpython-36.pyc
│ apps.cpython-36.pyc
│ models.cpython-36.pyc
│ views.cpython-36.pyc
│ __init__.cpython-36.pyc
│
├─helloworld
│ │ settings.py
│ │ urls.py
│ │ wsgi.py
│ │ __init__.py
│ │
│ └─__pycache__
│ settings.cpython-36.pyc
│ urls.cpython-36.pyc
│ wsgi.cpython-36.pyc
│ __init__.cpython-36.pyc
│
└─templates
3. settings中配置日志系统
# ================ Django logging begin =========================
import logging
LOGGING_DIR = "/logs/helloworld"
if not os.path.exists(LOGGING_DIR):
os.makedirs(LOGGING_DIR)
LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '[%(levelname)s]-[%(asctime)s]-[%(filename)s]-[%(funcName)s]-[%(lineno)d] >> %(message)s',
},
'simple': {
'format': '[%(funcName)s]-[%(lineno)d] >> %(message)s',
},
},
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'file_handler': {
'encoding': 'utf8',
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'when': 'D',
'interval': 1,
'backupCount': 180,
'filename': '%s/helloworld.log' % LOGGING_DIR,
'formatter': 'standard',
},
},
'loggers': {
'hello': {
'handlers': ['console', 'file_handler'],
'level': 'DEBUG',
'propagate': True,
},
}
}
# ================ Django logging end ==========================
4. Django中日志使用
4.1 hello/views.py
from django.http import HttpResponse
import logging
def get_logger(name=None):
return logging.getLogger(name=name)
logger = get_logger('hello')
def hello(request):
logger.info('this is the hello log for testing.')
return HttpResponse("Hello World!")
4.2 helloworld/urls.py
from hello.views import hello
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url('hello/', hello),
]
5. 日志使用效果
5.1 浏览器调用
5.2 控制台显示
[hello]-[25] >> this is the hello log for testing.
5.3 日志文件显示(/logs/helloworld/helloworld.log)
[INFO]-[2019-12-09 21:27:38,884]-[views.py]-[hello]-[25] >> this is the hello log for testing.