Module: TaggedLogger

Defined in:
lib/tagged_logger/railtie.rb,
lib/tagged_logger/tagged_logger.rb

Defined Under Namespace

Classes: ClassSpecificLogger, Railtie, TagMatcher

Class Method Summary collapse

Class Method Details

.blocks_for(level, tag) ⇒ Object

should private, but used by ClassSpecificLogger



57
58
59
60
61
62
63
64
65
# File 'lib/tagged_logger/tagged_logger.rb', line 57

def blocks_for(level, tag)
  blocks = []
  tag_aliases(tag) do |tag_alias|
    tag_blocks(level, tag_alias) do |tag_block|
      blocks << [tag_alias, tag_block]
    end
  end
  blocks
end

.config(options) ⇒ Object

Supported options: :replace_existing_logger => true/false



14
15
16
17
# File 'lib/tagged_logger/tagged_logger.rb', line 14

def config(options)
  @config = options
  self
end

.format(&block) ⇒ Object



46
47
48
49
# File 'lib/tagged_logger/tagged_logger.rb', line 46

def format(&block)
  @formatter = block
  self
end

.initObject



38
39
40
# File 'lib/tagged_logger/tagged_logger.rb', line 38

def init
  puts "TaggedLogger#init() is deprecated. Use TaggedLogger.rules with no block."
end

.klass_has_method?(klass, method) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tagged_logger/tagged_logger.rb', line 42

def klass_has_method?(klass, method)
  klass.instance_methods(false).include?(RUBY_VERSION >= '1.9' ? method.to_sym : method.to_s)
end

.optionsObject



19
20
21
# File 'lib/tagged_logger/tagged_logger.rb', line 19

def options
  @config
end

.patch_logger(patchee, replace_existing_logger) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tagged_logger/tagged_logger.rb', line 67

def patch_logger(patchee, replace_existing_logger)
  return self if @patched.include?(patchee)
  
  if klass_has_method?(patchee, :logger)
    return self if !replace_existing_logger
    #so we could resurrect old :logger method if we need
    patchee.class_eval { alias_method :tagged_logger_original_logger, :logger }
  end
  
  @patched << patchee
  
  patchee.class_eval do
    def logger
      klass = self.class == Class ? self : self.class
      result = klass.class_eval do
        return @class_logger if @class_logger
        @class_logger = ClassSpecificLogger.new(klass)
        @class_logger
      end
      result
    end
  end
  self
end

.rename(renames) ⇒ Object



51
52
53
54
# File 'lib/tagged_logger/tagged_logger.rb', line 51

def rename(renames)
  renames.each { |from, to| @rename_rules[tag_matcher(from)] = to }
  self
end

.resetObject



29
30
31
32
33
34
35
36
# File 'lib/tagged_logger/tagged_logger.rb', line 29

def reset
  unpatch_all
  @rename_rules = {}
  @tag_blocks = {}
  @formatter = nil
  @config = {}
  self
end

.rules(&block) ⇒ Object



23
24
25
26
27
# File 'lib/tagged_logger/tagged_logger.rb', line 23

def rules(&block)
  patch_logger(Object, options[:replace_existing_logger]) unless @patched.include?(Object)
  instance_eval(&block) if block
  self
end