slog 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +52 -0
- data/Gemfile +17 -0
- data/LICENSE +13 -0
- data/README.md +38 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/lib/slog/logger.rb +105 -0
- data/lib/slog/metadata.rb +25 -0
- data/lib/slog.rb +7 -0
- data/slog.gemspec +21 -0
- data/test/test_helper.rb +7 -0
- data/test/test_slog.rb +61 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f6281566b29155ca25d47ffda05399f95087c368
|
4
|
+
data.tar.gz: c10525a3d80f1a8d8f77aef31f84328472e1d505
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cbfa8a4d518fe83543b90cd8260b7be6d633630f5be17fb2ac9c3bd1686cd5c5bbee917d4204b16ed1f76e142f44f7991bf3bf4af867546af6b31c6a5f07e4f
|
7
|
+
data.tar.gz: ef037550aebd01bffd8a814879031da6007b18046793a5493ea6dc28e92c82399bedf076ba671e67d9e366e56828e2cb80a4f6c66285fb3e269fbf4c68d04f5e
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'vendor/**/*'
|
4
|
+
- 'spec/fixtures/**/*'
|
5
|
+
|
6
|
+
Style/Encoding:
|
7
|
+
EnforcedStyle: when_needed
|
8
|
+
|
9
|
+
Style/EmptyLines:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/EmptyLineBetweenDefs:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Style/EmptyLinesAroundBlockBody:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/EmptyLinesAroundClassBody:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/EmptyLinesAroundModuleBody:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/TrailingBlankLines:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/PercentLiteralDelimiters:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/FormatString:
|
31
|
+
EnforcedStyle: percent
|
32
|
+
|
33
|
+
Style/SingleLineMethods:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/AlignParameters:
|
37
|
+
EnforcedStyle: with_fixed_indentation
|
38
|
+
|
39
|
+
Style/MethodDefParentheses:
|
40
|
+
EnforcedStyle: require_no_parentheses
|
41
|
+
|
42
|
+
Style/AccessModifierIndentation:
|
43
|
+
EnforcedStyle: outdent
|
44
|
+
|
45
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
46
|
+
EnforcedStyle: no_space
|
47
|
+
|
48
|
+
Style/SpaceInsideBrackets:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/SpaceBeforeSemicolon:
|
52
|
+
Enabled: false
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2014 Sean Clemmer
|
2
|
+
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
purpose with or without fee is hereby granted, provided that the above
|
5
|
+
copyright notice and this permission notice appear in all copies.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Slog
|
2
|
+
|
3
|
+
Simple, colorful, JSON-based logging.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `slog` to your application's Gemspec or Gemfile.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
`Slog::Logger` extends the base `Logger` with JSON serialization, colorization,
|
12
|
+
a hashified initializer, and a new `TRACE` level:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require 'slog'
|
16
|
+
|
17
|
+
# First, these two are equivalent
|
18
|
+
logger_one = Slog::Logger.new
|
19
|
+
logger_two = Slog.new
|
20
|
+
|
21
|
+
# Redirect your logs to STDERR
|
22
|
+
Slog.new out: $stderr
|
23
|
+
|
24
|
+
# Set the level with a symbol or Logger constant
|
25
|
+
Slog.new level: :trace
|
26
|
+
|
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
|
+
|
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
|
38
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
|
6
|
+
require 'rake/testtask'
|
7
|
+
Rake::TestTask.new(:test) do |test|
|
8
|
+
test.libs << 'lib' << 'test'
|
9
|
+
test.test_files = FileList['test/test*.rb']
|
10
|
+
test.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: :test
|
14
|
+
|
15
|
+
|
16
|
+
require 'yard'
|
17
|
+
YARD::Rake::YardocTask.new do |t|
|
18
|
+
t.files = %w[ --readme Readme.md lib/**/*.rb - VERSION ]
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
require 'rubygems/tasks'
|
23
|
+
Gem::Tasks.new push: true, sign: {} do |tasks|
|
24
|
+
tasks.console.command = 'pry'
|
25
|
+
end
|
26
|
+
Gem::Tasks::Sign::Checksum.new sha2: true
|
27
|
+
|
28
|
+
|
29
|
+
require 'rake/version_task'
|
30
|
+
Rake::VersionTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/slog/logger.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'time'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'colorize'
|
6
|
+
|
7
|
+
|
8
|
+
# Extending the base Logger class with TRACE capabilities.
|
9
|
+
class Logger
|
10
|
+
|
11
|
+
# Adding the new severity level
|
12
|
+
SEV_LABEL << 'TRACE'
|
13
|
+
|
14
|
+
# So we can reference it by name
|
15
|
+
TRACE = SEV_LABEL.index('TRACE')
|
16
|
+
|
17
|
+
# Send a TRACE-level log line
|
18
|
+
def trace progname, &block
|
19
|
+
return true unless @trace
|
20
|
+
add TRACE, nil, progname, &block
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
module Slog
|
28
|
+
|
29
|
+
# A colorful JSON logger.
|
30
|
+
class Logger < Logger
|
31
|
+
|
32
|
+
DEFAULT_LEVEL = ::Logger::INFO
|
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
|
+
# Create a new Logger.
|
46
|
+
#
|
47
|
+
# 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
|
51
|
+
super out, shift_age, shift_size
|
52
|
+
self.level = level
|
53
|
+
set_formatter
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# Set the formatter to work our magic
|
58
|
+
def set_formatter
|
59
|
+
self.formatter = proc do |severity, datetime, _, message|
|
60
|
+
severity.downcase! # It's nicer this way
|
61
|
+
|
62
|
+
# If it ain't a structured log, it is now.
|
63
|
+
event = structure_event severity, datetime, message
|
64
|
+
|
65
|
+
# When debugging, mark the location in Hoss's source
|
66
|
+
unless level == DEFAULT_LEVEL
|
67
|
+
event.merge! marker: File.basename(caller[4])
|
68
|
+
end
|
69
|
+
|
70
|
+
# Pretty JSON on STDOUT, line-oriented otherwise
|
71
|
+
format_json event, severity
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# Turn a call to the formatter into a Hash structure
|
77
|
+
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
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Convert the structured event into it's JSON representation
|
86
|
+
def format_json event, severity
|
87
|
+
generator = @pretty ? :pretty_generate : :generate
|
88
|
+
event = JSON.send(generator, event) + "\n"
|
89
|
+
return event unless @colorize
|
90
|
+
event.colorize \
|
91
|
+
color: SEVERITY_COLORS[severity][0],
|
92
|
+
background: SEVERITY_COLORS[severity][1]
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
# Override the level setter to allow symbols and TRACE
|
97
|
+
def level= l
|
98
|
+
l = ::Logger.const_get l.upcase if l.is_a? Symbol
|
99
|
+
@trace = l == ::Logger::TRACE
|
100
|
+
l = ::Logger::DEBUG if @trace
|
101
|
+
super l
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Everything you wanted to know about the big Slog.
|
2
|
+
module Slog
|
3
|
+
|
4
|
+
# Point to actual gem location
|
5
|
+
ROOT = File.join File.dirname(__FILE__), '..', '..'
|
6
|
+
|
7
|
+
# We use a VERSION file to tie into our build pipeline
|
8
|
+
VERSION = File.read(File.join(ROOT, 'VERSION')).strip
|
9
|
+
|
10
|
+
# We don't really do all that much, be humble
|
11
|
+
SUMMARY = 'Simple, colorful, JSON-based logging'
|
12
|
+
|
13
|
+
# Your benevolent dictator for life
|
14
|
+
AUTHOR = 'Sean Clemmer'
|
15
|
+
|
16
|
+
# Turn here to strangle your dictator
|
17
|
+
EMAIL = 'sczizzo@gmail.com'
|
18
|
+
|
19
|
+
# Like the MIT license, but even simpler
|
20
|
+
LICENSE = 'ISC'
|
21
|
+
|
22
|
+
# If you really just can't get enough
|
23
|
+
HOMEPAGE = 'https://github.com/sczizzo/slog'
|
24
|
+
|
25
|
+
end
|
data/lib/slog.rb
ADDED
data/slog.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$LOAD_PATH.push File.expand_path(File.join('..', 'lib'), __FILE__)
|
3
|
+
require 'slog/metadata'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'slog'
|
7
|
+
s.version = Slog::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.author = Slog::AUTHOR
|
10
|
+
s.email = Slog::EMAIL
|
11
|
+
s.license = Slog::LICENSE
|
12
|
+
s.homepage = Slog::HOMEPAGE
|
13
|
+
s.summary = Slog::SUMMARY
|
14
|
+
s.description = Slog::SUMMARY + '.'
|
15
|
+
|
16
|
+
s.add_runtime_dependency 'colorize', '~> 0'
|
17
|
+
|
18
|
+
s.require_paths = %w[ lib ]
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/test_slog.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
require 'thread'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'tempfile'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'pathname'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
|
10
|
+
require 'slog'
|
11
|
+
|
12
|
+
|
13
|
+
Thread.abort_on_exception = true
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
# Mostly for coverage, testing the utility functions.
|
18
|
+
class TestSlog < MiniTest::Test
|
19
|
+
|
20
|
+
attr_reader :tmpdir, :logger
|
21
|
+
|
22
|
+
|
23
|
+
def setup
|
24
|
+
@tmpdir = Dir.mktmpdir
|
25
|
+
@logger = Slog::Logger.new colorize: false, out: $stderr
|
26
|
+
@logger.level = ::Logger::WARN
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def teardown
|
31
|
+
FileUtils.rm_rf tmpdir
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def test_slog_logger_structure_event now=DateTime.now
|
36
|
+
expected_event = {
|
37
|
+
'level' => 'info',
|
38
|
+
'@timestamp' => now.iso8601(3)
|
39
|
+
}
|
40
|
+
assert expected_event, logger.structure_event('info', now, {})
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def test_slog_logger_format_json
|
45
|
+
assert "{}\n", logger.format_json({}, 'info')
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def test_slog_logger_formatter now=DateTime.now
|
50
|
+
event = {
|
51
|
+
'level' => 'info',
|
52
|
+
'@timestamp' => now.iso8601(3)
|
53
|
+
}
|
54
|
+
expected_output = JSON.pretty_generate(event) + "\n"
|
55
|
+
assert expected_output, logger.formatter.call('INFO', now, nil, {})
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_slog_logger
|
59
|
+
assert logger.info({})
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Clemmer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Simple, colorful, JSON-based logging.
|
28
|
+
email: sczizzo@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rubocop.yml"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- VERSION
|
40
|
+
- lib/slog.rb
|
41
|
+
- lib/slog/logger.rb
|
42
|
+
- lib/slog/metadata.rb
|
43
|
+
- slog.gemspec
|
44
|
+
- test/test_helper.rb
|
45
|
+
- test/test_slog.rb
|
46
|
+
homepage: https://github.com/sczizzo/slog
|
47
|
+
licenses:
|
48
|
+
- ISC
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.2.2
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Simple, colorful, JSON-based logging
|
70
|
+
test_files:
|
71
|
+
- test/test_helper.rb
|
72
|
+
- test/test_slog.rb
|