Add configure method and logger option to choose other logger

master
Guillaume DOTT 2013-05-29 14:31:50 +02:00
parent bbc47fd82f
commit a83de02b0f
3 changed files with 50 additions and 48 deletions

View File

@ -16,6 +16,15 @@ Or install it yourself as:
$ gem install rails_big_brother $ gem install rails_big_brother
## Configuration
```ruby
RailsBigBrother.config do |config|
config.format = "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
config.logger = Rails.logger
end
```
## Usage ## Usage
Simply add this line in the model you want to log : Simply add this line in the model you want to log :
@ -23,18 +32,6 @@ Simply add this line in the model you want to log :
big_brother_watch big_brother_watch
``` ```
### Define log format
The default log format is :
```ruby
"%<big_brother>s;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
```
To define a new format, add this in an initializer :
```ruby
RailsBigBrother.format = "new_format"
```
### User and controller info ### User and controller info
To fill user and controller info, you need to define two methods in your `ApplicationController` To fill user and controller info, you need to define two methods in your `ApplicationController`

View File

@ -3,48 +3,54 @@ require "rails_big_brother/controller"
require "rails_big_brother/model" require "rails_big_brother/model"
module RailsBigBrother module RailsBigBrother
def format=(value) class << self
@format = value attr_writer :format, :logger
end
def format def logger
@format ||= "%<big_brother>s;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s" @logger ||= Rails.logger
end end
def user=(value) def format
store[:user] = value @format ||= "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
end end
def user def configure(&block)
store[:user] yield(self) if block_given?
end end
def controller_info=(value) def user=(value)
store[:controller_info] = value store[:user] = value
end end
def controller_info def user
store[:controller_info] store[:user]
end end
def controller_info_string def controller_info=(value)
case controller_info store[:controller_info] = value
when Array end
controller_info.join(',')
when Hash def controller_info
controller_info.map { |k,v| "#{k}:#{v}" }.join(',') store[:controller_info]
else end
controller_info
def controller_info_string
case controller_info
when Array
controller_info.join(',')
when Hash
controller_info.map { |k,v| "#{k}:#{v}" }.join(',')
else
controller_info
end
end
private
def store
Thread.current[:big_brother_log] ||= {}
end end
end end
private
def store
Thread.current[:big_brother_log] ||= {}
end
extend self
end end
ActiveSupport.on_load(:active_record) do ActiveSupport.on_load(:active_record) do

View File

@ -48,9 +48,8 @@ module RailsBigBrother
end end
def big_brother_log(action, *args) def big_brother_log(action, *args)
Rails.logger.info RailsBigBrother.format % RailsBigBrother.logger.info RailsBigBrother.format %
{ {
big_brother: "big_brother",
user: RailsBigBrother.user, user: RailsBigBrother.user,
controller_info: RailsBigBrother.controller_info_string, controller_info: RailsBigBrother.controller_info_string,
class: self.class.name, class: self.class.name,