tty-logger 0.1.0 → 0.6.0
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/CHANGELOG.md +62 -0
- data/README.md +300 -48
- data/lib/tty/logger.rb +187 -51
- data/lib/tty/logger/config.rb +63 -5
- data/lib/tty/logger/data_filter.rb +118 -0
- data/lib/tty/logger/event.rb +2 -2
- data/lib/tty/logger/formatters/json.rb +3 -4
- data/lib/tty/logger/formatters/text.rb +7 -6
- data/lib/tty/logger/handlers/base.rb +13 -0
- data/lib/tty/logger/handlers/console.rb +37 -14
- data/lib/tty/logger/levels.rb +34 -18
- data/lib/tty/logger/version.rb +3 -3
- metadata +15 -52
- data/Rakefile +0 -8
- data/examples/console.rb +0 -22
- data/examples/error.rb +0 -11
- data/examples/handler.rb +0 -19
- data/examples/output.rb +0 -15
- data/examples/override.rb +0 -29
- data/examples/stream.rb +0 -22
- data/spec/spec_helper.rb +0 -31
- data/spec/unit/add_handler_spec.rb +0 -25
- data/spec/unit/config_spec.rb +0 -107
- data/spec/unit/event_spec.rb +0 -22
- data/spec/unit/exception_spec.rb +0 -45
- data/spec/unit/formatter_spec.rb +0 -70
- data/spec/unit/formatters/json_spec.rb +0 -41
- data/spec/unit/formatters/text_spec.rb +0 -82
- data/spec/unit/handler_spec.rb +0 -83
- data/spec/unit/handlers/custom_spec.rb +0 -26
- data/spec/unit/handlers/null_spec.rb +0 -15
- data/spec/unit/handlers/stream_spec.rb +0 -72
- data/spec/unit/levels_spec.rb +0 -40
- data/spec/unit/log_metadata_spec.rb +0 -55
- data/spec/unit/log_spec.rb +0 -144
- data/spec/unit/output_spec.rb +0 -40
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-logger.gemspec +0 -35
data/Rakefile
DELETED
data/examples/console.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require_relative "../lib/tty/logger"
|
2
|
-
|
3
|
-
TTY::Logger.configure do |config|
|
4
|
-
config.max_bytes = 2**5
|
5
|
-
config.metadata = [:all]
|
6
|
-
config.handlers = [[:console, formatter: :text]]
|
7
|
-
config.level = :debug
|
8
|
-
end
|
9
|
-
|
10
|
-
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
11
|
-
|
12
|
-
logger.with(path: "/var/www/example.com").info("Deploying", "code")
|
13
|
-
|
14
|
-
puts "Levels:"
|
15
|
-
|
16
|
-
logger.debug("Debugging deployment")
|
17
|
-
logger.info("Info about the deploy")
|
18
|
-
logger.warn("Lack of resources")
|
19
|
-
logger.error("Failed to deploy")
|
20
|
-
logger.fatal("Terribly failed to deploy")
|
21
|
-
logger.success("Deployed successfully")
|
22
|
-
logger.wait("Ready to deploy")
|
data/examples/error.rb
DELETED
data/examples/handler.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative "../lib/tty/logger"
|
2
|
-
|
3
|
-
class MyHandler
|
4
|
-
def initialize(options = {})
|
5
|
-
@name = options[:name]
|
6
|
-
end
|
7
|
-
|
8
|
-
def call(event)
|
9
|
-
puts "(#{@name}) #{event.metadata[:name]} #{event.message}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
TTY::Logger.configure do |config|
|
14
|
-
config.handlers = [[MyHandler, {name: :hello}]]
|
15
|
-
end
|
16
|
-
|
17
|
-
logger = TTY::Logger.new
|
18
|
-
|
19
|
-
logger.info("Custom logging")
|
data/examples/output.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require_relative "../lib/tty/logger"
|
2
|
-
|
3
|
-
file = File.open("errors.log", "a")
|
4
|
-
|
5
|
-
TTY::Logger.configure do |config|
|
6
|
-
config.metadata = [:all]
|
7
|
-
config.handlers = [:stream]
|
8
|
-
config.output = file
|
9
|
-
end
|
10
|
-
|
11
|
-
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
12
|
-
|
13
|
-
logger.error("Failed to deploy")
|
14
|
-
|
15
|
-
file.close
|
data/examples/override.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require_relative "../lib/tty/logger"
|
2
|
-
|
3
|
-
logger = TTY::Logger.new
|
4
|
-
|
5
|
-
logger.success("Default success")
|
6
|
-
logger.error("Default error")
|
7
|
-
|
8
|
-
puts
|
9
|
-
|
10
|
-
new_style = TTY::Logger.new do |config|
|
11
|
-
config.handlers = [
|
12
|
-
[:console, {
|
13
|
-
styles: {
|
14
|
-
success: {
|
15
|
-
symbol: "+",
|
16
|
-
label: "Ohh yes"
|
17
|
-
},
|
18
|
-
error: {
|
19
|
-
symbol: "!",
|
20
|
-
label: "Dooh",
|
21
|
-
levelpad: 3
|
22
|
-
}
|
23
|
-
}
|
24
|
-
}]
|
25
|
-
]
|
26
|
-
end
|
27
|
-
|
28
|
-
new_style.success("Custom success")
|
29
|
-
new_style.error("Custom error")
|
data/examples/stream.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require_relative "../lib/tty/logger"
|
2
|
-
|
3
|
-
TTY::Logger.configure do |config|
|
4
|
-
config.max_bytes = 2**5
|
5
|
-
config.metadata = [:all]
|
6
|
-
config.handlers = [[:stream, formatter: :text]]
|
7
|
-
config.level = :debug
|
8
|
-
end
|
9
|
-
|
10
|
-
logger = TTY::Logger.new(fields: {app: "myapp", env: "prod"})
|
11
|
-
|
12
|
-
logger.with(path: "/var/www/example.com").info("Deploying", "code")
|
13
|
-
|
14
|
-
puts "Levels:"
|
15
|
-
|
16
|
-
logger.debug("Debugging deployment")
|
17
|
-
logger.info("Info about the deploy")
|
18
|
-
logger.warn("Lack of resources")
|
19
|
-
logger.error("Failed to deploy")
|
20
|
-
logger.fatal("Terribly failed to deploy")
|
21
|
-
logger.success("Deployed successfully")
|
22
|
-
logger.wait("Ready to deploy")
|
data/spec/spec_helper.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
if ENV['COVERAGE'] || ENV['TRAVIS']
|
4
|
-
require 'simplecov'
|
5
|
-
require 'coveralls'
|
6
|
-
|
7
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
8
|
-
SimpleCov::Formatter::HTMLFormatter,
|
9
|
-
Coveralls::SimpleCov::Formatter
|
10
|
-
])
|
11
|
-
|
12
|
-
SimpleCov.start do
|
13
|
-
command_name 'spec'
|
14
|
-
add_filter 'spec'
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
require "bundler/setup"
|
19
|
-
require "tty-logger"
|
20
|
-
|
21
|
-
RSpec.configure do |config|
|
22
|
-
# Enable flags like --only-failures and --next-failure
|
23
|
-
config.example_status_persistence_file_path = ".rspec_status"
|
24
|
-
|
25
|
-
# Disable RSpec exposing methods globally on `Module` and `main`
|
26
|
-
config.disable_monkey_patching!
|
27
|
-
|
28
|
-
config.expect_with :rspec do |c|
|
29
|
-
c.syntax = :expect
|
30
|
-
end
|
31
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "#add_handler" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "dynamically adds and removes a handler object" do
|
8
|
-
logger = TTY::Logger.new(output: output) do |config|
|
9
|
-
config.handlers = []
|
10
|
-
end
|
11
|
-
|
12
|
-
logger.info("No handler")
|
13
|
-
|
14
|
-
logger.add_handler :console
|
15
|
-
|
16
|
-
logger.info("Console handler")
|
17
|
-
|
18
|
-
logger.remove_handler :console
|
19
|
-
|
20
|
-
expect(output.string).to eq([
|
21
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
22
|
-
"\e[32minfo\e[0m ",
|
23
|
-
"Console handler \n"].join)
|
24
|
-
end
|
25
|
-
end
|
data/spec/unit/config_spec.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger::Config do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "defaults :max_bytes to 8192" do
|
8
|
-
config = described_class.new
|
9
|
-
expect(config.max_bytes).to eq(8192)
|
10
|
-
end
|
11
|
-
|
12
|
-
it "defaults :max_depth to 3" do
|
13
|
-
config = described_class.new
|
14
|
-
expect(config.max_depth).to eq(3)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "defaults :level to :info" do
|
18
|
-
config = described_class.new
|
19
|
-
expect(config.level).to eq(:info)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "sets :max_bytes" do
|
23
|
-
config = described_class.new
|
24
|
-
config.max_bytes = 2**8
|
25
|
-
expect(config.max_bytes).to eq(256)
|
26
|
-
end
|
27
|
-
|
28
|
-
it "defaults metadata to empty array" do
|
29
|
-
config = described_class.new
|
30
|
-
expect(config.metadata).to eq([])
|
31
|
-
end
|
32
|
-
|
33
|
-
it "defaults handlers to console" do
|
34
|
-
config = described_class.new
|
35
|
-
expect(config.handlers).to eq([:console])
|
36
|
-
end
|
37
|
-
|
38
|
-
it "defaults formatter to text" do
|
39
|
-
config = described_class.new
|
40
|
-
expect(config.formatter).to eq(:text)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "defaults date format to %F" do
|
44
|
-
config = described_class.new
|
45
|
-
expect(config.date_format).to eq("%F")
|
46
|
-
end
|
47
|
-
|
48
|
-
it "defaults output to stderr" do
|
49
|
-
config = described_class.new
|
50
|
-
expect(config.output).to eq($stderr)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "serializes data into hash" do
|
54
|
-
config = described_class.new
|
55
|
-
expect(config.to_h).to eq({
|
56
|
-
date_format: "%F",
|
57
|
-
formatter: :text,
|
58
|
-
handlers: [:console],
|
59
|
-
level: :info,
|
60
|
-
max_bytes: 8192,
|
61
|
-
max_depth: 3,
|
62
|
-
metadata: [],
|
63
|
-
output: $stderr,
|
64
|
-
time_format: "%T.%3N"
|
65
|
-
})
|
66
|
-
end
|
67
|
-
|
68
|
-
it "yields configuration instance" do
|
69
|
-
config = double(:config)
|
70
|
-
allow(TTY::Logger).to receive(:config).and_return(config)
|
71
|
-
expect { |block|
|
72
|
-
TTY::Logger.configure(&block)
|
73
|
-
}.to yield_with_args(config)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "configures output size" do
|
77
|
-
logger = TTY::Logger.new(output: output) do |config|
|
78
|
-
config.max_bytes = 2**4
|
79
|
-
config.level = :debug
|
80
|
-
end
|
81
|
-
|
82
|
-
logger.debug("Deploying", app: "myapp", env: "prod")
|
83
|
-
|
84
|
-
expect(output.string).to eq([
|
85
|
-
"\e[36m#{styles[:debug][:symbol]}\e[0m ",
|
86
|
-
"\e[36mdebug\e[0m ",
|
87
|
-
"Deploying ",
|
88
|
-
"\e[36mapp\e[0m=myapp ...\n"
|
89
|
-
].join)
|
90
|
-
end
|
91
|
-
|
92
|
-
it "configures maximum depth of structured data" do
|
93
|
-
logger = TTY::Logger.new(output: output) do |config|
|
94
|
-
config.max_depth = 1
|
95
|
-
config.level = :debug
|
96
|
-
end
|
97
|
-
|
98
|
-
logger.debug("Deploying", app: "myapp", env: { name: "prod" })
|
99
|
-
|
100
|
-
expect(output.string).to eq([
|
101
|
-
"\e[36m#{styles[:debug][:symbol]}\e[0m ",
|
102
|
-
"\e[36mdebug\e[0m ",
|
103
|
-
"Deploying ",
|
104
|
-
"\e[36mapp\e[0m=myapp \e[36menv\e[0m={...}\n"
|
105
|
-
].join)
|
106
|
-
end
|
107
|
-
end
|
data/spec/unit/event_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger::Event, "event" do
|
4
|
-
it "defaults backtrace to an empty array" do
|
5
|
-
event = described_class.new(["message"], {}, {})
|
6
|
-
expect(event.backtrace).to eq([])
|
7
|
-
end
|
8
|
-
|
9
|
-
it "extracts backtrace if message contains exception" do
|
10
|
-
event = nil
|
11
|
-
error = nil
|
12
|
-
|
13
|
-
begin
|
14
|
-
raise ArgumentError, "Wrong data"
|
15
|
-
rescue => ex
|
16
|
-
error = ex
|
17
|
-
event = described_class.new(["Error", ex], {}, {})
|
18
|
-
end
|
19
|
-
|
20
|
-
expect(event.backtrace.join).to eq(error.backtrace.join)
|
21
|
-
end
|
22
|
-
end
|
data/spec/unit/exception_spec.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "exception logging" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "handles exception type message when console" do
|
8
|
-
logger = TTY::Logger.new(output: output)
|
9
|
-
error = nil
|
10
|
-
|
11
|
-
begin
|
12
|
-
raise ArgumentError, "Wrong data"
|
13
|
-
rescue => ex
|
14
|
-
error = ex
|
15
|
-
logger.fatal("Error:", error)
|
16
|
-
end
|
17
|
-
|
18
|
-
expect(output.string).to eq([
|
19
|
-
"\e[31m#{styles[:fatal][:symbol]}\e[0m ",
|
20
|
-
"\e[31mfatal\e[0m ",
|
21
|
-
"Error: Wrong data \n",
|
22
|
-
"#{error.backtrace.map {|bktrace| bktrace.to_s.insert(0, " " * 4) }.join("\n")}\n"
|
23
|
-
].join)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "handles exception type message when stream" do
|
27
|
-
logger = TTY::Logger.new(output: output) do |config|
|
28
|
-
config.handlers = [:stream]
|
29
|
-
end
|
30
|
-
|
31
|
-
error = nil
|
32
|
-
|
33
|
-
begin
|
34
|
-
raise ArgumentError, "Wrong data"
|
35
|
-
rescue => ex
|
36
|
-
error = ex
|
37
|
-
logger.fatal("Error:", error)
|
38
|
-
end
|
39
|
-
|
40
|
-
expect(output.string).to eq([
|
41
|
-
"level=fatal message=\"Error: Wrong data\" backtrace=\"",
|
42
|
-
"#{error.backtrace.map {|bktrace| bktrace }.join(",")}\"\n"
|
43
|
-
].join)
|
44
|
-
end
|
45
|
-
end
|
data/spec/unit/formatter_spec.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "formatter" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "changes default formatter to JSON as class name" do
|
8
|
-
logger = TTY::Logger.new(output: output) do |config|
|
9
|
-
config.formatter = TTY::Logger::Formatters::JSON
|
10
|
-
end
|
11
|
-
|
12
|
-
logger.info("Logging", app: "myapp", env: "prod")
|
13
|
-
|
14
|
-
expect(output.string).to eq([
|
15
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
16
|
-
"\e[32minfo\e[0m ",
|
17
|
-
"Logging ",
|
18
|
-
"{\"\e[32mapp\e[0m\":\"myapp\",\"\e[32menv\e[0m\":\"prod\"}\n"].join)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "changes default formatter to JSON as name" do
|
22
|
-
logger = TTY::Logger.new(output: output) do |config|
|
23
|
-
config.formatter = :json
|
24
|
-
end
|
25
|
-
|
26
|
-
logger.info("Logging", app: "myapp", env: "prod")
|
27
|
-
|
28
|
-
expect(output.string).to eq([
|
29
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
30
|
-
"\e[32minfo\e[0m ",
|
31
|
-
"Logging ",
|
32
|
-
"{\"\e[32mapp\e[0m\":\"myapp\",\"\e[32menv\e[0m\":\"prod\"}\n"].join)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "changes default formatter for only one handler" do
|
36
|
-
logger = TTY::Logger.new(output: output) do |config|
|
37
|
-
config.handlers = [:console,
|
38
|
-
[:console, {formatter: :JSON}]]
|
39
|
-
end
|
40
|
-
|
41
|
-
logger.info("Logging", app: "myapp", env: "prod")
|
42
|
-
|
43
|
-
expect(output.string).to eq([
|
44
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
45
|
-
"\e[32minfo\e[0m ",
|
46
|
-
"Logging ",
|
47
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n",
|
48
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
49
|
-
"\e[32minfo\e[0m ",
|
50
|
-
"Logging ",
|
51
|
-
"{\"\e[32mapp\e[0m\":\"myapp\",\"\e[32menv\e[0m\":\"prod\"}\n"].join)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "fails to recognize formatter object type" do
|
55
|
-
expect {
|
56
|
-
TTY::Logger.new(output: output) do |config|
|
57
|
-
config.formatter = true
|
58
|
-
end
|
59
|
-
}.to raise_error(TTY::Logger::Error, "Unrecognized formatter name 'true'")
|
60
|
-
end
|
61
|
-
|
62
|
-
it "fails to recognize formatter name" do
|
63
|
-
|
64
|
-
expect {
|
65
|
-
TTY::Logger.new(output: output) do |config|
|
66
|
-
config.formatter = :unknown
|
67
|
-
end
|
68
|
-
}.to raise_error(TTY::Logger::Error, "Unrecognized formatter name ':unknown'")
|
69
|
-
end
|
70
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger::Formatters::JSON, "#dump" do
|
4
|
-
it "dumps a log line" do
|
5
|
-
formatter = described_class.new
|
6
|
-
data = {
|
7
|
-
app: "myapp",
|
8
|
-
env: "prod",
|
9
|
-
sql: "SELECT * FROM admins",
|
10
|
-
at: Time.at(123456).utc
|
11
|
-
}
|
12
|
-
|
13
|
-
expect(formatter.dump(data)).to eq("{\"app\":\"myapp\",\"env\":\"prod\",\"sql\":\"SELECT * FROM admins\",\"at\":\"1970-01-02 10:17:36 UTC\"}")
|
14
|
-
end
|
15
|
-
|
16
|
-
[
|
17
|
-
{obj: {a: "aaaaa", b: "bbbbb", c: "ccccc"}, bytes: 3*12+2, want: "{\"a\":\"aaaaa\",\"b\":\"bbbbb\",\"c\":\"ccccc\"}"},
|
18
|
-
{obj: {a: "aaaaa", b: "bbbbb", c: "ccccc"}, bytes: 2*12+4, want: "{\"a\":\"aaaaa\",\"b\":\"bbbbb\",\"c\":\"...\"}"},
|
19
|
-
{obj: {a: "aaaaa", b: "bbbbb", c: "ccccc"}, bytes: 12+4, want: "{\"a\":\"aaaaa\",\"b\":\"...\"}"},
|
20
|
-
{obj: {a: "aaaaa", b: "bbbbb", c: "ccccc"}, bytes: 11, want: "{\"a\":\"...\"}"},
|
21
|
-
].each do |data|
|
22
|
-
it "truncates #{data[:obj].inspect} to #{data[:want].inspect} of #{data[:bytes]} bytes" do
|
23
|
-
formatter = described_class.new
|
24
|
-
expect(formatter.dump(data[:obj], max_bytes: data[:bytes])).to eq(data[:want])
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
[
|
29
|
-
{obj: {a: {b: {c: "ccccc"}}}, depth: 1, want: "{\"a\":\"...\"}"},
|
30
|
-
{obj: {a: {b: {c: "ccccc"}}}, depth: 2, want: "{\"a\":{\"b\":\"...\"}}"},
|
31
|
-
{obj: {a: {b: {c: "ccccc"}}}, depth: 3, want: "{\"a\":{\"b\":{\"c\":\"ccccc\"}}}"},
|
32
|
-
{obj: {a: ["b", {c: "ccccc"}]}, depth: 1, want: "{\"a\":\"...\"}"},
|
33
|
-
{obj: {a: ["b", {c: "ccccc"}]}, depth: 2, want: "{\"a\":[\"b\",\"...\"]}"},
|
34
|
-
{obj: {a: ["b", {c: "ccccc"}]}, depth: 3, want: "{\"a\":[\"b\",{\"c\":\"ccccc\"}]}"},
|
35
|
-
].each do |data|
|
36
|
-
it "truncates nested object #{data[:obj].inspect} to #{data[:want].inspect}" do
|
37
|
-
formatter = described_class.new
|
38
|
-
expect(formatter.dump(data[:obj], max_depth: data[:depth])).to eq(data[:want])
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|