Class: Logue::Logger

Inherits:
Object
  • Object
show all
Includes:
Logue::Log::Severity
Defined in:
lib/logue/logger.rb

Constant Summary collapse

FRAME_RE =
Regexp.new('(.*):(\d+)(?::in \`(.*)\')?')

Constants included from Logue::Log::Severity

Logue::Log::Severity::DEBUG, Logue::Log::Severity::ERROR, Logue::Log::Severity::FATAL, Logue::Log::Severity::INFO, Logue::Log::Severity::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



46
47
48
# File 'lib/logue/logger.rb', line 46

def initialize
  set_defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/logue/logger.rb', line 236

def method_missing meth, *args, &blk
  validcolors = Sickill::Rainbow::TERM_COLORS
  # only handling foregrounds, not backgrounds
  if code = validcolors[meth]
    add_color_method meth.to_s, code + 30
    send meth, *args, &blk
  else
    super
  end
end

Instance Attribute Details

#colorize_lineObject

Returns the value of attribute colorize_line.



35
36
37
# File 'lib/logue/logger.rb', line 35

def colorize_line
  @colorize_line
end

#ignored_classesObject

Returns the value of attribute ignored_classes.



39
40
41
# File 'lib/logue/logger.rb', line 39

def ignored_classes
  @ignored_classes
end

#ignored_filesObject

Returns the value of attribute ignored_files.



37
38
39
# File 'lib/logue/logger.rb', line 37

def ignored_files
  @ignored_files
end

#ignored_methodsObject

Returns the value of attribute ignored_methods.



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

def ignored_methods
  @ignored_methods
end

#levelObject

Returns the value of attribute level.



36
37
38
# File 'lib/logue/logger.rb', line 36

def level
  @level
end

#outputObject

Returns the value of attribute output.



34
35
36
# File 'lib/logue/logger.rb', line 34

def output
  @output
end

#quietObject

Returns the value of attribute quiet.



33
34
35
# File 'lib/logue/logger.rb', line 33

def quiet
  @quiet
end

#trimObject

Returns the value of attribute trim.



40
41
42
# File 'lib/logue/logger.rb', line 40

def trim
  @trim
end

Instance Method Details

#add_color_method(color, code) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/logue/logger.rb', line 252

def add_color_method color, code
  instmeth = Array.new
  instmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
  instmeth << "  log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
  instmeth << "end"
  instance_eval instmeth.join("\n")

  clsmeth = Array.new
  clsmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
  clsmeth << "  logger.#{color}(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
  clsmeth << "end"

  class_eval clsmeth.join("\n")
end

#debug(msg = "", depth = 1, &blk) ⇒ Object



125
126
127
# File 'lib/logue/logger.rb', line 125

def debug msg = "", depth = 1, &blk
  log msg, DEBUG, depth + 1, &blk
end

#error(msg = "", depth = 1, &blk) ⇒ Object



137
138
139
# File 'lib/logue/logger.rb', line 137

def error msg = "", depth = 1, &blk
  log msg, ERROR, depth + 1, &blk
end

#fatal(msg = "", depth = 1, &blk) ⇒ Object



141
142
143
# File 'lib/logue/logger.rb', line 141

def fatal msg = "", depth = 1, &blk
  log msg, FATAL, depth + 1, &blk
end

#ignore_class(classname) ⇒ Object



109
110
111
# File 'lib/logue/logger.rb', line 109

def ignore_class classname
  ignored_classes[classname] = true
end

#ignore_file(fname) ⇒ Object



101
102
103
# File 'lib/logue/logger.rb', line 101

def ignore_file fname
  ignored_files[fname] = true
end

#ignore_method(methname) ⇒ Object



105
106
107
# File 'lib/logue/logger.rb', line 105

def ignore_method methname
  ignored_methods[methname] = true
end

#info(msg = "", depth = 1, &blk) ⇒ Object



129
130
131
# File 'lib/logue/logger.rb', line 129

def info msg = "", depth = 1, &blk
  log msg, INFO, depth + 1, &blk
end

#log(msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk) ⇒ Object

Logs the given message.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logue/logger.rb', line 146

def log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
  if lvl >= level
    frame = nil

    stk = caller 0
    stk.reverse.each_with_index do |frm, idx|
      if frm.index(%r{logue/log.*:\d+:in\b})
        break
      else
        frame = frm
      end
    end

    print_stack_frame frame, cname, msg, lvl, &blk
  end
end

#log_class(classname) ⇒ Object



121
122
123
# File 'lib/logue/logger.rb', line 121

def log_class classname
  ignored_classes.delete classname
end

#log_file(fname) ⇒ Object



