tty-logger 0.2.0 → 0.3.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 +12 -0
- data/README.md +69 -8
- data/lib/tty/logger.rb +14 -10
- data/lib/tty/logger/config.rb +34 -3
- data/lib/tty/logger/data_filter.rb +121 -0
- data/lib/tty/logger/handlers/console.rb +13 -3
- data/lib/tty/logger/version.rb +1 -1
- data/tty-logger.gemspec +4 -6
- metadata +4 -38
- data/Rakefile +0 -8
- data/examples/child.rb +0 -9
- data/examples/console.rb +0 -22
- data/examples/custom_type.rb +0 -27
- data/examples/error.rb +0 -11
- data/examples/handler.rb +0 -19
- data/examples/log.rb +0 -9
- data/examples/output.rb +0 -15
- data/examples/override.rb +0 -29
- data/examples/stream.rb +0 -22
- data/spec/perf/json_formatter_spec.rb +0 -29
- data/spec/perf/log_spec.rb +0 -21
- data/spec/perf/text_formatter_spec.rb +0 -29
- data/spec/spec_helper.rb +0 -31
- data/spec/unit/add_handler_spec.rb +0 -25
- data/spec/unit/config_spec.rb +0 -109
- data/spec/unit/copy_spec.rb +0 -27
- data/spec/unit/event_spec.rb +0 -22
- data/spec/unit/exception_spec.rb +0 -45
- data/spec/unit/filter_spec.rb +0 -32
- 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_at_spec.rb +0 -34
- data/spec/unit/log_metadata_spec.rb +0 -55
- data/spec/unit/log_spec.rb +0 -203
- 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 -34
@@ -1,72 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "#log" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
|
6
|
-
it "logs message with fields in text format" do
|
7
|
-
logger = TTY::Logger.new(output: output) do |config|
|
8
|
-
config.handlers = [:stream]
|
9
|
-
end
|
10
|
-
|
11
|
-
logger.info("Successfully deployed", app: 'myapp', env: 'prod')
|
12
|
-
|
13
|
-
expect(output.string).to eq([
|
14
|
-
"level=info message=\"Successfully deployed\" ",
|
15
|
-
"app=myapp env=prod\n"
|
16
|
-
].join)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "logs message with all metadata in text format" do
|
20
|
-
time_now = Time.new(2019, 7, 21, 14, 04, 35, "+02:00")
|
21
|
-
allow(Time).to receive(:now).and_return(time_now)
|
22
|
-
|
23
|
-
logger = TTY::Logger.new(output: output) do |config|
|
24
|
-
config.handlers = [:stream]
|
25
|
-
config.metadata = [:all]
|
26
|
-
end
|
27
|
-
|
28
|
-
logger.info("Successfully deployed", app: 'myapp', env: 'prod')
|
29
|
-
|
30
|
-
expect(output.string).to eq([
|
31
|
-
"pid=#{Process.pid} ",
|
32
|
-
"date=\"2019-07-21\" ",
|
33
|
-
"time=\"14:04:35.000\" ",
|
34
|
-
"path=\"#{__FILE__}:#{__LINE__ - 6}:in`<top (required)>`\" ",
|
35
|
-
"level=info message=\"Successfully deployed\" ",
|
36
|
-
"app=myapp env=prod\n"
|
37
|
-
].join)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "logs message with fields in JSON format" do
|
41
|
-
logger = TTY::Logger.new(output: output) do |config|
|
42
|
-
config.handlers = [[:stream, formatter: :json]]
|
43
|
-
end
|
44
|
-
|
45
|
-
logger.info("Successfully deployed", app: 'myapp', env: 'prod')
|
46
|
-
|
47
|
-
expect(output.string).to eq([
|
48
|
-
"{\"level\":\"info\",\"message\":\"Successfully deployed\",",
|
49
|
-
"\"app\":\"myapp\",\"env\":\"prod\"}\n"
|
50
|
-
].join)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "logs message with all metadata in json format" do
|
54
|
-
time_now = Time.new(2019, 7, 21, 14, 04, 35, "+02:00")
|
55
|
-
allow(Time).to receive(:now).and_return(time_now)
|
56
|
-
|
57
|
-
logger = TTY::Logger.new(output: output) do |config|
|
58
|
-
config.handlers = [[:stream, formatter: :json]]
|
59
|
-
config.metadata = [:all]
|
60
|
-
end
|
61
|
-
|
62
|
-
logger.info("Successfully deployed")
|
63
|
-
|
64
|
-
expect(output.string).to eq([
|
65
|
-
"{\"pid\":#{Process.pid},",
|
66
|
-
"\"date\":\"2019-07-21\",",
|
67
|
-
"\"time\":\"14:04:35.000\",",
|
68
|
-
"\"path\":\"#{__FILE__}:#{__LINE__ - 6}:in`<top (required)>`\",",
|
69
|
-
"\"level\":\"info\",\"message\":\"Successfully deployed\"}\n"
|
70
|
-
].join)
|
71
|
-
end
|
72
|
-
end
|
data/spec/unit/levels_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, 'levels' do
|
4
|
-
it "fails when unknown level" do
|
5
|
-
logger = TTY::Logger.new
|
6
|
-
expect {
|
7
|
-
logger.compare_levels(:error, :unknown)
|
8
|
-
}.to raise_error(ArgumentError, "Invalid level :unknown")
|
9
|
-
end
|
10
|
-
|
11
|
-
it "compares names with equal level" do
|
12
|
-
logger = TTY::Logger.new
|
13
|
-
expect(logger.compare_levels(:info, :info)).to eq(:eq)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "compares names with equal level" do
|
17
|
-
logger = TTY::Logger.new
|
18
|
-
expect(logger.compare_levels("INFO", "INFO")).to eq(:eq)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "compares numbers with equal level" do
|
22
|
-
logger = TTY::Logger.new
|
23
|
-
expect(logger.compare_levels(TTY::Logger::INFO_LEVEL, TTY::Logger::INFO_LEVEL)).to eq(:eq)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "compares names with lower level" do
|
27
|
-
logger = TTY::Logger.new
|
28
|
-
expect(logger.compare_levels(:debug, :warn)).to eq(:lt)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "compares numbers with lower level" do
|
32
|
-
logger = TTY::Logger.new
|
33
|
-
expect(logger.compare_levels(TTY::Logger::DEBUG_LEVEL, TTY::Logger::WARN_LEVEL)).to eq(:lt)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "compares names with greater level" do
|
37
|
-
logger = TTY::Logger.new
|
38
|
-
expect(logger.compare_levels(:error, :info)).to eq(:gt)
|
39
|
-
end
|
40
|
-
end
|
data/spec/unit/log_at_spec.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "#log_at" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
|
6
|
-
it "logs temporarily at noisy level" do
|
7
|
-
logger = TTY::Logger.new(output: output) do |config|
|
8
|
-
config.handlers = [:stream]
|
9
|
-
end
|
10
|
-
|
11
|
-
logger.debug("not logged")
|
12
|
-
|
13
|
-
logger.log_at :debug do
|
14
|
-
logger.debug("logged")
|
15
|
-
end
|
16
|
-
|
17
|
-
logger.debug("not logged")
|
18
|
-
|
19
|
-
expect(output.string).to eq("level=debug message=logged\n")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "logs temporarily at quiet level" do
|
23
|
-
logger = TTY::Logger.new(output: output) do |config|
|
24
|
-
config.handlers = [:stream]
|
25
|
-
end
|
26
|
-
|
27
|
-
logger.log_at TTY::Logger::ERROR_LEVEL do
|
28
|
-
logger.debug("not logged")
|
29
|
-
logger.error("logged")
|
30
|
-
end
|
31
|
-
|
32
|
-
expect(output.string).to eq("level=error message=logged\n")
|
33
|
-
end
|
34
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "log metadata" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "outputs metadata" do
|
8
|
-
time_now = Time.new(2019, 7, 10, 19, 42, 35, "+02:00")
|
9
|
-
allow(Time).to receive(:now).and_return(time_now)
|
10
|
-
|
11
|
-
logger = TTY::Logger.new(output: output) do |config|
|
12
|
-
config.metadata = [:time, :date, :file]
|
13
|
-
end
|
14
|
-
|
15
|
-
logger.info("Deploying", app: "myapp", env: "prod")
|
16
|
-
|
17
|
-
expected_output = [
|
18
|
-
"\e[37m[19:42:35.000]\e[0m ",
|
19
|
-
"\e[37m[2019-07-10]\e[0m ",
|
20
|
-
"\e[37m[#{__FILE__}:#{__LINE__ - 5}:in`<top (required)>`]\e[0m",
|
21
|
-
" #{TTY::Logger::Handlers::Console::ARROW} ",
|
22
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
23
|
-
"\e[32minfo\e[0m ",
|
24
|
-
"Deploying ",
|
25
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"
|
26
|
-
].join
|
27
|
-
|
28
|
-
expect(output.string).to eq(expected_output)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "outputs all metadata when :all key used" do
|
32
|
-
time_now = Time.new(2019, 7, 10, 19, 42, 35, "+02:00")
|
33
|
-
allow(Time).to receive(:now).and_return(time_now)
|
34
|
-
|
35
|
-
logger = TTY::Logger.new(output: output) do |config|
|
36
|
-
config.metadata = [:all]
|
37
|
-
end
|
38
|
-
|
39
|
-
logger.info("Deploying", app: "myapp", env: "prod")
|
40
|
-
|
41
|
-
expected_output = [
|
42
|
-
"\e[37m[#{Process.pid}]\e[0m ",
|
43
|
-
"\e[37m[2019-07-10]\e[0m ",
|
44
|
-
"\e[37m[19:42:35.000]\e[0m ",
|
45
|
-
"\e[37m[#{__FILE__}:#{__LINE__ - 6}:in`<top (required)>`]\e[0m",
|
46
|
-
" #{TTY::Logger::Handlers::Console::ARROW} ",
|
47
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
48
|
-
"\e[32minfo\e[0m ",
|
49
|
-
"Deploying ",
|
50
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"
|
51
|
-
].join
|
52
|
-
|
53
|
-
expect(output.string).to eq(expected_output)
|
54
|
-
end
|
55
|
-
end
|
data/spec/unit/log_spec.rb
DELETED
@@ -1,203 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "#log" do
|
4
|
-
let(:output) { StringIO.new }
|
5
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
6
|
-
|
7
|
-
it "logs without a method name directly using level" do
|
8
|
-
logger = TTY::Logger.new(output: output) do |config|
|
9
|
-
config.level = :debug
|
10
|
-
end
|
11
|
-
|
12
|
-
logger.log(:debug, "Deploying...")
|
13
|
-
|
14
|
-
expect(output.string).to eq([
|
15
|
-
"\e[36m#{styles[:debug][:symbol]}\e[0m ",
|
16
|
-
"\e[36mdebug\e[0m ",
|
17
|
-
"Deploying... \n"
|
18
|
-
].join)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "logs a message at debug level" do
|
22
|
-
logger = TTY::Logger.new(output: output) do |config|
|
23
|
-
config.level = :debug
|
24
|
-
end
|
25
|
-
|
26
|
-
logger.debug("Successfully", "deployed")
|
27
|
-
|
28
|
-
expect(output.string).to eq([
|
29
|
-
"\e[36m#{styles[:debug][:symbol]}\e[0m ",
|
30
|
-
"\e[36mdebug\e[0m ",
|
31
|
-
"Successfully deployed \n"].join)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "logs a message at info level" do
|
35
|
-
logger = TTY::Logger.new(output: output)
|
36
|
-
|
37
|
-
logger.info("Successfully", "deployed")
|
38
|
-
|
39
|
-
expect(output.string).to eq([
|
40
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
41
|
-
"\e[32minfo\e[0m ",
|
42
|
-
"Successfully deployed \n"].join)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "logs a message at warn level" do
|
46
|
-
logger = TTY::Logger.new(output: output)
|
47
|
-
|
48
|
-
logger.warn("Failed to", "deploy")
|
49
|
-
|
50
|
-
expect(output.string).to eq([
|
51
|
-
"\e[33m#{styles[:warn][:symbol]}\e[0m ",
|
52
|
-
"\e[33mwarning\e[0m ",
|
53
|
-
"Failed to deploy \n"].join)
|
54
|
-
end
|
55
|
-
|
56
|
-
it "logs a message at error level" do
|
57
|
-
logger = TTY::Logger.new(output: output)
|
58
|
-
|
59
|
-
logger.error("Failed to", "deploy")
|
60
|
-
|
61
|
-
expect(output.string).to eq([
|
62
|
-
"\e[31m#{styles[:error][:symbol]}\e[0m ",
|
63
|
-
"\e[31merror\e[0m ",
|
64
|
-
"Failed to deploy \n"].join)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "logs a message at fatal level" do
|
68
|
-
logger = TTY::Logger.new(output: output)
|
69
|
-
|
70
|
-
logger.fatal("Failed to", "deploy")
|
71
|
-
|
72
|
-
expect(output.string).to eq([
|
73
|
-
"\e[31m#{styles[:fatal][:symbol]}\e[0m ",
|
74
|
-
"\e[31mfatal\e[0m ",
|
75
|
-
"Failed to deploy \n"].join)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "logs a success message at info level" do
|
79
|
-
logger = TTY::Logger.new(output: output)
|
80
|
-
|
81
|
-
logger.success("Deployed", "successfully")
|
82
|
-
|
83
|
-
expect(output.string).to eq([
|
84
|
-
"\e[32m#{styles[:success][:symbol]}\e[0m ",
|
85
|
-
"\e[32msuccess\e[0m ",
|
86
|
-
"Deployed successfully \n"].join)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "logs a wait message at info level" do
|
90
|
-
logger = TTY::Logger.new(output: output)
|
91
|
-
|
92
|
-
logger.wait("Waiting for", "deploy")
|
93
|
-
|
94
|
-
expect(output.string).to eq([
|
95
|
-
"\e[36m#{styles[:wait][:symbol]}\e[0m ",
|
96
|
-
"\e[36mwaiting\e[0m ",
|
97
|
-
"Waiting for deploy \n"].join)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "logs a message in a block" do
|
101
|
-
logger = TTY::Logger.new(output: output)
|
102
|
-
|
103
|
-
logger.info { "Successfully deployed" }
|
104
|
-
|
105
|
-
expect(output.string).to eq([
|
106
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
107
|
-
"\e[32minfo\e[0m ",
|
108
|
-
"Successfully deployed \n"].join)
|
109
|
-
end
|
110
|
-
|
111
|
-
it "logs a message in a block as an array of elements" do
|
112
|
-
logger = TTY::Logger.new(output: output)
|
113
|
-
|
114
|
-
logger.info { ["Successfully", "deployed"] }
|
115
|
-
|
116
|
-
expect(output.string).to eq([
|
117
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
118
|
-
"\e[32minfo\e[0m ",
|
119
|
-
"Successfully deployed \n"].join)
|
120
|
-
end
|
121
|
-
|
122
|
-
it "logs a message in a block with metadata" do
|
123
|
-
logger = TTY::Logger.new(output: output)
|
124
|
-
|
125
|
-
logger.info { ["Successfully", "deployed", {app: "myapp", env: "prod"}] }
|
126
|
-
|
127
|
-
expect(output.string).to eq([
|
128
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
129
|
-
"\e[32minfo\e[0m ",
|
130
|
-
"Successfully deployed ",
|
131
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"
|
132
|
-
].join)
|
133
|
-
end
|
134
|
-
|
135
|
-
it "doesn't log when lower level" do
|
136
|
-
logger = TTY::Logger.new(output: output) do |config|
|
137
|
-
config.level = :warn
|
138
|
-
end
|
139
|
-
|
140
|
-
logger.debug("Successfully deployed")
|
141
|
-
|
142
|
-
expect(output.string).to eq("")
|
143
|
-
end
|
144
|
-
|
145
|
-
it "logs message with global fields" do
|
146
|
-
logger = TTY::Logger.new(output: output, fields: {app: 'myapp', env: 'prod'})
|
147
|
-
|
148
|
-
logger.info("Successfully deployed")
|
149
|
-
|
150
|
-
expect(output.string).to eq([
|
151
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
152
|
-
"\e[32minfo\e[0m ",
|
153
|
-
"Successfully deployed ",
|
154
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"].join)
|
155
|
-
end
|
156
|
-
|
157
|
-
it "logs message with scoped fields" do
|
158
|
-
logger = TTY::Logger.new(output: output)
|
159
|
-
|
160
|
-
logger.info("Successfully deployed", app: 'myapp', env: 'prod')
|
161
|
-
|
162
|
-
expect(output.string).to eq([
|
163
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m ",
|
164
|
-
"\e[32minfo\e[0m ",
|
165
|
-
"Successfully deployed ",
|
166
|
-
"\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"].join)
|
167
|
-
end
|
168
|
-
|
169
|
-
it "adds new custom log type" do
|
170
|
-
heart = "❤"
|
171
|
-
logger = TTY::Logger.new(output: output) do |config|
|
172
|
-
config.types = {thanks: {level: :info}}
|
173
|
-
config.handlers = [
|
174
|
-
[:console, {
|
175
|
-
styles: {
|
176
|
-
thanks: {
|
177
|
-
symbol: heart,
|
178
|
-
label: "thanks",
|
179
|
-
color: :red,
|
180
|
-
levelpad: 1
|
181
|
-
}
|
182
|
-
}
|
183
|
-
}]
|
184
|
-
]
|
185
|
-
end
|
186
|
-
|
187
|
-
logger.thanks("Great work!", app: "myapp", env: "prod")
|
188
|
-
|
189
|
-
expect(output.string).to eq([
|
190
|
-
"\e[31m#{heart}\e[0m ",
|
191
|
-
"\e[31mthanks\e[0m ",
|
192
|
-
"Great work! ",
|
193
|
-
"\e[31mapp\e[0m=myapp \e[31menv\e[0m=prod\n"].join)
|
194
|
-
end
|
195
|
-
|
196
|
-
it "fails to add already defined log type" do
|
197
|
-
expect {
|
198
|
-
TTY::Logger.new do |config|
|
199
|
-
config.types = {success: {level: :info}}
|
200
|
-
end
|
201
|
-
}.to raise_error(TTY::Logger::Error, "Already defined log type :success")
|
202
|
-
end
|
203
|
-
end
|
data/spec/unit/output_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Logger, "outputs" do
|
4
|
-
let(:styles) { TTY::Logger::Handlers::Console::STYLES }
|
5
|
-
|
6
|
-
it "outputs to multiple streams of the same handler" do
|
7
|
-
stream = StringIO.new
|
8
|
-
|
9
|
-
logger = TTY::Logger.new do |config|
|
10
|
-
config.handlers = [[:stream, formatter: :text]]
|
11
|
-
config.output = [stream, stream]
|
12
|
-
end
|
13
|
-
|
14
|
-
logger.info("logging")
|
15
|
-
|
16
|
-
expect(stream.string).to eq([
|
17
|
-
"level=info message=logging\n",
|
18
|
-
"level=info message=logging\n"
|
19
|
-
].join)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "outputs each handler to a different stream" do
|
23
|
-
stream = StringIO.new
|
24
|
-
|
25
|
-
logger = TTY::Logger.new do |config|
|
26
|
-
config.handlers = [
|
27
|
-
[:console, output: stream],
|
28
|
-
[:stream, output: stream]
|
29
|
-
]
|
30
|
-
end
|
31
|
-
|
32
|
-
logger.info("logging")
|
33
|
-
|
34
|
-
expect(stream.string).to eq([
|
35
|
-
"\e[32m#{styles[:info][:symbol]}\e[0m \e[32minfo\e[0m ",
|
36
|
-
"logging \n",
|
37
|
-
"level=info message=logging\n"
|
38
|
-
].join)
|
39
|
-
end
|
40
|
-
end
|