extended-logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b144a61490df22da5ade97aedb3fae7786c1c93
4
+ data.tar.gz: 3d0c3919b223b08aecc6630115d2f6c8ff86eb09
5
+ SHA512:
6
+ metadata.gz: 6ee0bbd9019da7244ee7a00638db0d10605a23bc3f06a22215baabcf78699f6ab0fb97e94694e31390a133692a51fe36cd7fbe340ee5b8930c703fd8c7bb9595
7
+ data.tar.gz: 10ca841e6bdcbe41f83ef74ad847c36d1ea42646ac794793f91cb9b01c7c5ba963fbc25b4fa6bc558c948025fb823ce9939c7da955aabf25edeae8be9029572d
@@ -0,0 +1,6 @@
1
+ require 'logger'
2
+
3
+ require 'extended_logger/extended_logger'
4
+ require 'extended_logger/factory'
5
+ require 'extended_logger/formatter'
6
+ require 'extended_logger/colored_formatter'
@@ -0,0 +1,51 @@
1
+ class ExtendedLogger
2
+ class ColoredFormatter < Formatter
3
+ ANSI_COLORS = %i(black red green yellow blue magenta cyan white)
4
+
5
+ attr_writer :palette
6
+
7
+ def bg *args
8
+ col :bg, *args
9
+ end
10
+
11
+ def call *arguments
12
+ log_entry = delegate *arguments
13
+
14
+ severity = arguments.fetch 0
15
+ colorizer = palette.fetch severity do
16
+ -> message { message }
17
+ end
18
+
19
+ instance_exec log_entry, &colorizer
20
+ end
21
+
22
+ def col fgbg, color_code, intensity_code, str
23
+ color_num = ANSI_COLORS.index color_code
24
+ intensity_num = { :normal => 0, :bright => 1 }.fetch intensity_code
25
+ fgbg_num = { :fg => 3, :bg => 4 }.fetch fgbg
26
+ "\e[#{intensity_num};#{fgbg_num}#{color_num}m#{str}\e[0m"
27
+ end
28
+
29
+ def fg *args
30
+ col :fg, *args
31
+ end
32
+
33
+ def default_palette
34
+ @@default_palette ||= {
35
+ 'FOCUS' => -> msg { bg(:blue, :normal, fg(:white, :bright, msg)) },
36
+ 'ANY' => -> msg { bg(:cyan, :normal, fg(:white, :bright, msg)) },
37
+ 'FATAL' => -> msg { bg(:red, :bright, fg(:white, :bright, msg)) },
38
+ 'ERROR' => -> msg { fg(:red, :bright, msg) },
39
+ 'WARN' => -> msg { fg(:yellow, :normal, msg) },
40
+ 'INFO' => -> msg { fg(:green, :normal, msg) },
41
+ 'DEBUG' => -> msg { fg(:blue, :normal, msg) },
42
+ 'TRACE' => -> msg { fg(:magenta, :bright, msg) },
43
+ 'DATA' => -> msg { fg(:cyan, :normal, msg) }
44
+ }
45
+ end
46
+
47
+ def palette
48
+ @palette || default_palette
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,77 @@
1
+ class ExtendedLogger < Logger
2
+ def self.configure receiver, attribute_name=nil
3
+ attribute_name ||= 'logger'.freeze
4
+ setter = "#{attribute_name}=".freeze
5
+
6
+ logger = get receiver
7
+
8
+ receiver.public_send setter, logger
9
+ end
10
+
11
+ def self.get receiver
12
+ receiver_name =
13
+ if receiver.is_a? Module
14
+ receiver.name
15
+ else
16
+ receiver.class.name
17
+ end
18
+
19
+ logger = Factory.()
20
+
21
+ logger.progname = "(#{receiver_name})"
22
+ logger
23
+ end
24
+
25
+ def self.instance
26
+ @instance ||= Factory.()
27
+ end
28
+
29
+ def self.levels
30
+ (-5..6)
31
+ end
32
+
33
+ def data *arguments, &block
34
+ add -2, *arguments, &block
35
+ end
36
+
37
+ def format_severity severity
38
+ unless self.class.levels.include? severity
39
+ Kernel.warn "(extended-logger) Unknown log severity level #{severity.inspect}"
40
+ end
41
+
42
+ case severity
43
+ when -5 then 'OBS_DATA'.freeze
44
+ when -4 then 'OBS_TRACE'.freeze
45
+ when -3 then 'OBS_DEBUG'.freeze
46
+ when -2 then 'DATA'.freeze
47
+ when -1 then 'TRACE'.freeze
48
+ when 6 then 'FOCUS'.freeze
49
+ else super
50
+ end
51
+ end
52
+
53
+ def io
54
+ return unless @logdev
55
+ @logdev.dev
56
+ end
57
+
58
+ def obs_data *arguments, &block
59
+ add -5, *arguments, &block
60
+ end
61
+
62
+ def obs_debug *arguments, &block
63
+ add -3, *arguments, &block
64
+ end
65
+
66
+ def obs_trace *arguments, &block
67
+ add -4, *arguments, &block
68
+ end
69
+
70
+ def trace *arguments, &block
71
+ add -1, *arguments, &block
72
+ end
73
+
74
+ def focus *arguments, &block
75
+ add 6, *arguments, &block
76
+ end
77
+ end
@@ -0,0 +1,43 @@
1
+ class ExtendedLogger
2
+ class Factory
3
+ attr_reader :env
4
+
5
+ def initialize env
6
+ @env = env
7
+ end
8
+
9
+ def self.build env=nil
10
+ env ||= ENV
11
+ new env
12
+ end
13
+
14
+ def self.call *arguments
15
+ instance = build *arguments
16
+ instance.()
17
+ end
18
+
19
+ def call
20
+ logger = ExtendedLogger.new device
21
+
22
+ if colors? logger.io
23
+ logger.formatter = ColoredFormatter.new
24
+ else
25
+ logger.formatter = Formatter.new
26
+ end
27
+
28
+ logger
29
+ end
30
+
31
+ def colors? io
32
+ if io.tty?
33
+ ENV['LOGGER_COLORS'] != 'off'
34
+ else
35
+ ENV['LOGGER_COLORS'] == 'on'
36
+ end
37
+ end
38
+
39
+ def device
40
+ ENV['LOGGER_DEVICE'] || $stdout
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ class ExtendedLogger
2
+ class Formatter
3
+ attr_writer :logger_formatter
4
+
5
+ def call *arguments
6
+ delegate *arguments
7
+ end
8
+
9
+ def delegate *arguments, message
10
+ message = format_message message, arguments
11
+ logger_formatter.(*arguments, message)
12
+ end
13
+
14
+ def format_message message, arguments
15
+ output = ''
16
+
17
+ message.each_line "\n".freeze do |line|
18
+ if line == "\n".freeze
19
+ line = "\\n"
20
+ else
21
+ line.gsub! "\r".freeze, "\\r".freeze
22
+ end
23
+
24
+ if output.empty?
25
+ output.concat line
26
+ else
27
+ formatted_line = logger_formatter.(*arguments, line.chomp("\n".freeze))
28
+ output.concat formatted_line
29
+ end
30
+ end
31
+
32
+ output.chomp "\n".freeze
33
+ end
34
+
35
+ def logger_formatter
36
+ @logger_formatter ||= Logger::Formatter.new
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Ladd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ extended-logger provides a richer logger implementation than the built in ruby
15
+ logger, offering more log levels and colors. In addition, loggers can be
16
+ constructed for your classes and modules, which include the constant name in
17
+ output for easy filtering.
18
+ email: nathanladd+github@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/extended_logger.rb
24
+ - lib/extended_logger/colored_formatter.rb
25
+ - lib/extended_logger/extended_logger.rb
26
+ - lib/extended_logger/factory.rb
27
+ - lib/extended_logger/formatter.rb
28
+ homepage:
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.4.5.1
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Provides an extended logger offering more features than the ruby logger
52
+ test_files: []
53
+ has_rdoc: