Class: RubySmart::SimpleLogger::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_smart/simple_logger/formatter.rb

Constant Summary collapse

SEVERITY_COLORS =

defines the severity colors

{
  'DEBUG'   => :blue,
  'INFO'    => :cyan,
  'WARN'    => :yellow,
  'ERROR'   => :red,
  'FATAL'   => :bg_red,
  'SUCCESS' => :green
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Formatter

initialize with options

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :format (Symbol)
    • define other format
  • :nl (Boolean)
    • create newline after each call (default: true)
  • :clr (Boolean)
    • colorizes the whole output (default: false)


59
60
61
62
63
64
65
66
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 59

def initialize(opts = {})
  # set default opts
  opts[:nl]     = true if opts[:nl].nil?
  opts[:format] = :default if opts[:format].nil?

  # only store required options
  @opts = opts.slice(:nl, :format, :clr)
end

Class Method Details

.formatsHash

returns all registered formats

Returns:

  • (Hash)

    formats



9
10
11
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 9

def formats
  class_variable_get('@@formats')
end

.formats=(formats) ⇒ Object

sets formats

Parameters:

  • formats (Hash)


15
16
17
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 15

def formats=(formats)
  class_variable_set('@@formats', formats)
end

Instance Method Details

#call(severity, time, progname, data) ⇒ Object

standard call method - used to format provided params



69
70
71
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 69

def call(severity, time, progname, data)
  instance_exec(severity, time, progname, data, &current_formatter)
end

#clear!Object

clears current formatter



101
102
103
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 101

def clear!
  @current_formatter = nil
end

#formatsHash

returns all class formats

Returns:

  • (Hash)

    formats



75
76
77
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 75

def formats
  self.class.formats
end

#opts(opts = nil) ⇒ Hash

combined getter & setter for options new options are merged with existing

Examples:

opts
> {formatter: :default, nl: true}

opts(nl: false, test: 45)
opts
> {formatter: :default, nl: false, test: 45}

Parameters:

  • opts (nil, Hash) (defaults to: nil)

Returns:

  • (Hash)

    opts



92
93
94
95
96
97
98
# File 'lib/ruby_smart/simple_logger/formatter.rb', line 92

def opts(opts = nil)
  return @opts if opts.nil?

  clear!

  @opts.merge!(opts)
end