用rubyonrails开发已有三个月左右, 还没有在model中使用过logger.今天碰巧需要在一个pseudo-model(不继承ActiveRecord::Base, 即不对应数据库中的表)中使用logger, 突然不知道应该从哪里获取logger.如果在Controller和Model的方法中, 可以直接引用logger, 例如:
ruby 代码
-
- class ProductsController < ApplicationController
- def index
- logger.info 'index action....'
- end
-
- end
-
-
- class Product < ActiveRecord::Base
- def products_on_sale
- logger.info "products_on_sale"
- end
- end
看了一下
wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging ,基本明白了rails中logger工作原理:
logger的配置是由environment.rb初始化完成的,主要有两个步骤,
首先为整个rails环境定义了一个默认的全局logger:
ruby 代码
- begin
- RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")
- rescue StandardError
- RAILS_DEFAULT_LOGGER = Logger.new(STDERR)
- RAILS_DEFAULT_LOGGER.level = Logger::WARN
- RAILS_DEFAULT_LOGGER.warn(
- "Rails Error: Unable to access log file. Please ensure that log/#{RAILS_ENV}.log exists and is chmod 0666. " +
- "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
- )
- end
接着将这个logger
分别分配给rails中的主要的components: ActiveRecord, ActionController, ActionMailer
ruby 代码
- [ActiveRecord, ActionController, ActionMailer].each { |mod| mod::Base.logger ||= RAILS_DEFAULT_LOGGER }
根据以上原理, 我们可以得出结论:
- 在ActiveRecord/ActionController/ActionMailer中我们可以直接引用logger(如上面代码所示)
- 在除这些rails主要的component之外的类中,可以直接引用全局常量:RAILS_DEFAULT_LOGGER,例如:
ruby 代码
- # this is a pseudo-model class, so it does not associate with any specific rails class
- class CartItem
- def price
- RAILS_DEFAULT_LOGGER.info 'in price method'
- end
- end
分享到:
相关推荐
前端开源库-pseudo-elements伪元素,所有CSS伪元素的列表。
前端开源库-has-pseudo-class具有伪类,确定选择器是否包含伪类
Pseudo-Mask Matters in Weakly-Supervised Semantic Segmentation
2016_Pseudo-Random High-Frequency Square-Wave Voltage Injection Based Sensorless Control of IPMSM Drives for Audible Noise Reduction 王教授的论文 2016_Pseudo-Random High-Frequency Square-Wave ...
Hadoop分布式环境搭建教程一
面向信息检索的群体伪相关反馈技术,万小军,,伪相关反馈技术已经成功应用于改善信息检索质量。现有技术基于单一的搜索引擎(模型),并假设该引擎返回的顶端文档与查询相关。
@ shimyshack / tailwindcss-pseudo-element-plugin 提供一个插件before和after的变体以及pseudo-content-{value}实用工具类,顺风CSS。 安装 从npm安装插件: # Using npm npm install @shimyshack/tailwindcss-...
前端开源库-pseudo-classes.zip
前端开源库-pseudo-elements.zip
前端开源库-has-pseudo-element具有伪元素,确定选择器是否包含伪元素。
前端开源库-pseudo-classes伪类,获取所有CSS伪类的列表。
python库。 资源全名:pseudo-python-0.2.12.tar.gz
tailwindcss-pseudo-elements TailwindCSS插件,添加伪元素的变体( ::before , ::after , ::first-letter等)。 用法 安装 NPM npm install tailwindcss-pseudo-elements --save-dev 纱 yarn add tailwindcss-...
是采用伪随机编码结构光照明主动视觉技术,用编码结构光照明被测场景,实现动态三维场景的重建
资源分类:Python库 所属语言:Python 资源全名:pseudo-0.2.4-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
作者: , , , , , , , 和 引文@inproceedings{qian2020end, title={End-to-End Pseudo-LiDAR for Image-Based 3D Object Detection}, author={Qian, Rui and Garg, Divyansh and Wang, Yan and You, Yurong ...
npm install stylelint-selector-pseudo-class-lvhfa --save-dev 用法 将此配置添加到您的.stylelintrc : { " plugins " : [ " stylelint-selector-pseudo-class-lvhfa " ], " rules " : [ " plugin/...
Pseudo-LiDAR from Visual Depth Estimation.pdf
资源分类:Python库 所属语言:Python 资源全名:pseudo-python-0.2.14.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:pseudo-python-0.2.16.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059