slog 1.0.0 → 1.1.0

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: f6281566b29155ca25d47ffda05399f95087c368
4
- data.tar.gz: c10525a3d80f1a8d8f77aef31f84328472e1d505
3
+ metadata.gz: 5d3de78de50b7caaa19ac71dd29c9a7b5acfc3a5
4
+ data.tar.gz: 0a2ba828d3563e997df44394cee048b1d9ef6638
5
5
  SHA512:
6
- metadata.gz: 7cbfa8a4d518fe83543b90cd8260b7be6d633630f5be17fb2ac9c3bd1686cd5c5bbee917d4204b16ed1f76e142f44f7991bf3bf4af867546af6b31c6a5f07e4f
7
- data.tar.gz: ef037550aebd01bffd8a814879031da6007b18046793a5493ea6dc28e92c82399bedf076ba671e67d9e366e56828e2cb80a4f6c66285fb3e269fbf4c68d04f5e
6
+ metadata.gz: fda260806e74c4109edd77f0bdebf6b9e236427e0387300fd83eba5828ce396eb2d58340451b42497d70a037769612fc7b02ceb1ab43faed098c2e4d28b46d06
7
+ data.tar.gz: 76ddc56ef3243f5e3a800f1f47a167a65af6d456f491cceae4e7705fffad2668394a96cbf5da3a2a6fb7e69e9e9e340deb5514ee4943e2a62dd3a0b8ceff2415
data/README.md CHANGED
@@ -1,38 +1,85 @@
1
1
  # Slog
2
2
 
3
- Simple, colorful, JSON-based logging.
4
-
5
- ## Installation
6
-
7
- Add `slog` to your application's Gemspec or Gemfile.
8
-
9
- ## Usage
3
+ Easy structured logging.
10
4
 
11
5
  `Slog::Logger` extends the base `Logger` with JSON serialization, colorization,
12
- a hashified initializer, and a new `TRACE` level:
6
+ and a new `TRACE` level. Output is suitable for collection by Logstash or Franz.
13
7
 
14
8
  ```ruby
15
9
  require 'slog'
16
10
 
17
- # First, these two are equivalent
18
- logger_one = Slog::Logger.new
19
- logger_two = Slog.new
20
11
 
21
- # Redirect your logs to STDERR
22
- Slog.new out: $stderr
12
+ # Full initializer
13
+ Slog::Logger.new \
14
+ out: $stdout, # Set the log output handle
15
+ shift_age: 7, # Number of log files to keep
16
+ shift_size: 1_048_576, # Maximum size of log files (in bytes)
17
+ colorize: true, # Toggle colorized output
18
+ prettify: true, # Toggle pretty JSON output
19
+ level: :info, # Set log level (Logger constant or Symbol)
20
+ level_transform: 'downcase', # Transform the log level (String methods)
21
+ level_field: 'level', # Set level field name (nil to disable)
22
+ message_field: 'message', # Set message field name (nil to disable)
23
+ timestamp_field: '@timestamp', # Set timestamp field name (nil to disable)
24
+ timestamp_format: '%FT%T.%3N%:z', # Set timestamp format (Time#strftime)
25
+ # Map each log level to fore- and background colors
26
+ # Colors provided by the fantastic "colorize" gem
27
+ color_map: { 'debug' => [ :blue, :default ],
28
+ 'info' => [ :green, :default ],
29
+ 'warn' => [ :yellow, :default ],
30
+ 'error' => [ :red, :default ],
31
+ 'fatal' => [ :red, :black ],
32
+ 'trace' => [ :magenta, :default ] }
33
+
34
+
35
+ # These two are equivalent
36
+ Slog::Logger.new
37
+ Slog.new
38
+
39
+
40
+ # By default, output is pretty and colored (I swear)
41
+ Slog.new.info 'example'
42
+ #
43
+ # {
44
+ # "level": "info",
45
+ # "@timestamp": "2014-12-25T06:22:43.459-08:00",
46
+ # "message": "example"
47
+ # }
48
+ #
49
+
50
+
51
+ # When logging to file, you prolly want to disable that
52
+ Slog.new(out: 'test.log', colorize: false, prettify: false).info 'example'
53
+ #
54
+ # {"level":"info","@timestamp":"2014-12-25T06:25:54.793-08:00","message":"example"}
55
+ #
56
+
23
57
 
24
- # Set the level with a symbol or Logger constant
25
- Slog.new level: :trace
58
+ # You'll no doubt have noticed you passed a String into the Logger, but a Hash
59
+ # came out the other end. That's "structured" logging. The String turned into
60
+ # the "message" field, and "level" and "@timestamp" fields were added. All
61
+ # that's totally configurable:
62
+ Slog.new({
63
+ message_field: nil,
64
+ timestamp_field: 'date',
65
+ timestamp_format: '%Y.%m.%d'
66
+ }).info 'example'
67
+ #
68
+ # {
69
+ # "level": "info",
70
+ # "date": "2014.12.25"
71
+ # }
72
+ #
26
73
 
27
- # When logging to file, you may want to disable colors and pretty output
28
- Slog.new out: 'test.log', colorization: false, pretty: false
29
74
 
30
- # Finally, all the options and their defaults
31
- Slog.new \
32
- out: $stdout, # Set the log output handle
33
- shift_age: 7, # Number of log files to keey
34
- shift_size: 1_048_576, # Maximum size of log files
35
- colorize: true, # Toggle colorization of logs
36
- pretty: true, # Toggle pretty JSON output
37
- level: :info # Set the initial level
75
+ # You can write structured logs by passing a Hash
76
+ Slog.new({
77
+ level_field: nil,
78
+ }).info event: 'example', level: 'MINE', '@timestamp' => nil
79
+ #
80
+ # {
81
+ # "event": "example",
82
+ # "level": "MINE"
83
+ # }
84
+ #
38
85
  ```
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ task default: :test
15
15
 
