logue 1.0.11 → 1.0.13
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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -3
- data/lib/logue/colorlog.rb +54 -0
- data/lib/logue/filter.rb +60 -0
- data/lib/logue/frame.rb +19 -10
- data/lib/logue/legacy_logger.rb +69 -0
- data/lib/logue/level.rb +20 -0
- data/lib/logue/line.rb +12 -13
- data/lib/logue/location.rb +15 -16
- data/lib/logue/location_format.rb +36 -35
- data/lib/logue/log.rb +20 -20
- data/lib/logue/loggable.rb +18 -18
- data/lib/logue/logger.rb +93 -200
- data/lib/logue/pathutil.rb +29 -30
- data/lib/logue/severity.rb +5 -6
- data/lib/logue/stack.rb +24 -0
- data/lib/logue/version.rb +1 -1
- data/lib/logue/writer.rb +44 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27c5cf726353cfb91ce2c443c8cd02f5a8c0e272
|
4
|
+
data.tar.gz: 02125714a6032756c58acff5522fe104748bd270
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43947a643388fa154feb99be7c02a3a33dcc701e75e256738f97911bdd982ecb23a236922e8db4bd4693b43eb46a28b56e14313221da001bbeb95b79da67d4a5
|
7
|
+
data.tar.gz: f3aa71bcd285189271ec1a5d48463c79c247d2b57ee58fece9ae4f483ae9f7f69621e103ef5f803d639a7678f3014179d82038067f78649d7cecc89b11799636
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
logue
|
2
2
|
=====
|
3
3
|
|
4
|
-
A Ruby gem for generating logging and debugging output. Logging statements
|
5
|
-
|
6
|
-
called.
|
4
|
+
A Ruby gem for generating logging and debugging output. Logging statements include the file, line,
|
5
|
+
class and method from which the logging method was called.
|
7
6
|
|
8
7
|
## EXAMPLES
|
9
8
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
#
|
4
|
+
# = colorlog.rb
|
5
|
+
#
|
6
|
+
# Logging Module
|
7
|
+
#
|
8
|
+
# Author:: Jeff Pace <jeugenepace@gmail.com>
|
9
|
+
# Documentation:: Author
|
10
|
+
#
|
11
|
+
|
12
|
+
require 'rainbow/x11_color_names'
|
13
|
+
require 'rainbow/color'
|
14
|
+
require 'logue/level'
|
15
|
+
|
16
|
+
#
|
17
|
+
# == ColorLog
|
18
|
+
#
|
19
|
+
# This class logs messages using terminal colors, forwarding them to a log method.
|
20
|
+
#
|
21
|
+
# == Examples
|
22
|
+
#
|
23
|
+
# See the unit tests in colorlog_test.rb
|
24
|
+
#
|
25
|
+
|
26
|
+
module Logue
|
27
|
+
module ColorLog
|
28
|
+
def method_missing meth, *args, &blk
|
29
|
+
# validcolors = Rainbow::X11ColorNames::NAMES
|
30
|
+
validcolors = Rainbow::Color::Named::NAMES
|
31
|
+
if code = validcolors[meth]
|
32
|
+
add_color_method meth.to_s, code + 30
|
33
|
+
send meth, *args, &blk
|
34
|
+
else
|
35
|
+
super
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def respond_to? meth
|
40
|
+
# validcolors = Rainbow::X11ColorNames::NAMES
|
41
|
+
validcolors = Rainbow::Color::Named::NAMES
|
42
|
+
validcolors.include?(meth) || super
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_color_method color, code
|
46
|
+
instmeth = Array.new.tap do |a|
|
47
|
+
a << 'def ' + color.to_s + '(msg = "", lvl = Logue::Level::DEBUG, cname = nil, &blk)'
|
48
|
+
a << ' log("\e[' + code.to_s + 'm#{msg}\e[0m", lvl, cname, &blk)'
|
49
|
+
a << 'end'
|
50
|
+
end
|
51
|
+
instance_eval instmeth.join "\n"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/logue/filter.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
module Logue
|
7
|
+
class Filter
|
8
|
+
include Comparable
|
9
|
+
|
10
|
+
attr_reader :ignored_files
|
11
|
+
attr_reader :ignored_methods
|
12
|
+
attr_reader :ignored_classes
|
13
|
+
|
14
|
+
def initialize ignored_files: Array.new, ignored_methods: Array.new, ignored_classes: Array.new
|
15
|
+
@ignored_files = ignored_files
|
16
|
+
@ignored_methods = ignored_methods
|
17
|
+
@ignored_classes = ignored_classes
|
18
|
+
end
|
19
|
+
|
20
|
+
def ignore_file file
|
21
|
+
@ignored_files << file
|
22
|
+
end
|
23
|
+
|
24
|
+
def log_file file
|
25
|
+
@ignored_files.delete file
|
26
|
+
end
|
27
|
+
|
28
|
+
def ignore_class cls
|
29
|
+
@ignored_classes << cls
|
30
|
+
end
|
31
|
+
|
32
|
+
def log_class cls
|
33
|
+
@ignored_classes.delete cls
|
34
|
+
end
|
35
|
+
|
36
|
+
def ignore_method meth
|
37
|
+
@ignored_methods << meth
|
38
|
+
end
|
39
|
+
|
40
|
+
def log_method meth
|
41
|
+
@ignored_methods.delete meth
|
42
|
+
end
|
43
|
+
|
44
|
+
def log? file, cls, meth
|
45
|
+
!@ignored_files.include?(file) && !@ignored_classes.include?(cls) && !@ignored_methods.include?(meth)
|
46
|
+
end
|
47
|
+
|
48
|
+
def compare_fields
|
49
|
+
[ :ignored_files, :ignored_methods, :ignored_classes ]
|
50
|
+
end
|
51
|
+
|
52
|
+
def <=> other
|
53
|
+
compare_fields.each do |field|
|
54
|
+
cmp = send(field) <=> other.send(field)
|
55
|
+
return cmp if cmp.nonzero?
|
56
|
+
end
|
57
|
+
0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/logue/frame.rb
CHANGED
@@ -9,20 +9,29 @@ module Logue
|
|
9
9
|
|
10
10
|
attr_reader :path
|
11
11
|
attr_reader :line
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :method
|
13
13
|
|
14
|
-
def initialize
|
15
|
-
|
14
|
+
def initialize entry: nil, path: nil, line: nil, method: nil
|
15
|
+
# entry if called from "caller(x)" elements, path/line/method if called from
|
16
|
+
# "caller_location(x)" elements.
|
17
|
+
if entry
|
16
18
|
md = FRAME_RE.match entry
|
17
|
-
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@function = md[3] || ""
|
19
|
+
@path = md[1]
|
20
|
+
@line = md[2].to_i
|
21
|
+
@method = md[3] || ""
|
21
22
|
else
|
22
|
-
@path
|
23
|
-
@line
|
24
|
-
@
|
23
|
+
@path = path
|
24
|
+
@line = line
|
25
|
+
@method = method
|
25
26
|
end
|
26
27
|
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
[ :path, :line, :method ].collect { |field| "#{field}: " + send(field).to_s }.join ", "
|
31
|
+
end
|
32
|
+
|
33
|
+
def formatted format, cname
|
34
|
+
format.format @path, @line, cname, @method
|
35
|
+
end
|
27
36
|
end
|
28
37
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
#
|
4
|
+
# = legacy_logger.rb
|
5
|
+
#
|
6
|
+
# Logging Module
|
7
|
+
#
|
8
|
+
# Author:: Jeff Pace <jeugenepace@gmail.com>
|
9
|
+
# Documentation:: Author
|
10
|
+
#
|
11
|
+
|
12
|
+
require 'logue/filter'
|
13
|
+
require 'logue/location_format'
|
14
|
+
|
15
|
+
module Logue
|
16
|
+
module LegacyLogger
|
17
|
+
def trim= what
|
18
|
+
$stderr.puts "Logger#trim is deprecated, and ignored"
|
19
|
+
end
|
20
|
+
|
21
|
+
def ignore_file fname
|
22
|
+
filter.ignore_file fname
|
23
|
+
end
|
24
|
+
|
25
|
+
def ignore_method methname
|
26
|
+
filter.ignore_method methname
|
27
|
+
end
|
28
|
+
|
29
|
+
def ignore_class classname
|
30
|
+
filter.ignore_class classname
|
31
|
+
end
|
32
|
+
|
33
|
+
def log_file fname
|
34
|
+
filter.log_file fname
|
35
|
+
end
|
36
|
+
|
37
|
+
def log_method methname
|
38
|
+
filter.log_method methname
|
39
|
+
end
|
40
|
+
|
41
|
+
def log_class classname
|
42
|
+
filter.log_class classname
|
43
|
+
end
|
44
|
+
|
45
|
+
def output
|
46
|
+
writer.output
|
47
|
+
end
|
48
|
+
|
49
|
+
def output= obj
|
50
|
+
writer.output = obj
|
51
|
+
end
|
52
|
+
|
53
|
+
def colorize_line
|
54
|
+
writer.colorize_line
|
55
|
+
end
|
56
|
+
|
57
|
+
def colorize_line= b
|
58
|
+
writer.colorize_line = b
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_default_widths
|
62
|
+
self.format = LocationFormat.new
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_color lvl, color
|
66
|
+
writer.colors[lvl] = color
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/logue/level.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
#
|
4
|
+
# = severity.rb
|
5
|
+
#
|
6
|
+
# Logging Module
|
7
|
+
#
|
8
|
+
# Author:: Jeff Pace <jeugenepace@gmail.com>
|
9
|
+
# Documentation:: Author
|
10
|
+
#
|
11
|
+
|
12
|
+
module Logue
|
13
|
+
module Level
|
14
|
+
DEBUG = 0
|
15
|
+
INFO = 1
|
16
|
+
WARN = 2
|
17
|
+
ERROR = 3
|
18
|
+
FATAL = 4
|
19
|
+
end
|
20
|
+
end
|
data/lib/logue/line.rb
CHANGED
@@ -4,19 +4,18 @@
|
|
4
4
|
require 'logue/location_format'
|
5
5
|
|
6
6
|
module Logue
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@msg = msg
|
16
|
-
end
|
7
|
+
class Line
|
8
|
+
attr_reader :location
|
9
|
+
attr_reader :msg
|
10
|
+
|
11
|
+
def initialize location, msg
|
12
|
+
@location = location
|
13
|
+
@msg = msg
|
14
|
+
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
def format locformat
|
17
|
+
logmsg = @location.format locformat
|
18
|
+
logmsg + " " + @msg
|
19
|
+
end
|
21
20
|
end
|
22
21
|
end
|
data/lib/logue/location.rb
CHANGED
@@ -4,22 +4,21 @@
|
|
4
4
|
require 'logue/location_format'
|
5
5
|
|
6
6
|
module Logue
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@function = function
|
20
|
-
end
|
7
|
+
class Location
|
8
|
+
attr_reader :path
|
9
|
+
attr_reader :lineno
|
10
|
+
attr_reader :cls
|
11
|
+
attr_reader :method
|
12
|
+
|
13
|
+
def initialize path, lineno, cls, method
|
14
|
+
@path = path
|
15
|
+
@lineno = lineno
|
16
|
+
@cls = cls
|
17
|
+
@method = method
|
18
|
+
end
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
def format locformat
|
21
|
+
locformat.format @path, @lineno, @cls, @method
|
22
|
+
end
|
24
23
|
end
|
25
24
|
end
|
@@ -3,45 +3,46 @@
|
|
3
3
|
|
4
4
|
require 'logue/pathutil'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
class Logue::LocationFormat
|
13
|
-
attr_accessor :file
|
14
|
-
attr_accessor :line
|
15
|
-
attr_accessor :function
|
16
|
-
attr_accessor :trim
|
17
|
-
|
18
|
-
def initialize args = Hash.new
|
19
|
-
@file = args.fetch :file, Logue::LocationFormatWidths::DEFAULT_FILENAME
|
20
|
-
@line = args.fetch :line, Logue::LocationFormatWidths::DEFAULT_LINE
|
21
|
-
@function = args.fetch :function, Logue::LocationFormatWidths::DEFAULT_FUNCTION
|
22
|
-
@trim = args.fetch :trim, true
|
23
|
-
end
|
24
|
-
|
25
|
-
def copy args
|
26
|
-
values = { file: @file, line: @line, function: @function, trim: @trim }
|
27
|
-
self.class.new values.merge(args)
|
28
|
-
end
|
29
|
-
|
30
|
-
def format path, line, cls, func
|
31
|
-
if cls
|
32
|
-
func = cls.to_s + "#" + func
|
6
|
+
module Logue
|
7
|
+
class LocationFormat
|
8
|
+
module Defaults
|
9
|
+
FILENAME = -25
|
10
|
+
LINE = 4
|
11
|
+
METHOD = -20
|
33
12
|
end
|
34
13
|
|
35
|
-
|
36
|
-
|
37
|
-
|
14
|
+
attr_accessor :file
|
15
|
+
attr_accessor :line
|
16
|
+
attr_accessor :method
|
17
|
+
attr_accessor :trim
|
18
|
+
|
19
|
+
def initialize file: Defaults::FILENAME, line: Defaults::LINE, method: Defaults::METHOD, trim: true
|
20
|
+
@file = file
|
21
|
+
@line = line
|
22
|
+
@method = method
|
23
|
+
@trim = trim
|
24
|
+
end
|
25
|
+
|
26
|
+
def copy args
|
27
|
+
values = { file: @file, line: @line, method: @method, trim: @trim }
|
28
|
+
self.class.new values.merge(args)
|
38
29
|
end
|
39
30
|
|
40
|
-
format
|
41
|
-
|
42
|
-
|
31
|
+
def format path, line, cls, func
|
32
|
+
if cls
|
33
|
+
func = cls.to_s + "#" + func
|
34
|
+
end
|
35
|
+
|
36
|
+
if trim
|
37
|
+
path = PathUtil.trim_right path.to_s, @file
|
38
|
+
func = PathUtil.trim_left func, @method
|
39
|
+
end
|
40
|
+
|
41
|
+
sprintf format_string, path, line, func
|
42
|
+
end
|
43
43
|
|
44
|
-
|
45
|
-
|
44
|
+
def format_string
|
45
|
+
"[%#{@file}s:%#{@line}d] {%#{@method}s}"
|
46
|
+
end
|
46
47
|
end
|
47
48
|
end
|
data/lib/logue/log.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
#
|
11
11
|
|
12
12
|
require 'logue/logger'
|
13
|
-
require 'logue/
|
13
|
+
require 'logue/level'
|
14
14
|
require 'logue/colors'
|
15
15
|
|
16
16
|
#
|
@@ -54,7 +54,7 @@ module Logue
|
|
54
54
|
class Log
|
55
55
|
$LOGGING_LEVEL = nil
|
56
56
|
|
57
|
-
include
|
57
|
+
include Level
|
58
58
|
|
59
59
|
# by default, class methods delegate to a single app-wide log.
|
60
60
|
|
@@ -115,8 +115,8 @@ module Logue
|
|
115
115
|
|
116
116
|
def self.add_color_method color, code
|
117
117
|
instmeth = Array.new
|
118
|
-
instmeth << "def #{color} msg = \"\", lvl = Log::DEBUG,
|
119
|
-
instmeth << " logger.#{color} (\"\\e[#{code}m\#{msg\}\\e[0m\", lvl,
|
118
|
+
instmeth << "def #{color} msg = \"\", lvl = Log::DEBUG, cname = nil, &blk"
|
119
|
+
instmeth << " logger.#{color} (\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, cname, &blk)"
|
120
120
|
instmeth << "end"
|
121
121
|
|
122
122
|
# an instance, but on the class object, not the log instance:
|
@@ -128,45 +128,45 @@ module Logue
|
|
128
128
|
logger.set_widths file_width, line_width, func_width
|
129
129
|
end
|
130
130
|
|
131
|
-
def self.debug msg = "",
|
132
|
-
logger.debug msg,
|
131
|
+
def self.debug msg = "", cname = nil, &blk
|
132
|
+
logger.debug msg, cname, &blk
|
133
133
|
end
|
134
134
|
|
135
|
-
def self.info msg = "",
|
136
|
-
logger.info msg,
|
135
|
+
def self.info msg = "", cname = nil, &blk
|
136
|
+
logger.info msg, cname, &blk
|
137
137
|
end
|
138
138
|
|
139
|
-
def self.fatal msg = "",
|
140
|
-
logger.fatal msg,
|
139
|
+
def self.fatal msg = "", cname = nil, &blk
|
140
|
+
logger.fatal msg, cname, &blk
|
141
141
|
end
|
142
142
|
|
143
|
-
def self.log msg = "", lvl = DEBUG,
|
144
|
-
logger.log msg, lvl,
|
143
|
+
def self.log msg = "", lvl = DEBUG, cname = nil, &blk
|
144
|
+
logger.log msg, lvl, cname, &blk
|
145
145
|
end
|
146
146
|
|
147
|
-
def self.stack msg = "", lvl = DEBUG,
|
148
|
-
logger.stack msg, lvl,
|
147
|
+
def self.stack msg = "", lvl = DEBUG, cname = nil, &blk
|
148
|
+
logger.stack msg, lvl, cname, &blk
|
149
149
|
end
|
150
150
|
|
151
|
-
def self.warn msg = "",
|
151
|
+
def self.warn msg = "", cname = nil, &blk
|
152
152
|
if verbose
|
153
|
-
logger.log msg, WARN,
|
153
|
+
logger.log msg, WARN, cname, &blk
|
154
154
|
else
|
155
155
|
$stderr.puts "WARNING: " + msg
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
|
-
def self.error msg = "",
|
159
|
+
def self.error msg = "", cname = nil, &blk
|
160
160
|
if verbose
|
161
|
-
logger.log msg, ERROR,
|
161
|
+
logger.log msg, ERROR, cname, &blk
|
162
162
|
else
|
163
163
|
$stderr.puts "ERROR: " + msg
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
167
|
-
def self.write msg,
|
167
|
+
def self.write msg, cname = nil, &blk
|
168
168
|
if verbose
|
169
|
-
stack msg, Log::WARN,
|
169
|
+
stack msg, Log::WARN, cname, &blk
|
170
170
|
elsif quiet
|
171
171
|
# nothing
|
172
172
|
else
|
data/lib/logue/loggable.rb
CHANGED
@@ -41,36 +41,36 @@ require 'logue/colors'
|
|
41
41
|
module Logue
|
42
42
|
module Loggable
|
43
43
|
# Logs the given message, including the class whence invoked.
|
44
|
-
def log msg = "", lvl = Log::DEBUG,
|
45
|
-
delegate_log_class.log msg, lvl,
|
44
|
+
def log msg = "", lvl = Log::DEBUG, &blk
|
45
|
+
delegate_log_class.log msg, lvl, self.class.to_s, &blk
|
46
46
|
end
|
47
47
|
|
48
|
-
def debug msg = "",
|
49
|
-
delegate_log_class.debug msg,
|
48
|
+
def debug msg = "", &blk
|
49
|
+
delegate_log_class.debug msg, self.class.to_s, &blk
|
50
50
|
end
|
51
51
|
|
52
|
-
def info msg = "",
|
53
|
-
delegate_log_class.info msg,
|
52
|
+
def info msg = "", &blk
|
53
|
+
delegate_log_class.info msg, self.class.to_s, &blk
|
54
54
|
end
|
55
55
|
|
56
|
-
def warn msg = "",
|
57
|
-
delegate_log_class.warn msg,
|
56
|
+
def warn msg = "", &blk
|
57
|
+
delegate_log_class.warn msg, self.class.to_s, &blk
|
58
58
|
end
|
59
59
|
|
60
|
-
def error msg = "",
|
61
|
-
delegate_log_class.error msg,
|
60
|
+
def error msg = "", &blk
|
61
|
+
delegate_log_class.error msg, self.class.to_s, &blk
|
62
62
|
end
|
63
63
|
|
64
|
-
def fatal msg = "",
|
65
|
-
delegate_log_class.fatal msg,
|
64
|
+
def fatal msg = "", &blk
|
65
|
+
delegate_log_class.fatal msg, self.class.to_s, &blk
|
66
66
|
end
|
67
67
|
|
68
|
-
def stack msg = "", lvl = Log::DEBUG,
|
69
|
-
delegate_log_class.stack msg, lvl,
|
68
|
+
def stack msg = "", lvl = Log::DEBUG, &blk
|
69
|
+
delegate_log_class.stack msg, lvl, self.class.to_s, &blk
|
70
70
|
end
|
71
71
|
|
72
|
-
def write msg = "",
|
73
|
-
delegate_log_class.write msg,
|
72
|
+
def write msg = "", &blk
|
73
|
+
delegate_log_class.write msg, self.class.to_s, &blk
|
74
74
|
end
|
75
75
|
|
76
76
|
def method_missing meth, *args, &blk
|
@@ -89,8 +89,8 @@ module Logue
|
|
89
89
|
|
90
90
|
def add_color_method color, code
|
91
91
|
meth = Array.new
|
92
|
-
meth << "def #{color}(msg = \"\", lvl = Log::DEBUG,
|
93
|
-
meth << " Log.#{color} msg, lvl,
|
92
|
+
meth << "def #{color}(msg = \"\", lvl = Log::DEBUG, cname = nil, &blk)"
|
93
|
+
meth << " Log.#{color} msg, lvl, self.class.to_s, &blk"
|
94
94
|
meth << "end"
|
95
95
|
self.class.module_eval meth.join("\n")
|
96
96
|
end
|
data/lib/logue/logger.rb
CHANGED
@@ -9,13 +9,12 @@
|
|
9
9
|
# Documentation:: Author
|
10
10
|
#
|
11
11
|
|
12
|
-
require '
|
13
|
-
require '
|
14
|
-
require '
|
15
|
-
require 'logue/
|
16
|
-
require 'logue/
|
17
|
-
require 'logue/
|
18
|
-
require 'logue/frame'
|
12
|
+
require 'logue/level'
|
13
|
+
require 'logue/colorlog'
|
14
|
+
require 'logue/writer'
|
15
|
+
require 'logue/filter'
|
16
|
+
require 'logue/legacy_logger'
|
17
|
+
require 'logue/stack'
|
19
18
|
|
20
19
|
#
|
21
20
|
# == Logger
|
@@ -29,220 +28,114 @@ require 'logue/frame'
|
|
29
28
|
#
|
30
29
|
|
31
30
|
module Logue
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
attr_accessor :format
|
43
|
-
|
44
|
-
include Logue::Log::Severity
|
45
|
-
|
46
|
-
def initialize
|
47
|
-
reset
|
48
|
-
end
|
49
|
-
|
50
|
-
def verbose= v
|
51
|
-
@level = case v
|
52
|
-
when TrueClass
|
53
|
-
DEBUG
|
54
|
-
when FalseClass
|
55
|
-
FATAL
|
56
|
-
when Integer
|
57
|
-
v
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def reset
|
62
|
-
@level = FATAL
|
63
|
-
@ignored_files = Hash.new
|
64
|
-
@ignored_methods = Hash.new
|
65
|
-
@ignored_classes = Hash.new
|
66
|
-
@output = $stdout
|
67
|
-
@colors = Array.new
|
68
|
-
@colorize_line = false
|
69
|
-
@format = Logue::LocationFormat.new
|
70
|
-
end
|
71
|
-
|
72
|
-
def trim= what
|
73
|
-
end
|
74
|
-
|
75
|
-
def verbose
|
76
|
-
level <= DEBUG
|
77
|
-
end
|
78
|
-
|
79
|
-
def set_default_widths
|
80
|
-
@format = Logue::LocationFormat.new
|
81
|
-
end
|
82
|
-
|
83
|
-
def verbose
|
84
|
-
level <= DEBUG
|
85
|
-
end
|
86
|
-
|
87
|
-
def quiet
|
88
|
-
level >= WARN
|
89
|
-
end
|
90
|
-
|
91
|
-
def quiet= b
|
92
|
-
level = b ? WARN : DEBUG
|
93
|
-
end
|
94
|
-
|
95
|
-
# Assigns output to a file with the given name. Returns the file; the client is responsible for
|
96
|
-
# closing it.
|
97
|
-
def outfile= f
|
98
|
-
@output = f.kind_of?(IO) ? f : File.new(f, "w")
|
99
|
-
end
|
100
|
-
|
101
|
-
# Creates a printf format for the given widths, for aligning output. To lead lines with zeros
|
102
|
-
# (e.g., "00317") the line_width argument must be a string, not an integer.
|
103
|
-
def set_widths file, line, function
|
104
|
-
@format = Logue::LocationFormat.new file: file, line: line, function: function
|
105
|
-
end
|
31
|
+
class Logger
|
32
|
+
include LegacyLogger
|
33
|
+
include ColorLog
|
34
|
+
|
35
|
+
attr_accessor :level
|
36
|
+
attr_accessor :format
|
37
|
+
attr_accessor :filter
|
38
|
+
attr_accessor :writer
|
39
|
+
|
40
|
+
include Level
|
106
41
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
42
|
+
def initialize format: LocationFormat.new, level: WARN, filter: Filter.new, writer: Writer.new
|
43
|
+
reset format: format, level: level, filter: filter, writer: writer
|
44
|
+
end
|
45
|
+
|
46
|
+
def verbose= v
|
47
|
+
@level = case v
|
48
|
+
when TrueClass
|
49
|
+
DEBUG
|
50
|
+
when FalseClass
|
51
|
+
FATAL
|
52
|
+
when Integer
|
53
|
+
v
|
54
|
+
end
|
55
|
+
end
|
118
56
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
def log_method methname
|
124
|
-
ignored_methods.delete methname
|
125
|
-
end
|
126
|
-
|
127
|
-
def log_class classname
|
128
|
-
ignored_classes.delete classname
|
129
|
-
end
|
57
|
+
def verbose
|
58
|
+
@level <= DEBUG
|
59
|
+
end
|
130
60
|
|
131
|
-
|
132
|
-
|
133
|
-
|
61
|
+
def reset format: LocationFormat.new, level: FATAL, filter: Filter.new, writer: Writer.new
|
62
|
+
@level = level
|
63
|
+
@filter = filter
|
64
|
+
@format = format
|
65
|
+
@writer = writer
|
66
|
+
end
|
67
|
+
|
68
|
+
def quiet
|
69
|
+
@level >= WARN
|
70
|
+
end
|
134
71
|
|
135
|
-
|
136
|
-
|
137
|
-
|
72
|
+
def quiet= b
|
73
|
+
@level = b ? WARN : DEBUG
|
74
|
+
end
|
138
75
|
|
139
|
-
|
140
|
-
|
141
|
-
|
76
|
+
# Assigns output to a file with the given name. Returns the file; the client is responsible for
|
77
|
+
# closing it.
|
78
|
+
def outfile= f
|
79
|
+
io = f.kind_of?(IO) ? f : File.new(f, "w")
|
80
|
+
@writer.output = io
|
81
|
+
end
|
142
82
|
|
143
|
-
|
144
|
-
|
145
|
-
|
83
|
+
# Creates a printf format for the given widths, for aligning output. To lead lines with zeros
|
84
|
+
# (e.g., "00317") the line argument must be a string, with leading zeros, not an integer.
|
85
|
+
def set_widths file, line, method
|
86
|
+
@format = LocationFormat.new file: file, line: line, method: method
|
87
|
+
end
|
146
88
|
|
147
|
-
|
148
|
-
|
149
|
-
|
89
|
+
def debug msg = "", cname = nil, &blk
|
90
|
+
log msg, DEBUG, cname, &blk
|
91
|
+
end
|
150
92
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
frame = nil
|
155
|
-
|
156
|
-
stk = caller 0
|
157
|
-
stk.reverse.each_with_index do |frm, idx|
|
158
|
-
if frm.index(%r{logue/log.*:\d+:in\b})
|
159
|
-
break
|
160
|
-
else
|
161
|
-
frame = frm
|
162
|
-
end
|
163
|
-
end
|
93
|
+
def info msg = "", cname = nil, &blk
|
94
|
+
log msg, INFO, cname, &blk
|
95
|
+
end
|
164
96
|
|
165
|
-
|
97
|
+
def warn msg = "", cname = nil, &blk
|
98
|
+
log msg, WARN, cname, &blk
|
166
99
|
end
|
167
|
-
end
|
168
100
|
|
169
|
-
|
170
|
-
|
171
|
-
if lvl >= level
|
172
|
-
stk = caller depth
|
173
|
-
stk.each do |frame|
|
174
|
-
print_stack_frame frame, cname, msg, lvl, &blk
|
175
|
-
cname = nil
|
176
|
-
msg = ""
|
177
|
-
end
|
101
|
+
def error msg = "", cname = nil, &blk
|
102
|
+
log msg, ERROR, cname, &blk
|
178
103
|
end
|
179
|
-
end
|
180
104
|
|
181
|
-
|
182
|
-
|
183
|
-
func = cname ? cname + "#" + frm.function : frm.function
|
184
|
-
|
185
|
-
unless ignored_files[frm.path] || (cname && ignored_classes[cname]) || ignored_methods[func]
|
186
|
-
print_formatted(frm.path, frm.line, func, msg, lvl, &blk)
|
105
|
+
def fatal msg = "", cname = nil, &blk
|
106
|
+
log msg, FATAL, cname, &blk
|
187
107
|
end
|
188
|
-
end
|
189
108
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
def print hdr, msg, lvl, &blk
|
196
|
-
if blk
|
197
|
-
x = blk.call
|
198
|
-
if x.kind_of? String
|
199
|
-
msg = x
|
200
|
-
else
|
201
|
-
return
|
202
|
-
end
|
109
|
+
# Logs the given message.
|
110
|
+
def log msg = "", lvl = DEBUG, cname = nil, &blk
|
111
|
+
log_frames cname, msg, lvl, 0, &blk
|
203
112
|
end
|
204
113
|
|
205
|
-
|
114
|
+
# Shows the current stack.
|
115
|
+
def stack msg = "", lvl = DEBUG, cname = nil, &blk
|
116
|
+
log_frames cname, msg, lvl, -1, &blk
|
117
|
+
end
|
206
118
|
|
207
|
-
|
208
|
-
if
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
119
|
+
def log_frames cname, msg, lvl, num, &blk
|
120
|
+
if lvl >= level
|
121
|
+
stack = Stack.new
|
122
|
+
stack.filtered[0 .. num].each do |frame|
|
123
|
+
log_frame frame, cname, msg, lvl, &blk
|
124
|
+
cname = nil
|
125
|
+
msg = ""
|
126
|
+
end
|
213
127
|
end
|
214
|
-
|
215
|
-
@output.puts hdr + " " + msg
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
def set_color lvl, color
|
220
|
-
@colors[lvl] = color
|
221
|
-
end
|
128
|
+
end
|
222
129
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
if code = validcolors[meth]
|
228
|
-
add_color_method meth.to_s, code + 30
|
229
|
-
send meth, *args, &blk
|
230
|
-
else
|
231
|
-
super
|
130
|
+
def log_frame frame, cname, msg, lvl, &blk
|
131
|
+
if @filter.log? frame.path, cname, frame.method
|
132
|
+
print_formatted frame, cname, msg, lvl, &blk
|
133
|
+
end
|
232
134
|
end
|
233
|
-
end
|
234
|
-
|
235
|
-
def respond_to? meth
|
236
|
-
# validcolors = Rainbow::X11ColorNames::NAMES
|
237
|
-
validcolors = Rainbow::Color::Named::NAMES
|
238
|
-
validcolors.include?(meth) || super
|
239
|
-
end
|
240
135
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
instmeth << "end"
|
246
|
-
instance_eval instmeth.join("\n")
|
136
|
+
def print_formatted frame, cname, msg, lvl, &blk
|
137
|
+
location = frame.formatted @format, cname
|
138
|
+
@writer.print location, msg, level, &blk
|
139
|
+
end
|
247
140
|
end
|
248
141
|
end
|
data/lib/logue/pathutil.rb
CHANGED
@@ -2,42 +2,41 @@
|
|
2
2
|
# -*- ruby -*-
|
3
3
|
|
4
4
|
module Logue
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
str[0 ... maxlen.to_i.abs]
|
11
|
-
end
|
5
|
+
class PathUtil
|
6
|
+
class << self
|
7
|
+
def trim_left str, maxlen
|
8
|
+
str[0 ... maxlen.to_i.abs]
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
11
|
+
def trim_right str, maxlen
|
12
|
+
mxln = maxlen.abs
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
if str.length > mxln
|
15
|
+
trim_path_right str, maxlen
|
16
|
+
else
|
17
|
+
str
|
18
|
+
end
|
20
19
|
end
|
21
|
-
end
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
str = newstr
|
32
|
-
else
|
33
|
-
newstr = "..." + "/" + str
|
34
|
-
if newstr.length <= mxln
|
21
|
+
def trim_path_right path, maxlen
|
22
|
+
mxln = maxlen.abs
|
23
|
+
|
24
|
+
comps = path.split "/"
|
25
|
+
str = comps.pop
|
26
|
+
comps.reverse.each do |comp|
|
27
|
+
newstr = comp + "/" + str
|
28
|
+
if newstr.length + 4 <= mxln
|
35
29
|
str = newstr
|
30
|
+
else
|
31
|
+
newstr = "..." + "/" + str
|
32
|
+
if newstr.length <= mxln
|
33
|
+
str = newstr
|
34
|
+
end
|
35
|
+
break
|
36
36
|
end
|
37
|
-
break
|
38
37
|
end
|
39
|
-
|
40
|
-
|
41
|
-
end
|
38
|
+
str
|
39
|
+
end
|
40
|
+
end
|
42
41
|
end
|
43
42
|
end
|
data/lib/logue/severity.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/ruby -w
|
2
2
|
# -*- ruby -*-
|
3
3
|
#
|
4
|
-
# =
|
4
|
+
# = level.rb
|
5
5
|
#
|
6
6
|
# Logging Module
|
7
7
|
#
|
@@ -9,14 +9,13 @@
|
|
9
9
|
# Documentation:: Author
|
10
10
|
#
|
11
11
|
|
12
|
+
require 'logue/level'
|
13
|
+
|
12
14
|
module Logue
|
13
15
|
class Log
|
16
|
+
# legacy module that is replaced by Logue::Level
|
14
17
|
module Severity
|
15
|
-
|
16
|
-
INFO = 1
|
17
|
-
WARN = 2
|
18
|
-
ERROR = 3
|
19
|
-
FATAL = 4
|
18
|
+
include Logue::Level
|
20
19
|
end
|
21
20
|
end
|
22
21
|
end
|
data/lib/logue/stack.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'logue/frame'
|
5
|
+
|
6
|
+
module Logue
|
7
|
+
class Stack
|
8
|
+
attr_reader :frames
|
9
|
+
|
10
|
+
def initialize depth: 2
|
11
|
+
# caller_locations requires Ruby 2.0+
|
12
|
+
locations = caller_locations depth
|
13
|
+
@frames = locations.collect do |loc|
|
14
|
+
# no absolute_path from "(eval)"
|
15
|
+
Frame.new path: loc.absolute_path || loc.path, line: loc.lineno, method: loc.label
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def filtered
|
20
|
+
logframe = @frames.rindex { |frm| frm.path.include? "logue/lib/logue" }
|
21
|
+
@frames[logframe + 1 .. -1]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/logue/version.rb
CHANGED
data/lib/logue/writer.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
module Logue
|
5
|
+
class Writer
|
6
|
+
attr_accessor :output
|
7
|
+
attr_accessor :colors
|
8
|
+
attr_accessor :colorize_line
|
9
|
+
|
10
|
+
def initialize output: $stdout, colors: Array.new, colorize_line: false
|
11
|
+
@output = output
|
12
|
+
@colors = colors
|
13
|
+
@colorize_line = colorize_line
|
14
|
+
end
|
15
|
+
|
16
|
+
def print location, msg, lvl, &blk
|
17
|
+
if blk
|
18
|
+
x = blk.call
|
19
|
+
if x.kind_of? String
|
20
|
+
msg = x
|
21
|
+
else
|
22
|
+
return
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
msg = msg.to_s.chomp
|
27
|
+
line = line location, msg, lvl
|
28
|
+
@output.puts line
|
29
|
+
end
|
30
|
+
|
31
|
+
def line location, msg, lvl
|
32
|
+
if lvlcol = @colors[lvl]
|
33
|
+
if @colorize_line
|
34
|
+
line = location + " " + msg
|
35
|
+
line.color lvlcol
|
36
|
+
else
|
37
|
+
location + " " + msg.color(lvlcol)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
location + " " + msg
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Pace
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,8 +97,12 @@ files:
|
|
97
97
|
- bin/console
|
98
98
|
- bin/setup
|
99
99
|
- lib/logue.rb
|
100
|
+
- lib/logue/colorlog.rb
|
100
101
|
- lib/logue/colors.rb
|
102
|
+
- lib/logue/filter.rb
|
101
103
|
- lib/logue/frame.rb
|
104
|
+
- lib/logue/legacy_logger.rb
|
105
|
+
- lib/logue/level.rb
|
102
106
|
- lib/logue/line.rb
|
103
107
|
- lib/logue/location.rb
|
104
108
|
- lib/logue/location_format.rb
|
@@ -107,7 +111,9 @@ files:
|
|
107
111
|
- lib/logue/logger.rb
|
108
112
|
- lib/logue/pathutil.rb
|
109
113
|
- lib/logue/severity.rb
|
114
|
+
- lib/logue/stack.rb
|
110
115
|
- lib/logue/version.rb
|
116
|
+
- lib/logue/writer.rb
|
111
117
|
- logue.gemspec
|
112
118
|
homepage: https://www.github.com/jpace/logue
|
113
119
|
licenses:
|