yarl 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/README.md +61 -0
- data/examples/complex.rb +24 -0
- data/examples/simple.rb +4 -0
- data/lib/yarl/version.rb +1 -1
- data/lib/yarl.rb +78 -25
- data/media/examples_complex.png +0 -0
- data/media/examples_simple.png +0 -0
- data/media/tests_colors.png +0 -0
- data/media/tests_levels.png +0 -0
- data/tests/benchmarks.rb +1 -1
- data/tests/colors.rb +1 -1
- data/tests/levels.rb +5 -4
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 765b0c1c815784ba8772409a9387a7597942594e011b41c25862abb6b0953ee7
|
4
|
+
data.tar.gz: 8a6f8e5d7ca40784d7fb5e87c5928986c094f7820e8cbd8fdea212283febe6ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4a981724da9ecf40bfe3071f27778f9130c3fe281f9ae1a117ef1f891eb53fa417cd7df2233494ebbafb22c554ba516eb363a044aeb0f943399d0531da69fa1
|
7
|
+
data.tar.gz: 35398ee9f6228e3161faf65e7fabaef1ab9cf6dac45cee8a3613c70d5eab41d0982c7a56ae59c84e883833647f05bcc5ffd7a23119820d31d21cf5c31a9f8bfe
|
data/.gitignore
CHANGED
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# YARL - Yet Another Ruby Logger
|
2
|
+
YARL is a [ruby/logger](https://github.com/ruby/logger) extension that provides:
|
3
|
+
- Full [ANSI 4-bit color](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) support
|
4
|
+
- SPAM -- a severity level lower than DEBUG
|
5
|
+
- Clean, low-clutter formatting
|
6
|
+
- Common defaults, like writing to STDOUT
|
7
|
+
|
8
|
+
As an extension, `logger` is fully implemented.
|
9
|
+
|
10
|
+
## Color Support
|
11
|
+
YARL supports 4-bit ANSI colors -- both text and background -- in the logger header.
|
12
|
+
|
13
|
+
![[citation needed]](../master/media/tests_colors.png?raw=true)
|
14
|
+
|
15
|
+
Additionally, the following severity levels have message colors:
|
16
|
+
- `FATAL` has a background color -- defaults to red
|
17
|
+
- `ERROR` has a text color -- defaults to red
|
18
|
+
- `SPAM` has a text color -- defaults to bright black
|
19
|
+
|
20
|
+

|
21
|
+
|
22
|
+
## Installation
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
`gem 'yarl'`
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
`$ bundle`
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
`$ gem install yarl`
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Simple Example
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'yarl'
|
41
|
+
|
42
|
+
logger = YARL.new "Simple Example", color: :green
|
43
|
+
logger.info "Hello, World!"
|
44
|
+
```
|
45
|
+
|
46
|
+
will produce
|
47
|
+

|
48
|
+
|
49
|
+
### Complex Example
|
50
|
+
|
51
|
+
Please see [examples/complex.rb](../master/examples/complex.rb) for the source code.
|
52
|
+
|
53
|
+
This example will produce
|
54
|
+

|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Feel free to [make an issue](https://github.com/david-massey/yarl/issues/new), and I will do my best to respond quickly.
|
59
|
+
|
60
|
+
Or, fork it, edit it, and pull request.
|
61
|
+
|
data/examples/complex.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'yarl'
|
2
|
+
|
3
|
+
module Some
|
4
|
+
class Klass
|
5
|
+
attr_accessor :logger
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@logger = YARL.new\
|
9
|
+
self.class,
|
10
|
+
destination: STDERR,
|
11
|
+
color: :bright_magenta,
|
12
|
+
datetime_format: '%H:%M:%S.%6NZ',
|
13
|
+
spam_color: :black # I *never* want to see SPAM
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(x, y)
|
17
|
+
@logger.spam { x + y }
|
18
|
+
x + y
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
complex = Some::Klass.new
|
24
|
+
complex.logger.info complex.add('Hello, ', 'World!')
|
data/examples/simple.rb
ADDED
data/lib/yarl/version.rb
CHANGED
data/lib/yarl.rb
CHANGED
@@ -4,6 +4,10 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
4
4
|
module Severity
|
5
5
|
include Logger::Severity
|
6
6
|
|
7
|
+
# Normal but significant conditions
|
8
|
+
# Conditions that are not error/warning conditions, but that may require special handling.
|
9
|
+
NOTICE = 1.5
|
10
|
+
|
7
11
|
# For that time you need more messages that annoy the hell out of everyone else.
|
8
12
|
# These should rarely -- if ever -- be displayed.
|
9
13
|
SPAM = -1
|
@@ -11,37 +15,47 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
11
15
|
|
12
16
|
include Severity
|
13
17
|
|
14
|
-
def initialize
|
18
|
+
def initialize progname = nil, **kwargs
|
15
19
|
destination = kwargs[:destination] || STDOUT
|
16
20
|
super destination
|
17
21
|
|
18
|
-
@progname
|
19
|
-
@level
|
22
|
+
@progname = progname.nil? ? self.class : progname
|
23
|
+
@level = kwargs[:level] || INFO
|
20
24
|
|
21
25
|
# Header colors
|
22
|
-
|
23
|
-
|
26
|
+
color = text_color(kwargs[:color] || kwargs[:header])
|
27
|
+
background = background_color(kwargs[:background])
|
28
|
+
@header = format_code color, background
|
29
|
+
|
30
|
+
# Default body colors
|
31
|
+
@body = text_color(kwargs[:body])
|
24
32
|
|
25
|
-
#
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
33
|
+
# Automatic body colors
|
34
|
+
@fatal_body = background_color(kwargs[:fatal] || kwargs[:fatal_color] || :red)
|
35
|
+
@error_body = text_color(kwargs[:error] || kwargs[:error_color] || :red)
|
36
|
+
@warn_body = text_face (kwargs[:warn] || kwargs[:warn_face] || :bold)
|
37
|
+
@spam_body = text_color(kwargs[:spam] || kwargs[:spam_color] || :bright_black)
|
29
38
|
|
30
39
|
# Log formatting
|
31
40
|
@datetime_format = kwargs[:datetime_format] || '%Y-%m-%dT%H:%M:%S.%3NZ'
|
41
|
+
if kwargs[:ascii8bit]
|
42
|
+
@to_hex_regex = /([^ -~])/no
|
43
|
+
elsif !(kwargs[:printable] === false)
|
44
|
+
@to_hex_regex = /([\x00-\x19])/o
|
45
|
+
end
|
32
46
|
|
33
47
|
if kwargs[:formatter].is_a?(Proc)
|
34
48
|
@formatter = kwargs[:formatter]
|
35
49
|
else
|
36
50
|
@formatter ||= proc { |severity, datetime, progname, message|
|
37
51
|
datetime = datetime.strftime(@datetime_format)
|
38
|
-
"\e[#{@
|
52
|
+
"\e[#{@header}m#{datetime} #{severity[0]} #{progname}\e[#{@body}m #{message}\e[0m\n"
|
39
53
|
}
|
40
54
|
end
|
41
55
|
end
|
42
56
|
|
43
57
|
# A basic rewrite of Logger.add to support message body colors.
|
44
|
-
def add
|
58
|
+
def add severity, message = nil, progname = nil
|
45
59
|
severity ||= UNKNOWN
|
46
60
|
return true if @logdev.nil? or severity < @level
|
47
61
|
|
@@ -50,13 +64,17 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
50
64
|
|
51
65
|
progname = @progname if message == progname
|
52
66
|
|
67
|
+
message.gsub!(@to_hex_regex) {|c| "[%02X]" % c.ord} if @to_hex_regex
|
68
|
+
|
53
69
|
case severity
|
54
70
|
when FATAL
|
55
|
-
message = "\e[#{@
|
71
|
+
message = "\e[#{@fatal_body}m#{message}"
|
56
72
|
when ERROR
|
57
|
-
message = "\e[#{@
|
73
|
+
message = "\e[#{@error_body}m#{message}"
|
74
|
+
when WARN
|
75
|
+
message = "\e[#{@warn_body}m#{message}"
|
58
76
|
when SPAM
|
59
|
-
message = "\e[#{@
|
77
|
+
message = "\e[#{@spam_body}m#{message}"
|
60
78
|
end
|
61
79
|
|
62
80
|
@logdev.write(
|
@@ -71,12 +89,14 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
71
89
|
true
|
72
90
|
end
|
73
91
|
|
74
|
-
# SPAM support
|
75
|
-
# Add
|
76
|
-
def level=
|
92
|
+
# SPAM and NOTICE support
|
93
|
+
# Add to level setter
|
94
|
+
def level= severity
|
77
95
|
severity = severity.to_s if severity.is_a?(Symbol)
|
78
96
|
|
79
97
|
case severity
|
98
|
+
when 'notice'
|
99
|
+
@level = NOTICE
|
80
100
|
when 'spam'
|
81
101
|
@level = SPAM
|
82
102
|
else
|
@@ -84,14 +104,20 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
84
104
|
end
|
85
105
|
end
|
86
106
|
|
87
|
-
# Returns +true+ iff the current severity level allows
|
107
|
+
# Returns +true+ iff the current severity level allows
|
108
|
+
def notice?; @level <= NOTICE; end
|
88
109
|
def spam?; @level <= SPAM; end
|
89
110
|
|
90
111
|
# Sets the severity to SPAM.
|
112
|
+
def notice!; self.level = NOTICE; end
|
91
113
|
def spam!; self.level = SPAM; end
|
92
114
|
|
93
|
-
# Log a +
|
94
|
-
def
|
115
|
+
# Log a +NOTICE+ message.
|
116
|
+
def notice progname = nil, &block
|
117
|
+
add(NOTICE, nil, progname, &block)
|
118
|
+
end
|
119
|
+
# Log a +SPAM+ message.
|
120
|
+
def spam progname = nil, &block
|
95
121
|
add(SPAM, nil, progname, &block)
|
96
122
|
end
|
97
123
|
|
@@ -101,23 +127,50 @@ class YARL < Logger # Yet Another Ruby Logger
|
|
101
127
|
SPAM => 'SPAM',
|
102
128
|
DEBUG => 'DEBUG',
|
103
129
|
INFO => 'INFO',
|
130
|
+
NOTICE => 'NOTICE',
|
104
131
|
WARN => 'WARN',
|
105
132
|
ERROR => 'ERROR',
|
106
133
|
FATAL => 'FATAL',
|
107
134
|
UNKNOWN => 'UNKWN'
|
108
135
|
}
|
109
136
|
|
110
|
-
def format_severity
|
137
|
+
def format_severity severity
|
111
138
|
SEV_LABEL[severity] || 'ANY'
|
112
139
|
end
|
113
140
|
|
141
|
+
def format_code color, background = 0
|
142
|
+
if color > 0 && background > 0
|
143
|
+
"#{color};#{background}"
|
144
|
+
elsif color > 0
|
145
|
+
"#{color}"
|
146
|
+
elsif background > 0
|
147
|
+
"#{background}"
|
148
|
+
else
|
149
|
+
'0'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Font Face Methods
|
154
|
+
def text_face mode
|
155
|
+
return FACE_MODES[mode]
|
156
|
+
end
|
157
|
+
|
158
|
+
FACE_MODES = {
|
159
|
+
:reset => 0,
|
160
|
+
:bold => 1,
|
161
|
+
:underscore => 4,
|
162
|
+
:blink => 5 # Please don't...
|
163
|
+
}
|
164
|
+
|
114
165
|
# Color Methods
|
115
|
-
def text_color
|
116
|
-
(COLOR_CODES[color] || 67) + 30
|
166
|
+
def text_color color
|
167
|
+
return (COLOR_CODES[color] || 67) + 30 if color
|
168
|
+
return 0
|
117
169
|
end
|
118
170
|
|
119
|
-
def background_color
|
120
|
-
(COLOR_CODES[color] || 0) + 40
|
171
|
+
def background_color color
|
172
|
+
return (COLOR_CODES[color] || 0) + 40 if color
|
173
|
+
return 0
|
121
174
|
end
|
122
175
|
|
123
176
|
COLOR_CODES = {
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/tests/benchmarks.rb
CHANGED
data/tests/colors.rb
CHANGED
data/tests/levels.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
|
1
|
+
require_relative '../lib/yarl'
|
2
2
|
|
3
3
|
logger = YARL.new color: :green
|
4
4
|
|
5
5
|
def test_levels(logger)
|
6
6
|
logger.fatal "It's just a flesh wound!"
|
7
7
|
logger.error 'PC LOAD LETTER'
|
8
|
-
logger.warn '
|
8
|
+
logger.warn 'Here there be dragons!'
|
9
|
+
logger.notice 'In order to build a hyperspatial express route, Earth will be destroyed.'
|
9
10
|
logger.info 'The more you know'
|
10
|
-
logger.debug 'Better call
|
11
|
-
logger.spam
|
11
|
+
logger.debug 'Better call Terminix'
|
12
|
+
logger.spam "Nobody likes spam\r\n"
|
12
13
|
end
|
13
14
|
|
14
15
|
puts "YARL logger level defaults to INFO"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yarl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Massey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logger
|
@@ -50,8 +50,15 @@ files:
|
|
50
50
|
- ".gitignore"
|
51
51
|
- Gemfile
|
52
52
|
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- examples/complex.rb
|
55
|
+
- examples/simple.rb
|
53
56
|
- lib/yarl.rb
|
54
57
|
- lib/yarl/version.rb
|
58
|
+
- media/examples_complex.png
|
59
|
+
- media/examples_simple.png
|
60
|
+
- media/tests_colors.png
|
61
|
+
- media/tests_levels.png
|
55
62
|
- tests/benchmarks.rb
|
56
63
|
- tests/colors.rb
|
57
64
|
- tests/levels.rb
|
@@ -75,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
82
|
- !ruby/object:Gem::Version
|
76
83
|
version: '0'
|
77
84
|
requirements: []
|
78
|
-
|
79
|
-
rubygems_version: 2.5.2.3
|
85
|
+
rubygems_version: 3.0.1
|
80
86
|
signing_key:
|
81
87
|
specification_version: 4
|
82
88
|
summary: Logger extension that provides colors and clean, default formatting.
|