16
16
  require 'yard'
17
17
  YARD::Rake::YardocTask.new do |t|
18
- t.files = %w[ --readme Readme.md lib/**/*.rb - VERSION ]
18
+ t.files = %w[ --readme Readme.md lib/**/*.rb - VERSION LICENSE ]
19
19
  end
20
20
 
21
21
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/slog/logger.rb CHANGED
@@ -5,7 +5,7 @@ require 'json'
5
5
  require 'colorize'
6
6
 
7
7
 
8
- # Extending the base Logger class with TRACE capabilities.
8
+ # Extending the base Logger with TRACE capabilities.
9
9
  class Logger
10
10
 
11
11
  # Adding the new severity level
@@ -14,7 +14,7 @@ class Logger
14
14
  # So we can reference it by name
15
15
  TRACE = SEV_LABEL.index('TRACE')
16
16
 
17
- # Send a TRACE-level log line
17
+ # Send a TRACE-level log
18
18
  def trace progname, &block
19
19
  return true unless @trace
20
20
  add TRACE, nil, progname, &block
@@ -26,30 +26,48 @@ end
26
26
 
27
27
  module Slog
28
28
 
29
- # A colorful JSON logger.
29
+ # A colorful structured logger.
30
30
  class Logger < Logger
31
31
 
32
32
  DEFAULT_LEVEL = ::Logger::INFO
33
33
 
34
- # Maps each log level to a unique combination of fore- and background colors
35
- SEVERITY_COLORS = {
36
- 'debug' => [ :blue, :default ],
37
- 'info' => [ :green, :default ],
38
- 'warn' => [ :yellow, :default ],
39
- 'error' => [ :red, :default ],
40
- 'fatal' => [ :red, :black ],
41
- 'trace' => [ :magenta, :default ]
42
- }
43
-
44
-
45
34
  # Create a new Logger.
46
35
  #
47
36
  # A little different than the canonical Logger. Add options to set the
48
- # log level and todisable both colorization and pretty JSON output.
49
- def initialize out:$stdout, shift_age:7, shift_size:1048576, colorize:true, pretty:true, level:DEFAULT_LEVEL
50
- @colorize, @pretty = colorize, pretty
37
+ # log level and to disable both colorization and pretty JSON output. In
38
+ # this form, it's perfect for use with either Logstash or Franz.
39
+ def initialize \
40
+ out:$stdout, # Output handle
41
+ shift_age:7, # Max seven logs
42
+ shift_size:1_048_576, # 1024 KiB
43
+ colorize:true, # Disable for files
44
+ prettify:true, # Disable for files
45
+ level:DEFAULT_LEVEL, # INFO
46
+ level_transform:'downcase', # Any String method
47
+ level_field:'level', # nil to disable
48
+ message_field: 'message', # nil to disable
49
+ timestamp_field:'@timestamp', # nil to disable
50
+ timestamp_format:'%FT%T.%3N%:z', # ISO-8601
51
+ color_map: {
52
+ 'debug' => [ :blue, :default ],
53
+ 'info' => [ :green, :default ],
54
+ 'warn' => [ :yellow, :default ],
55
+ 'error' => [ :red, :default ],
56
+ 'fatal' => [ :red, :black ],
57
+ 'trace' => [ :magenta, :default ]
58
+ }
59
+
60
+ @prettify = prettify
61
+ @colorize = colorize
62
+ @color_map = color_map
63
+ @level_field = level_field
64
+ @level_transform = level_transform
65
+ @timestamp_field = timestamp_field
66
+ @timestamp_format = timestamp_format
67
+
51
68
  super out, shift_age, shift_size
