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
## 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
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
```
### 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
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"
module RailsBigBrother
def format=(value)
@format = value
end
class << self
attr_writer :format, :logger
def format
@format ||= "%<big_brother>s;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
end
def logger
@logger ||= Rails.logger
end
def user=(value)
store[:user] = value
end
def format
@format ||= "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
end
def user
store[:user]
end
def configure(&block)
yield(self) if block_given?
end
def controller_info=(value)
store[:controller_info] = value
end
def user=(value)
store[:user] = value
end
def controller_info
store[:controller_info]
end
def user
store[:user]
end
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
def controller_info=(value)
store[:controller_info] = value
end
def controller_info
store[:controller_info]
end
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
private
def store
Thread.current[:big_brother_log] ||= {}
end
extend self
end
ActiveSupport.on_load(:active_record) do

View File

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