Class: Bricolage::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/bricolage/logger.rb

Constant Summary collapse

DEFAULT_ROTATION_SIZE =

100MB

1024 ** 2 * 100

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



17
18
19
# File 'lib/bricolage/logger.rb', line 17

def Logger.default
  @default ||= new
end

.intern_severity(sev) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/bricolage/logger.rb', line 8

def Logger.intern_severity(sev)
  if sev.kind_of?(Integer)
    sev
  else
    SEV_LABEL.index(sev.to_s.upcase) or
        raise ParameterError, "no such log level: #{sev}"
  end
end

.new(device: $stderr, rotation_period: nil, rotation_size: DEFAULT_ROTATION_SIZE) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/bricolage/logger.rb', line 23

def Logger.new(device: $stderr, rotation_period: nil, rotation_size: DEFAULT_ROTATION_SIZE)
  logger = super(device, (rotation_period || 0), rotation_size)
  logger.level = (device == $stderr && $stderr.tty?) ? Logger::DEBUG : Logger::INFO
  logger.formatter = -> (sev, time, prog, msg) {
    "#{time}: #{sev}: #{msg}\n"
  }
  logger
end

Instance Method Details

#elapsed_time(label, t) ⇒ Object



50
51
52
# File 'lib/bricolage/logger.rb', line 50

def elapsed_time(label, t)
  info "#{label}#{pretty_interval(t)}"
end

#exception(ex) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/bricolage/logger.rb', line 32

def exception(ex)
  buf = StringIO.new
  buf.puts "#{ex.class}: #{ex.message}"
  ex.backtrace.each do |trace|
    buf.puts "\t" + trace
  end
  error buf.string
end

#with_elapsed_time(label = '') ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/bricolage/logger.rb', line 41

def with_elapsed_time(label = '')
  start_time = Time.now
  begin
    return yield
  ensure
    elapsed_time(label, Time.now - start_time)
  end
end