52
69
  self.level = level
70
+
53
71
  set_formatter
54
72
  end
55
73
 
@@ -57,7 +75,7 @@ module Slog
57
75
  # Set the formatter to work our magic
58
76
  def set_formatter
59
77
  self.formatter = proc do |severity, datetime, _, message|
60
- severity.downcase! # It's nicer this way
78
+ severity.downcase!
61
79
 
62
80
  # If it ain't a structured log, it is now.
63
81
  event = structure_event severity, datetime, message
@@ -67,7 +85,7 @@ module Slog
67
85
  event.merge! marker: File.basename(caller[4])
68
86
  end
69
87
 
70
- # Pretty JSON on STDOUT, line-oriented otherwise
88
+ # Apply colorization and prettification as required
71
89
  format_json event, severity
72
90
  end
73
91
  end
@@ -75,29 +93,33 @@ module Slog
75
93
 
76
94
  # Turn a call to the formatter into a Hash structure
77
95
  def structure_event severity, datetime, message
78
- message = { message: message } unless message.is_a? Hash
79
- { 'level' => severity,
80
- '@timestamp' => datetime.iso8601(3) # Logstash/Franz format
81
- }.merge message
96
+ message = { @message_field => message } unless message.is_a? Hash
97
+ event = {
98
+ @level_field => severity.send(@level_transform),
99
+ @timestamp_field => datetime.strftime(@timestamp_format)
100
+ }
101
+ event.merge message
102
+ event.delete nil # ignore @field if @field.nil?
103
+ event
82
104
  end
83
105
 
84
106
 
85
107
  # Convert the structured event into it's JSON representation
86
108
  def format_json event, severity
87
- generator = @pretty ? :pretty_generate : :generate
109
+ generator = @prettify ? :pretty_generate : :generate
88
110
  event = JSON.send(generator, event) + "\n"
89
111
  return event unless @colorize
90
112
  event.colorize \
91
- color: SEVERITY_COLORS[severity][0],
92
- background: SEVERITY_COLORS[severity][1]
113
+ color: @color_map[severity][0],
114
+ background: @color_map[severity][1]
93
115
  end
94
116
 
95
117
 
96
118
  # Override the level setter to allow symbols and TRACE
97
119
  def level= l
98
120
  l = ::Logger.const_get l.upcase if l.is_a? Symbol
99
- @trace = l == ::Logger::TRACE
100
- l = ::Logger::DEBUG if @trace
121
+ @trace = l == ::Logger::TRACE # TRACE is really a fancy DEBUG
122
+ l = ::Logger::DEBUG if @trace # Here's the proof.
101
123
  super l
102
124
  end
103
125
 
data/lib/slog/metadata.rb CHANGED
@@ -8,7 +8,7 @@ module Slog
8
8
  VERSION = File.read(File.join(ROOT, 'VERSION')).strip
9
9
 
10
10
  # We don't really do all that much, be humble
11
- SUMMARY = 'Simple, colorful, JSON-based logging'
11
+ SUMMARY = 'Easy structured logging'
12
12
 
13
13
  # Your benevolent dictator for life
14
14
  AUTHOR = 'Sean Clemmer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Clemmer
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Simple, colorful, JSON-based logging.
27
+ description: Easy structured logging.
28
28
  email: sczizzo@gmail.com
29
29
  executables: []
30
30
  extensions: []
@@ -66,7 +66,7 @@ rubyforge_project:
66
66
  rubygems_version: 2.2.2
67
67
  signing_key:
68
68
  specification_version: 4
69
- summary: Simple, colorful, JSON-based logging
69
+ summary: Easy structured logging
70
70
  test_files:
71
71
  - test/test_helper.rb
72
72
  - test/test_slog.rb