Django中使用logging模块
在web应用中记录页面访问情况,在jsp里有filter,
在Django里有middleware。
下面的例子就是在中间件里记录页面访问情况。
settings.py
# Django settings for testsite project.
import logging
DEBUG = True
# DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = '' # Or path to database file if using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '!utms++9y)scz_(1fuwfi&e2z#%c=f(2ltg$b^zdo()mly)&6c'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'middleware.QtsAuthenticationMiddleware',
'logmiddleware.QtsAuthenticationMiddleware',
)
ROOT_URLCONF = 'testsite.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
#'./templates',
'E:/Python25/scripts/testsite/templates',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
)
logging.basicConfig(
level = logging.DEBUG,
#
format = '%(asctime)s %(levelname)s %(module)s.%(funcName)s Line:%(lineno)d %(message)s',
filename = 'filelog.log',
)
#print 'E:/Python25/scripts/testsite/templates',
logmiddleware.py
'''
Created on 2009-4-9
@author: Administrator
'''
from django.http import HttpResponseRedirect
from django.contrib.auth import SESSION_KEY
from urllib import quote
import logging
class QtsAuthenticationMiddleware(object):
def process_request(self, request):
#print request.path
logging.info("enter:" + request.path)
pass
def process_response(self, request, response):
logging.info("exit:" + request.path)
return response
分享到:
相关推荐
详解python的日志模块logging和django中对logging模块的使用
日志在程序开发中是少不了的,通过日志我们可以分析到错误在什么地方,有什么异常。在生产环境下有很大的用处。...下面这篇文章主要介绍了Django日志模块logging的相关资料,需要的朋友可以参考下。
主要介绍了Python Django框架实现应用添加logging日志操作,结合实例形式分析了Django框架中添加Python内建日志模块相关操作技巧,需要的朋友可以参考下
基于邮件通知的服务监控和告警系统 ... 'class':'logging.handlers.RotatingFileHandler', 'filename':os.path.join(LOG_DIR,'backend.log') 'maxBytes':'1024*1024*1024' # class参数如果日志大小超了会新建
它从浏览器接收报告,并对它们执行以下任何/所有操作: 使用python logging模块记录它们。 通过电子邮件将它们发送给您。 通过Django模型将它们保存到数据库中。 在它们上运行任何您自己的自定义函数。 可以生成...
因为最近在做平台,发现有同事,使用django封装了日志模块,看样子很简单,准备自己单独做了一个日志封装模板,对于python不熟练的我,封装部分参考了多个博主的内容,形成自己的日志模块,内容如下: 封装部分 创建...
目录 第1章 Python 处理 cassandra 升级后的回滚脚本 第 2 章 多套方案来提高 python web 框架的并发处理能力 ...第 30 章 python Howto 之 logging 模块 第 31 章 Python FAQ3-python 中 的原始(raw)字符串
05 logging模块 06 re模块补充 07 configparse模块 08 hashlib模块 09 计算器作业以及思路 10 模块导入补充 第24章 01 面向对象设计 02 类相关知识 03 对象相关知识 04 类属性增删改查 05 实例属性的增删改查 06 ...
GoFrame English | GF(GoFrame)是GF(GoFrame)的模块化,强大,高性能和企业级应用程序开发框架。 提供一系列核心组件和许多实用模块,例如:缓存,日志记录,容器,计时器,资源,验证器,数据库orm等。...建议使用go
logging optparse csv collections pdb pty assert 第三方模块 elasticsearch requests mysql consul jira qrcode ping optparse pyinotify ConfigParser 实战应用 scripts wechat tools cap process、thread、...