From a83de02b0fbb7221df77ca8d1515af676a76398e Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 29 May 2013 14:31:50 +0200 Subject: [PATCH] Add configure method and logger option to choose other logger --- README.md | 21 +++++----- lib/rails_big_brother.rb | 74 ++++++++++++++++++---------------- lib/rails_big_brother/model.rb | 3 +- 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index c88c8b6..4ff7441 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,15 @@ Or install it yourself as: $ gem install rails_big_brother +## Configuration + +```ruby +RailsBigBrother.config do |config| + config.format = "big_brother;%s;%s;%s;%s;%s;%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 -"%s;%s;%s;%s;%s;%s;%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` diff --git a/lib/rails_big_brother.rb b/lib/rails_big_brother.rb index 1ff3826..9b72993 100644 --- a/lib/rails_big_brother.rb +++ b/lib/rails_big_brother.rb @@ -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 ||= "%s;%s;%s;%s;%s;%s;%s" - end + def logger + @logger ||= Rails.logger + end - def user=(value) - store[:user] = value - end + def format + @format ||= "big_brother;%s;%s;%s;%s;%s;%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 diff --git a/lib/rails_big_brother/model.rb b/lib/rails_big_brother/model.rb index 22d7d08..0892789 100644 --- a/lib/rails_big_brother/model.rb +++ b/lib/rails_big_brother/model.rb @@ -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,