Class: YARL
- Inherits:
-
Logger
- Object
- Logger
- YARL
- 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
-
#add(severity, message = nil, progname = nil) ⇒ Object
A basic rewrite of Logger.add to support message body colors.
-
#initialize(progname = nil, **kwargs) ⇒ YARL
constructor
A new instance of YARL.
-
#level=(severity) ⇒ Object
SPAM and NOTICE support Add to level setter.
-
#notice(progname = nil, &block) ⇒ Object
Log a
NOTICE
message. -
#notice! ⇒ Object
Sets the severity to SPAM.
-
#notice? ⇒ Boolean
Returns
true
iff the current severity level allows. -
#spam(progname = nil, &block) ⇒ Object
Log a
SPAM
message. - #spam! ⇒ Object
- #spam? ⇒ Boolean
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, | datetime = datetime.strftime(@datetime_format) "\e[#{@header}m#{datetime} #{severity[0]} #{progname}\e[#{@body}m #{}\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, = nil, progname = nil severity ||= UNKNOWN return true if @logdev.nil? or severity < @level progname ||= @progname ||= block_given? ? yield : progname progname = @progname if == progname = .to_s.gsub(@to_hex_regex) {|c| "[%02X]" % c.ord} if @to_hex_regex if .is_a?(String) && .size > (@message_range.max + 1) = "#{.slice @message_range}..." end case severity when FATAL = "\e[#{@fatal_body}m#{}" when ERROR = "\e[#{@error_body}m#{}" when WARN = "\e[#{@warn_body}m#{}" when SPAM = "\e[#{@spam_body}m#{}" end @logdev.write( ( format_severity(severity), Time.now.utc, progname, ) ) 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
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 |