Class: Rails3::Assist::Logging

Inherits:
Object
  • Object
show all
Defined in:
lib/logging_assist/logging.rb

Constant Summary collapse

DEBUG_LVS =

DEBUG < INFO < WARN < ERROR < FATAL

[:debug, :info, :warn, :error, :fatal]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {:level => :debug}) ⇒ Logging

Returns a new instance of Logging.



31
32
33
34
35
36
# File 'lib/logging_assist/logging.rb', line 31

def initialize options = {:level => :debug}
  Log4r::Logger.global.level = Log4r::ALL
  @logger ||= Log4r::Logger.new('logger')            
  add_stdout options[:level]
  color_options = options[:color_options] if options
end

Instance Attribute Details

#debug_colorObject

Returns the value of attribute debug_color.



17
18
19
# File 'lib/logging_assist/logging.rb', line 17

def debug_color
  @debug_color
end

#error_colorObject

Returns the value of attribute error_color.



17
18
19
# File 'lib/logging_assist/logging.rb', line 17

def error_color
  @error_color
end

#fatal_colorObject

Returns the value of attribute fatal_color.



17
18
19
# File 'lib/logging_assist/logging.rb', line 17

def fatal_color
  @fatal_color
end

#info_colorObject

Returns the value of attribute info_color.



17
18
19
# File 'lib/logging_assist/logging.rb', line 17

def info_color
  @info_color
end

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/logging_assist/logging.rb', line 15

def logger
  @logger
end

#warn_colorObject

Returns the value of attribute warn_color.



17
18
19
# File 'lib/logging_assist/logging.rb', line 17

def warn_color
  @warn_color
end

Instance Method Details

#add_logfile(options = {}) ⇒ Object



108
109
110
# File 'lib/logging_assist/logging.rb', line 108

def add_logfile options = {}
  add_outputter FileOutputter.new "logfile", :filename => options[:logfile] || logfile, :formatter => simple_formatter, :level => get_lv(options[:level] || :debug)
end

#add_outputter(outputter) ⇒ Object



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

def add_outputter outputter
  logger.outputters << outputter
end

#add_stdout(level = :debug) ⇒ Object



66
67
68
# File 'lib/logging_assist/logging.rb', line 66

def add_stdout level = :debug
  add_outputter Log4r::ColorOutputter.new 'color', :formatter => simple_formatter, :level => get_lv(level), :colors => default_colors
end

#debug(msg) ⇒ Object



112
113
114
115
116
117
# File 'lib/logging_assist/logging.rb', line 112

def debug msg
  set_log_class do
    logger.debug "[#{@log_class}]"
  end
  logger.debug "#{msg}" 
end

#debug_lv=(level) ⇒ Object

def remove_all_file_outputs

logger.outputters.each do |outp|
  logger.outputters.delete(outp) if outp.kind_of? Log4r::FileOutputter
end

end

Raises:

  • (ArgumentError)


59
60
61
62
63
64
# File 'lib/logging_assist/logging.rb', line 59

def debug_lv= level
  raise ArgumentError, "Debug lv must be one of #{DEBUG_LVS}" if !DEBUG_LVS.include? lv
  logger.each_outputter do |name, outputter|
    outputter.level = get_lv(level)        
  end
end

#default_colorsObject



70
71
72
73
74
75
# File 'lib/logging_assist/logging.rb', line 70

def default_colors  
  DEBUG_LVS.inject({}) do |res, lv|
    res[lv] = send :"default_#{lv}_color"
    res
  end
end

#default_debug_colorObject

def available_levels

DEBUG_LVS # [:debug, :info, :warn, :error, :fatal]

end



87
88
89
# File 'lib/logging_assist/logging.rb', line 87

def default_debug_color
  :cyan
end

#default_error_colorObject



99
100
101
# File 'lib/logging_assist/logging.rb', line 99

def default_error_color
  :red
end

#default_fatal_colorObject



103
104
105
# File 'lib/logging_assist/logging.rb', line 103

def default_fatal_color
  {:color => :white, :background => :red}
end

#default_info_colorObject



91
92
93
# File 'lib/logging_assist/logging.rb', line 91

def default_info_color
  :light_blue
end

#default_warn_colorObject



95
96
97
# File 'lib/logging_assist/logging.rb', line 95

def default_warn_color
  :yellow
end

#error(msg) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/logging_assist/logging.rb', line 134

def error msg
  set_log_class do
    logger.warn "[#{@log_class}]"
  end
  logger.error draw_line
  logger.error "! #{msg.upcase}" 
  logger.error      
end

#fatal(msg) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/logging_assist/logging.rb', line 143

def fatal msg
  set_log_class do
    logger.warn "[#{@log_class}]"
  end
  logger.fatal draw_line :char => '='
  logger.error "**** #{msg.upcase} ****" 
  logger.fatal draw_line :char => '='
end

#info(msg) ⇒ Object



119
120
121
122
123
124
# File 'lib/logging_assist/logging.rb', line 119

def info msg
  set_log_class do
    logger.info "[#{@log_class}]"
  end
  logger.info "-- #{msg}" 
end

#logfileObject



19
20
21
22
# File 'lib/logging_assist/logging.rb', line 19

def logfile
  gen_logfile = RSpec::Generator.logfile if defined?(RSpec::Generator) 
  gen_logfile || @logfile || 'logging.log'
end

#logfile=(file) ⇒ Object



24
25
26
# File 'lib/logging_assist/logging.rb', line 24

def logfile= file
  @logfile = file
end

#remove_outputs(type) ⇒ Object



46
47
48
49
50
51
# File 'lib/logging_assist/logging.rb', line 46

def remove_outputs type
  klass = "Log4r::#{type.to_s.classify}Outputter".constantize
  logger.outputters.each do |outp|
    logger.outputters.delete(outp) if outp.kind_of? klass
  end
end

#remove_outputter(outputter) ⇒ Object



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

def remove_outputter outputter
  logger.outputters.delete(outputter)
end

#set_color(name, color_options = {}) ⇒ Object



77
78
79
80
81
# File 'lib/logging_assist/logging.rb', line 77

def set_color name, color_options = {}
  send :"#{name}_color=", color_options
  Outputter['color'].colors[name.to_sym] = color_options
  Outputter['color'].colors
end

#warn(msg) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/logging_assist/logging.rb', line 126

def warn msg
  set_log_class do
    logger.warn "[#{@log_class}]"
  end
  logger.warn draw_line
  logger.warn ":: #{msg}" 
end