extended-logger 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04c42a1f441f494d24cd679dd976708be64aa427
4
- data.tar.gz: 6df62ebef815689873aaeb6a06a0871fa3f08683
3
+ metadata.gz: 1f2713c515733dc13f51bf477ac74c0c9f0ca10b
4
+ data.tar.gz: c5e036c168e6f6fe60cf61aca39651622ff2c90c
5
5
  SHA512:
6
- metadata.gz: 360523b782677d1ee1b9d047296dabad796437ef2323dc2c3c72647557b772d37937d504abfefde0be4e118875498098f14210b17b3d5d8351c7fb741e1a02cc
7
- data.tar.gz: 84c9ec6267c63090142cf11be93e5470d8775222a30b7d7f4a0383220a1024226e27868c432c645186a05e8dd1382941ec2ace6514569ab09f4365c4e4f9563b
6
+ metadata.gz: 43e937e9ff1752630a29c230850da54a4a3596b9baf24bb472f9cc0c9fa2fce50c37b1fedbd73de6d315c37d174a40b3271d085f42bf60a4fcb0607eee1d3f67
7
+ data.tar.gz: 8057fe6a05cd17779e1d57e8b1060e7362746e8d2b46e317bd1e9208293fe8c79ed7b89a7f6d5624fd0219fde869de77491b4055c4e9b4258b5d068b0762bde8
@@ -3,4 +3,3 @@ require 'logger'
3
3
  require 'extended_logger/extended_logger'
4
4
  require 'extended_logger/factory'
5
5
  require 'extended_logger/formatter'
6
- require 'extended_logger/colored_formatter'
@@ -27,27 +27,34 @@ class ExtendedLogger < Logger
27
27
  end
28
28
 
29
29
  def self.levels
30
- (-5..6)
30
+ @levels ||= {
31
+ -7 => 'OBSOLETE'.freeze,
32
+ -6 => 'DATA'.freeze,
33
+ -5 => 'TRACE'.freeze,
34
+ -4 => 'DEBUG'.freeze,
35
+ -3 => 'OPT_DATA'.freeze,
36
+ -2 => 'OPT_TRACE'.freeze,
37
+ -1 => 'OPT_DEBUG'.freeze,
38
+ 0 => 'INFO'.freeze,
39
+ 1 => 'PASS'.freeze,
40
+ 2 => 'FAIL'.freeze,
41
+ 3 => 'FOCUS'.freeze,
42
+ 4 => 'WARN'.freeze,
43
+ 5 => 'ERROR'.freeze,
44
+ 6 => 'FATAL'.freeze,
45
+ }
31
46
  end
32
47
 
33
- def data *arguments, &block
34
- add -2, *arguments, &block
48
+ def self.level? level
49
+ levels.key? level
35
50
  end
36
51
 
37
52
  def format_severity severity
38
- unless self.class.levels.include? severity
53
+ unless self.class.level? severity
39
54
  Kernel.warn "(extended-logger) Unknown log severity level #{severity.inspect}"
40
55
  end
41
56
 
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
57
+ self.class.levels.fetch severity, 'ANY'.freeze
51
58
  end
52
59
 
53
60
  def io
@@ -55,23 +62,13 @@ class ExtendedLogger < Logger
55
62
  @logdev.dev
56
63
  end
57
64
 
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
65
+ def unknown *arguments, &block
66
+ add 7, *arguments, &block
72
67
  end
73
68
 
74
- def focus *arguments, &block
75
- add 6, *arguments, &block
69
+ levels.each do |level, name|
70
+ define_method name.downcase do |*arguments, &block|
71
+ add level, *arguments, &block
72
+ end
76
73
  end
77
74
  end
@@ -19,20 +19,18 @@ class ExtendedLogger
19
19
  def call
20
20
  logger = ExtendedLogger.new device
21
21
 
22
- if colors? logger.io
23
- logger.formatter = ColoredFormatter.new
24
- else
25
- logger.formatter = Formatter.new
26
- end
22
+ formatter = Formatter.new
23
+ formatter.palette = Formatter.default_palette if colors? logger.io
24
+ logger.formatter = formatter
27
25
 
28
26
  logger
29
27
  end
30
28
 
31
29
  def colors? io
32
30
  if io.tty?
33
- ENV['LOGGER_COLORS'] != 'off'
31
+ ENV['LOGGER_COLOR'] != 'off'
34
32
  else
35
- ENV['LOGGER_COLORS'] == 'on'
33
+ ENV['LOGGER_COLOR'] == 'on'
36
34
  end
37
35
  end
38
36
 
@@ -1,9 +1,21 @@
1
1
  class ExtendedLogger
2
2
  class Formatter
3
3
  attr_writer :logger_formatter
4
+ attr_writer :palette
4
5
 
5
- def call *arguments
6
- delegate *arguments
6
+ def call severity, *arguments
7
+ log_entry = delegate severity, *arguments
8
+ color log_entry, severity
9
+ end
10
+
11
+ def color log_entry, severity
12
+ colorizer = palette[severity]
13
+
14
+ if colorizer
15
+ log_entry = "#{colorizer}#{log_entry}\e[0m"
16
+ end
17
+
18
+ log_entry
7
19
  end
8
20
 
9
21
  def delegate *arguments, message
@@ -35,5 +47,43 @@ class ExtendedLogger
35
47
  def logger_formatter
36
48
  @logger_formatter ||= Logger::Formatter.new
37
49
  end
50
+
51
+ def palette
52
+ @palette ||= {}
53
+ end
54
+
55
+ def self.ansi_colors
56
+ @ansi_colors ||= %i(black red green yellow blue magenta cyan white)
57
+ end
58
+
59
+ def self.col fg, brightness, bg=nil
60
+ brightness = { :bright => 1, :normal => 0 }.fetch brightness
61
+ escape = "\e[#{brightness};3#{ansi_colors.index fg}m"
62
+
63
+ if bg
64
+ escape << "\e[4#{ansi_colors.index bg}m"
65
+ end
66
+
67
+ escape
68
+ end
69
+
70
+ def self.default_palette
71
+ @default_palette ||= {
72
+ 'OBSOLETE' => col(:black, :normal),
73
+ 'DATA' => col(:green, :normal),
74
+ 'TRACE' => col(:cyan, :normal),
75
+ 'DEBUG' => col(:blue, :normal),
76
+ 'OPT_DATA' => col(:green, :normal, :white),
77
+ 'OPT_TRACE' => col(:cyan, :normal, :white),
78
+ 'OPT_DEBUG' => col(:blue, :normal, :white),
79
+ 'PASS' => col(:white, :bright, :green),
80
+ 'FAIL' => col(:white, :bright, :red),
81
+ 'FOCUS' => col(:white, :bright, :blue),
82
+ 'WARN' => col(:yellow, :normal),
83
+ 'ERROR' => col(:red, :normal),
84
+ 'FATAL' => col(:black, :bright, :red),
85
+ 'ANY' => col(:white, :bright, :magenta),
86
+ }
87
+ end
38
88
  end
39
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extended-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Ladd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  extended-logger provides a richer logger implementation than the built in ruby
@@ -21,7 +21,6 @@ extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - lib/extended_logger.rb
24
- - lib/extended_logger/colored_formatter.rb
25
24
  - lib/extended_logger/extended_logger.rb
26
25
  - lib/extended_logger/factory.rb
27
26
  - lib/extended_logger/formatter.rb
@@ -1,51 +0,0 @@
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