Class: DuckTest::Logger

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

Overview

Custom Logger

Constant Summary

SYSTEM =

ActiveSupport::BufferedLogger

6

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) ducklog

Instance of DuckTest::Logger



20
21
22
23
# File 'lib/duck_test/logger.rb', line 20

def self.ducklog
  dir = defined?(Rails) ? Rails.root : "."
  return @@ducklog ||= self.new("#{dir}/log/ducktest.log", INFO)
end

+ (Number) log_level

Gets the current logging level for the ducklog.

Returns:

  • (Number)


56
57
58
59
# File 'lib/duck_test/logger.rb', line 56

def self.log_level
  @@log_level ||= self.ducklog.level
  return @@log_level
end

+ (Number) log_level=(key)

Sets the logging level for the ducklog.

Parameters:

  • key (Symbol, Number)

    A value representing the desired logging level. Can be a Symbol such as :debug, :info, etc. or an ActiveSupport::BufferedLogger Constant DEBUG, INFO, etc.

Returns:

  • (Number)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/duck_test/logger.rb', line 41

def self.log_level=(key)
  key = key.blank? ? "" : key.to_sym
  value = self.to_severity(key)

  unless value.blank?
    @@log_level = value
    value = value.eql?(SYSTEM) ? DEBUG : value
    self.ducklog.level = value
  end

end

+ (Object) to_severity(key)

Converts a Symbol to a valid log level and vise versa.

Returns:

  • (Object)

    The return value is based on the argument :key.

    • If you pass a Symbol, you get a log level.

    • If you pass a log level, you get a Symbol.



30
31
32
33
34
# File 'lib/duck_test/logger.rb', line 30

def self.to_severity(key)
  values = {debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL, unknown: UNKNOWN, system: SYSTEM}
  value = values.map.find {|value| value[key.kind_of?(Symbol) ? 0 : 1].eql?(key)}
  return value.blank? ? nil : value[key.kind_of?(Symbol) ? 1 : 0]
end

Instance Method Details

- (Object) console(msg = nil, progname = nil, &block)



63
64
65
66
67
# File 'lib/duck_test/logger.rb', line 63

def console(msg = nil, progname = nil, &block)
  STDOUT.puts msg
  add(INFO, "#{Config.framework_name.to_s.rjust(15)}: #{msg}", progname, &block)
  return nil
end

- (Object) debug(msg = nil, progname = nil, &block)



83
84
85
86
# File 'lib/duck_test/logger.rb', line 83

def debug(msg = nil, progname = nil, &block)
  add(DEBUG, "#{Config.framework_name.to_s.rjust(15)}: #{msg}", progname, &block)
  return nil
end

- (Object) exception(exception, progname = nil, &block)



71
72
73
74
75
76
77
78
79
# File 'lib/duck_test/logger.rb', line 71

def exception(exception, progname = nil, &block)
  STDOUT.puts %(ERROR!! ducktest.log for: #{exception})
  #if self.class.log_level.eql?(DEBUG)
    exception.backtrace.each {|x| STDOUT.puts x}
  #end
  add(ERROR, "#{Config.framework_name.to_s.rjust(15)}: #{exception.to_s}", progname, &block)
  exception.backtrace.each {|x| add(ERROR, x, progname, &block)}
  return nil
end

- (Object) info(msg = nil, progname = nil, &block)



90
91
92
93
# File 'lib/duck_test/logger.rb', line 90

def info(msg = nil, progname = nil, &block)
  add(INFO, "#{Config.framework_name.to_s.rjust(15)}: #{msg}", progname, &block)
  return nil
end

- (Object) system(msg = nil, progname = nil, &block)



97
98
99
100
101
102
# File 'lib/duck_test/logger.rb', line 97

def system(msg = nil, progname = nil, &block)
  if self.class.log_level.eql?(SYSTEM)
    add(DEBUG, "#{Config.framework_name.to_s.rjust(15)}: #{msg}", progname, &block)
  end
  return nil
end