113
114
115
# File 'lib/logue/logger.rb', line 113

def log_file fname
  ignored_files.delete fname
end

#log_method(methname) ⇒ Object



117
118
119
# File 'lib/logue/logger.rb', line 117

def log_method methname
  ignored_methods.delete methname
end

#outfile=(f) ⇒ Object

Assigns output to a file with the given name. Returns the file; client is responsible for closing it.



86
87
88
# File 'lib/logue/logger.rb', line 86

def outfile= f
  @output = f.kind_of?(IO) ? f : File.new(f, "w")
end


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/logue/logger.rb', line 208

def print hdr, msg, lvl, &blk
  if blk
    x = blk.call
    if x.kind_of? String
      msg = x
    else
      return
    end
  end

  msg = msg.to_s.chomp

  if lvlcol = @colors[lvl]
    if colorize_line
      line = hdr + " " + msg
      @output.puts line.color(lvlcol)
    else
      @output.puts hdr + " " + msg.color(lvlcol)
    end
  else
    @output.puts hdr + " " + msg
  end      
end


196
197
198
199
200
201
202
203
204
205
206
# File 'lib/logue/logger.rb', line 196

def print_formatted file, line, func, msg, lvl, &blk
  if trim
    fmt = Format.new
    file = fmt.trim_right file, @file_width
    line = fmt.trim_left  line, @line_width
    func = fmt.trim_left  func, @function_width
  end

  hdr = sprintf @format, file, line, func
  print hdr, msg, lvl, &blk
end


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/logue/logger.rb', line 175

def print_stack_frame frame, cname, msg, lvl, &blk
  md = FRAME_RE.match frame
  file, line, func = md[1], md[2], (md[3] || "")
  # file.sub!(/.*\//, "")

  # Ruby 1.9 expands the file name, but 1.8 doesn't:
  pn = Pathname.new(file).expand_path
  
  file = pn.to_s

  if cname
    func = cname + "#" + func
  end
  
  if ignored_files[file] || (cname && ignored_classes[cname]) || ignored_methods[func]
    # skip this one.
  else
    print_formatted(file, line, func, msg, lvl, &blk)
  end
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/logue/logger.rb', line 247

def respond_to? meth
  validcolors = Sickill::Rainbow::TERM_COLORS
  validcolors.include?(meth) || super
end

#set_color(lvl, color) ⇒ Object



232
233
234
# File 'lib/logue/logger.rb', line 232

def set_color lvl, color
  @colors[lvl] = color
end

#set_default_widthsObject



76
77
78
# File 'lib/logue/logger.rb', line 76

def set_default_widths
  set_widths Log::DEFAULT_FILENAME_WIDTH, Log::DEFAULT_LINENUM_WIDTH, Log::DEFAULT_FUNCTION_WIDTH
end

#set_defaultsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logue/logger.rb', line 61

def set_defaults
  $LOGGING_LEVEL   = @level = FATAL
  @ignored_files   = Hash.new
  @ignored_methods = Hash.new
  @ignored_classes = Hash.new
  @width           = 0
  @output          = $stdout
  @colors          = Array.new
  @colorize_line   = false
  @quiet           = false
  @trim            = true

  set_default_widths
end

#set_widths(file_width, line_width, func_width) ⇒ Object

Creates a printf format for the given widths, for aligning output. To lead lines with zeros (e.g., “00317”) the line_width argument must be a string, not an integer.



93
94
95
96
97
98
99
# File 'lib/logue/logger.rb', line 93

def set_widths file_width, line_width, func_width
  @file_width = file_width
  @line_width = line_width
  @function_width = func_width
  
  @format = "[%#{file_width}s:%#{line_width}d] {%#{func_width}s}"
end

#stack(msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk) ⇒ Object

Shows the current stack.



164
165
166
167
168
169
170
171
172
173
# File 'lib/logue/logger.rb', line 164

def stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
  if lvl >= level
    stk = caller depth
    for frame in stk
      print_stack_frame frame, cname, msg, lvl, &blk
      cname = nil
      msg = ""
    end
  end
end

#verboseObject



80
81
82
# File 'lib/logue/logger.rb', line 80

def verbose
  level <= DEBUG
end

#verbose=(v) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/logue/logger.rb', line 50

def verbose= v
  @level = case v
           when TrueClass 
             DEBUG
           when FalseClass 
             FATAL
           when Integer
             v
           end
end

#warn(msg = "", depth = 1, &blk) ⇒ Object



133
134
135
# File 'lib/logue/logger.rb', line 133

def warn msg = "", depth = 1, &blk
  log msg, WARN, depth + 1, &blk
end