Class: YARL

Inherits:
Logger
  • Object
show all
Includes:
Severity
Defined in:
lib/yarl.rb,
lib/yarl/version.rb

Overview

Yet Another Ruby Logger

Defined Under Namespace

Modules: Severity

Constant Summary collapse

VERSION =
'0.3.7'

Constants included from Severity

Severity::NOTICE, Severity::SPAM

Instance Method Summary collapse

Constructor Details

#initialize(progname = nil, **kwargs) ⇒ YARL

Returns a new instance of YARL.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yarl.rb', line 18

def initialize progname = nil, **kwargs
  destination = kwargs[:destination] || $stdout
  super destination

  buffered = kwargs[:buffered] || false
  destination.sync = !buffered if destination.respond_to? 'sync'

  @progname   = progname.nil? ? self.class : progname
  self.level  = kwargs[:level] || INFO
  
  # Header colors
  color       =       text_color(kwargs[:color] || kwargs[:header])
  background  = background_color(kwargs[:background])
  @header     = format_code color, background

  # Default body colors
  @body       =       text_color(kwargs[:body])

  # Automatic body colors
  @fatal_body = background_color(kwargs[:fatal] || kwargs[:fatal_color] || :red)
  @error_body =       text_color(kwargs[:error] || kwargs[:error_color] || :red)
  @warn_body  =       text_face (kwargs[:warn]  || kwargs[:warn_face]   || :bold)
  @spam_body  =       text_color(kwargs[:spam]  || kwargs[:spam_color]  || :bright_black)

  # Log formatting
  @datetime_format = kwargs[:datetime_format] || '%Y-%m-%dT%H:%M:%S.%3NZ'
  if kwargs[:ascii8bit]
    @to_hex_regex = /([^ -~])/no
  elsif !(kwargs[:printable] === false)
    @to_hex_regex = /([\x00-\x19])/o
  end

  @message_range = kwargs[:message_range]
  @message_range ||= 0...512

  if kwargs[:formatter].is_a?(Proc)
    @formatter = kwargs[:formatter]
  else
    @formatter ||= proc { |severity, datetime, progname, message|
      datetime = datetime.strftime(@datetime_format)
      "\e[#{@header}m#{datetime} #{severity[0]} #{progname}\e[#{@body}m #{message}\e[0m\n"
    }
  end
end

Instance Method Details

#add(severity, message = nil, progname = nil) ⇒ Object

A basic rewrite of Logger.add to support message body colors.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/yarl.rb', line 64

def add severity, message = nil, progname = nil
  severity ||= UNKNOWN
  return true if @logdev.nil? or severity < @level

  progname  ||= @progname
  message   ||= block_given? ? yield : progname

  progname = @progname if message == progname

  message = message.to_s.gsub(@to_hex_regex) {|c| "[%02X]" % c.ord} if @to_hex_regex
  if message.is_a?(String) && message.size > (@message_range.max + 1)
    message = "#{message.slice @message_range}..."
  end

  case severity
  when FATAL
    message = "\e[#{@fatal_body}m#{message}"
  when ERROR
    message = "\e[#{@error_body}m#{message}"
  when WARN
    message = "\e[#{@warn_body}m#{message}"
  when SPAM
    message = "\e[#{@spam_body}m#{message}"
  end

  @logdev.write(
    format_message(
      format_severity(severity),
      Time.now.utc,
      progname,
      message
    )
  )

  true
end

#level=(severity) ⇒ Object

SPAM and NOTICE support Add to level setter



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/yarl.rb', line 103

def level= severity
  severity = severity.to_s if severity.is_a?(Symbol)
  severity = severity.downcase if severity.is_a?(String)

  case severity
  when 'notice'
    @level = NOTICE
  when 'spam'
    @level = SPAM
  else
    super
  end
end

#notice(progname = nil, &block) ⇒ Object

Log a NOTICE message.



126
127
128
# File 'lib/yarl.rb', line 126

def notice progname = nil, &block
  add(NOTICE, nil, progname, &block)
end

#notice!Object

Sets the severity to SPAM.



122
# File 'lib/yarl.rb', line 122

def notice!; self.level = NOTICE; end

#notice?Boolean

Returns true iff the current severity level allows

Returns:

  • (Boolean)


118
# File 'lib/yarl.rb', line 118

def notice?; @level <= NOTICE; end

#spam(progname = nil, &block) ⇒ Object

Log a SPAM message.



130
131
132
# File 'lib/yarl.rb', line 130

def spam progname = nil, &block
  add(SPAM, nil, progname, &block)
end

#spam!Object



123
# File 'lib/yarl.rb', line 123

def spam!; self.level = SPAM; end

#spam?Boolean

Returns:

  • (Boolean)


119
# File 'lib/yarl.rb', line 119

def spam?; @level <= SPAM; end