apache2+djiango+logging configuration
来源:互联网 发布:网络运营学校 编辑:程序博客网 时间:2024/06/16 04:24
坦白说,这条路上的坑不少,因为一个配置不慎,就会说你的项目跑不起来;因为这个原因,很多人都不敢在djiango的settings.py里进行logging配置了,而是为了省事,直接看/var/www/apache2下的log.但这个log没有配置成自动给你切割文件及限定文件大小,这对于我来说是不能接受的,用东东,就要用好的。不啰嗦,上方案。
项目所在目录/home/page/mysite
1.修改djiango的settings.py文件,加上logging配置.
# Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/1.6/howto/static-files/MEDIA_URL = '/media/'STATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR,'static')STATICFILES_DIRS = [ os.path.join(BASE_DIR, "blog/static"),]STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',# 'django.contrib.staticfiles.finders.DefaultStorageFinder',)SESSION_ENGINE = 'django.contrib.sessions.backends.file'LOGGING = { "version": 1, "disable_existing_loggers": False, "formatters": { "simple": { "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" } }, "handlers": { "console": { "class": "logging.StreamHandler", "level": "DEBUG", "formatter": "simple", "stream": "ext://sys.stdout" }, "info_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "INFO", "formatter": "simple", "filename": os.path.join(STATIC_ROOT+'/logs/','info.log'), "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" }, "error_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "ERROR", "formatter": "simple", "filename": os.path.join(STATIC_ROOT+'/logs/','error.log'), "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" } }, "loggers": { "my_module": { "level": "INFO", "handlers": ["info_file_handler"], "propagate": "no" } }, "root": { "level": "INFO", "handlers": ["console"] } }
2.在/home/page/mysite下创建目录/static/log
3.在python文件里如何使用logging.
import logginglogger = logging.getLogger('my_module')# Create your views here.def index(request): #blog_list = BlogsPost.objects.all() blog_list = [{"title":"111111111","timestamp":"kkkkkkkk","body":"jjjjjjjjjjj"},{"title":"222222222222","timestamp":"kkkkkkkk","body":"jjjjjjjjjjj"}] return render_to_response('index.html',{'blog_list':blog_list})@api_view(['GET', 'POST'])def hello(request): blog_list = BlogsPost.objects.all() cur = connection.cursor() cur.execute("select * from blog_blogspost;") rows = cur.fetchall() connection.commit() cur.close() logger.info('Start reading database') # read database here records = {'john': 55, 'tom': 66} logger.debug('Records: %s', records) logger.info('Updating records ...') # update records here logger.info('Finish updating records') if(rows):message=rows[0] else:message = { "count": 4, "next": "http://localhost:8000/api/entries/?limit=2&offset=2", "previous": None, "results": [{ "id": 1, "title": "Hello, Django REST Framework!!", "body": "Hello!", "created_at": "2015-12-12T11:55:22.310203Z", "status": "draft", "author": 1},{ "id": 2, "title": "The Zen of Python", "body": "The Zen of Python, by Tim Peters\r\n\r\nBeautiful is better than ugly.\r\nExplicit is better than implicit.\r\nSimple is better than complex.\r\nComplex is better than complicated.\r\nFlat is better than nested.\r\nSparse is better than dense.\r\nReadability counts.\r\nSpecial cases aren't special enough to break the rules.\r\nAlthough practicality beats purity.\r\nErrors should never pass silently.\r\nUnless explicitly silenced.\r\nIn the face of ambiguity, refuse the temptation to guess.\r\nThere should be one-- and preferably only one --obvious way to do it.\r\nAlthough that way may not be obvious at first unless you're Dutch.\r\nNow is better than never.\r\nAlthough never is often better than *right* now.\r\nIf the implementation is hard to explain, it's a bad idea.\r\nIf the implementation is easy to explain, it may be a good idea.\r\nNamespaces are one honking great idea -- let's do more of those!", "created_at": "2015-12-12T11:56:32.854278Z", "status": "draft", "author": 2} ]} return Response(message)
4.在/home/page/mysite/static/logs查看error.log及info.log
5.在/var/log/apache2里查看error.log
可以看到两处log都记录了
关键点在下面这句
"disable_existing_loggers": False,
另外会有坑的是如果你在python方法里用了print语句,在/var/log/apache2/error.log文件里就会出现wgi_err.这个问题我找了许多文章才找到解决方法,希望后人不要再踩坑
阅读全文
0 0
- apache2+djiango+logging configuration
- ubuntu16.04+apache2+djiango
- Axis Logging Configuration
- Spark Streaming Logging Configuration
- Spark Streaming Logging Configuration
- Apache2 virtual folder configuration
- elasticsearchException[Failed to load logging configuration]
- linux 下安装Djiango
- pycharm中的djiango命令
- Apache Kylin无法启动,卡在 “Logging initialized using configuration..."
- JBossAS 6.1.0.Final启动报Unable to read the logging configuration from 'file:logging.properties'
- The configuration section for Logging cannot be found in the configuration source
- Struts2 ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging
- ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only err
- ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only err
- apache2.4 AH01630: client denied by server configuration错误
- djiango学习笔记(一)
- Logging
- qtableview + QAbstractTableModel 表头排序
- Java陷阱一箩筐----面试题集
- Zabbix3.0 配置模板监控windows agent端的内存及磁盘容量并触发报警。
- jquery ajax参数详解
- 将后台JSON数据填充Form表单
- apache2+djiango+logging configuration
- 网络安全事件频发,怎么保护自己的生物识别信息?
- Manifest merger failed with multiple errors, Error:Execution failed for task ':app:processDebu
- tcp/ip协议栈--socket API 之accept()
- Cookie实现记住密码的功能
- Android实现RecyclerView侧滑删除和长按拖拽-ItemTouchHelper
- BZOJ1066 (洛谷P2472) [SCOI2007]蜥蜴
- spark dataframe 将一列展开,该列所有值都变成新列
- E