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,12 +3,19 @@ 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
def logger
@logger ||= Rails.logger
end end
def format def format
@format ||= "%<big_brother>s;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s" @format ||= "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
end
def configure(&block)
yield(self) if block_given?
end end
def user=(value) def user=(value)
@ -43,8 +50,7 @@ module RailsBigBrother
def store def store
Thread.current[:big_brother_log] ||= {} Thread.current[:big_brother_log] ||= {}
end end
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,