markbates / mack

A Ruby web application framework

This URL has Read+Write access

mack / lib / mack / initialization / logging.rb
100644 103 lines (76 sloc) 2.436 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#--
# Configure logging
#++
 
module Mack
  
  def self.logger
    $mack_default_logger
  end
  
  def self.logger=(log)
    $mack_default_logger = log
  end
  
end
 
unless Mack.logger
 
  module Log4r # :nodoc:
    class IOOutputter # :nodoc:
 
      # let's not do this more than once. :)
      unless Log4r::IOOutputter.private_instance_methods.include?("old_write")
 
        alias_method :old_write, :write
 
        def write(data)
          case data
          when /^(DEBUG:|INFO:|WARN:|ERROR:|FATAL:)\s\[.*\]\s(SELECT|INSERT|UPDATE|DELETE|CREATE|DROP)/
            old_write(Color.yellow(data))
          when /^(ERROR:|FATAL:)/
            old_write(Color.red(data))
          else
            old_write(data)
          end
        end
 
      end
 
    end # IOOutputter
  end # Log4r
  
  log_directory = app_config.log_root || File.join(Mack.root, "log")
  FileUtils.mkdir_p(log_directory)
 
  Mack.logger = Log4r::Logger.new('')
  Mack.logger.level = Module.instance_eval("Log4r::#{(app_config.log_level || :info).to_s.upcase}")
  
  format = Log4r::PatternFormatter.new(:pattern => "%l:\t[%d]\t%M")
  
  if Mack.env == "development"
    # console:
    Mack.logger.add(Log4r::StdoutOutputter.new('console', :formatter => format))
  end
  
  # file:
  Mack.logger.add(Log4r::FileOutputter.new('fileOutputter', :filename => File.join(log_directory, "#{Mack.env}.log"), :trunc => false, :formatter => format))
end
 
module Mack
  module Logging # :nodoc:
    # Used to house a list of filters for parameter logging. The initial list
    # includes password and password_confirmation
    class Filter
      include Singleton
      
      # The list of parameters you want filtered for logging.
      attr_reader :list
      
      def initialize
        @list = [:password, :password_confirmation]
      end
      
      # Adds 'n' number of parameter names to the list
      def add(*args)
        @list << args
        @list.flatten!
      end
      
      # Removes 'n' number of parameter names from the list
      def remove(*args)
        @list.delete_values(*args)
      end
      
      class << self
        
        def remove(*args)
          Mack::Logging::Filter.instance.remove(*args)
        end
        
        def add(*args)
          Mack::Logging::Filter.instance.add(*args)
        end
        
        def list
          Mack::Logging::Filter.instance.list
        end
        
      end
      
    end
  end
end