Class: RailsAssist::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.



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

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.



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

def debug_color
  @debug_color
end

#error_colorObject

Returns the value of attribute error_color.



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

def error_color
  @error_color
end

#fatal_colorObject

Returns the value of attribute fatal_color.



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

def fatal_color
  @fatal_color
end

#info_colorObject

Returns the value of attribute info_color.



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

def info_color
  @info_color
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#warn_colorObject

Returns the value of attribute warn_color.



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

def warn_color
  @warn_color
end

Instance Method Details

#add_logfile(options = {}) ⇒ Object



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

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



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

def add_outputter outputter
  logger.outputters << outputter
end

#add_stdout(level = :debug) ⇒ Object



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

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



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

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)


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

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



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

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



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

def default_debug_color
  :cyan
end

#default_error_colorObject



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

def default_error_color
  :red
end

#default_fatal_colorObject



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

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

#default_info_colorObject



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

def default_info_color
  :light_blue
end

#default_warn_colorObject



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

def default_warn_color
  :yellow
end

#error(msg) ⇒ Object



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

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



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

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



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

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

#logfileObject



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

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

#logfile=(file) ⇒ Object



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

def logfile= file
  @logfile = file
end

#remove_outputs(type) ⇒ Object



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

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



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

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

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



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

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



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

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