tty-logger 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::Logger, 'handlers' do
4
+ let(:output) { StringIO.new }
5
+ let(:styles) { TTY::Logger::Handlers::Console::STYLES }
6
+
7
+ it "coerces name into handler object" do
8
+ logger = TTY::Logger.new(output: output) do |config|
9
+ config.handlers = [:console]
10
+ end
11
+
12
+ logger.info("Logging")
13
+
14
+ expect(output.string).to eq([
15
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
16
+ "\e[32minfo\e[0m ",
17
+ "Logging \n"].join)
18
+ end
19
+
20
+ it "coerces class name into handler object" do
21
+ logger = TTY::Logger.new(output: output) do |config|
22
+ config.handlers = [TTY::Logger::Handlers::Console]
23
+ end
24
+
25
+ logger.info("Logging")
26
+
27
+ expect(output.string).to eq([
28
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
29
+ "\e[32minfo\e[0m ",
30
+ "Logging \n"].join)
31
+ end
32
+
33
+ it "changes default handler styling" do
34
+ logger = TTY::Logger.new(output: output) do |config|
35
+ config.handlers = [
36
+ [:console, {styles: {info: {symbol: "+", label: "INFO"}}}]
37
+ ]
38
+ end
39
+
40
+ logger.info("Logging")
41
+
42
+ expect(output.string).to eq([
43
+ "\e[32m+\e[0m ",
44
+ "\e[32mINFO\e[0m ",
45
+ "Logging \n"].join)
46
+ end
47
+
48
+ it "logs different levels for each handler" do
49
+ logger = TTY::Logger.new(output: output) do |config|
50
+ config.handlers = [
51
+ [:console, level: :error],
52
+ [:console, level: :debug]
53
+ ]
54
+ end
55
+
56
+ logger.info("Info")
57
+ logger.error("Error")
58
+
59
+ expect(output.string).to eq([
60
+ "\e[32m#{styles[:info][:symbol]}\e[0m \e[32minfo\e[0m Info \n",
61
+ "\e[31m#{styles[:error][:symbol]}\e[0m \e[31merror\e[0m Error \n",
62
+ "\e[31m#{styles[:error][:symbol]}\e[0m \e[31merror\e[0m Error \n",
63
+ ].join)
64
+ end
65
+
66
+ it "fails to coerce unknown object type into handler object" do
67
+ expect {
68
+ TTY::Logger.new do |config|
69
+ config.handlers = [true]
70
+ end
71
+ }.to raise_error(TTY::Logger::Error,
72
+ "Handler needs to be a class name or a symbol name")
73
+ end
74
+
75
+ it "fails to coerce name into handler object" do
76
+ expect {
77
+ TTY::Logger.new do |config|
78
+ config.handlers = [:unknown]
79
+ end
80
+ }.to raise_error(TTY::Logger::Error,
81
+ "Handler needs to be a class name or a symbol name")
82
+ end
83
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::Logger::Handlers::Null, "custom handler" do
4
+ let(:output) { StringIO.new }
5
+
6
+ it "logs messages with a custom handler" do
7
+ stub_const("MyHandler", Class.new do
8
+ def initialize(output: nil, config: nil, label: nil)
9
+ @label = label
10
+ @output = output
11
+ end
12
+
13
+ def call(event)
14
+ @output.puts "(#{@label}) #{event.message.join}"
15
+ end
16
+ end)
17
+
18
+ logger = TTY::Logger.new(output: output) do |config|
19
+ config.handlers = [[MyHandler, {label: "myhandler"}]]
20
+ end
21
+
22
+ logger.info("Logging")
23
+
24
+ expect(output.string).to eq("(myhandler) Logging\n")
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::Logger::Handlers::Null, "null handler" do
4
+ let(:output) { StringIO.new }
5
+
6
+ it "doesn't log with a null handler" do
7
+ logger = TTY::Logger.new(output: output) do |config|
8
+ config.handlers = [:null]
9
+ end
10
+
11
+ logger.info("Logging")
12
+
13
+ expect(output.string).to eq("")
14
+ end
15
+ end
@@ -0,0 +1,72 @@
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
@@ -0,0 +1,40 @@
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
@@ -0,0 +1,55 @@
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
@@ -1,7 +1,144 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe TTY::Logger do
4
- it "logs a message" do
5
- TTY::Logger.new
3
+ RSpec.describe TTY::Logger, "#log" do
4
+ let(:output) { StringIO.new }
5
+ let(:styles) { TTY::Logger::Handlers::Console::STYLES }
6
+
7
+ it "logs a message at debug level" do
8
+ logger = TTY::Logger.new(output: output) do |config|
9
+ config.level = :debug
10
+ end
11
+
12
+ logger.debug("Successfully", "deployed")
13
+
14
+ expect(output.string).to eq([
15
+ "\e[36m#{styles[:debug][:symbol]}\e[0m ",
16
+ "\e[36mdebug\e[0m ",
17
+ "Successfully deployed \n"].join)
18
+ end
19
+
20
+ it "logs a message at info level" do
21
+ logger = TTY::Logger.new(output: output)
22
+
23
+ logger.info("Successfully", "deployed")
24
+
25
+ expect(output.string).to eq([
26
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
27
+ "\e[32minfo\e[0m ",
28
+ "Successfully deployed \n"].join)
29
+ end
30
+
31
+ it "logs a message at warn level" do
32
+ logger = TTY::Logger.new(output: output)
33
+
34
+ logger.warn("Failed to", "deploy")
35
+
36
+ expect(output.string).to eq([
37
+ "\e[33m#{styles[:warn][:symbol]}\e[0m ",
38
+ "\e[33mwarning\e[0m ",
39
+ "Failed to deploy \n"].join)
40
+ end
41
+
42
+ it "logs a message at error level" do
43
+ logger = TTY::Logger.new(output: output)
44
+
45
+ logger.error("Failed to", "deploy")
46
+
47
+ expect(output.string).to eq([
48
+ "\e[31m#{styles[:error][:symbol]}\e[0m ",
49
+ "\e[31merror\e[0m ",
50
+ "Failed to deploy \n"].join)
51
+ end
52
+
53
+ it "logs a message at fatal level" do
54
+ logger = TTY::Logger.new(output: output)
55
+
56
+ logger.fatal("Failed to", "deploy")
57
+
58
+ expect(output.string).to eq([
59
+ "\e[31m#{styles[:fatal][:symbol]}\e[0m ",
60
+ "\e[31mfatal\e[0m ",
61
+ "Failed to deploy \n"].join)
62
+ end
63
+
64
+ it "logs a success message at info level" do
65
+ logger = TTY::Logger.new(output: output)
66
+
67
+ logger.success("Deployed", "successfully")
68
+
69
+ expect(output.string).to eq([
70
+ "\e[32m#{styles[:success][:symbol]}\e[0m ",
71
+ "\e[32msuccess\e[0m ",
72
+ "Deployed successfully \n"].join)
73
+ end
74
+
75
+ it "logs a wait message at info level" do
76
+ logger = TTY::Logger.new(output: output)
77
+
78
+ logger.wait("Waiting for", "deploy")
79
+
80
+ expect(output.string).to eq([
81
+ "\e[36m#{styles[:wait][:symbol]}\e[0m ",
82
+ "\e[36mwaiting\e[0m ",
83
+ "Waiting for deploy \n"].join)
84
+ end
85
+
86
+ it "logs a message in a block" do
87
+ logger = TTY::Logger.new(output: output) do |config|
88
+ config.level = :debug
89
+ end
90
+
91
+ logger.info { "Successfully deployed" }
92
+
93
+ expect(output.string).to eq([
94
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
95
+ "\e[32minfo\e[0m ",
96
+ "Successfully deployed \n"].join)
97
+ end
98
+
99
+ it "doesn't log when lower level" do
100
+ logger = TTY::Logger.new(output: output) do |config|
101
+ config.level = :warn
102
+ end
103
+
104
+ logger.debug("Successfully deployed")
105
+
106
+ expect(output.string).to eq("")
107
+ end
108
+
109
+ it "logs message with global fields" do
110
+ logger = TTY::Logger.new(output: output, fields: {app: 'myapp', env: 'prod'})
111
+
112
+ logger.info("Successfully deployed")
113
+
114
+ expect(output.string).to eq([
115
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
116
+ "\e[32minfo\e[0m ",
117
+ "Successfully deployed ",
118
+ "\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"].join)
119
+ end
120
+
121
+ it "logs message with fields" do
122
+ logger = TTY::Logger.new(output: output)
123
+
124
+ logger.with(app: 'myapp', env: 'prod').info("Successfully deployed")
125
+
126
+ expect(output.string).to eq([
127
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
128
+ "\e[32minfo\e[0m ",
129
+ "Successfully deployed ",
130
+ "\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"].join)
131
+ end
132
+
133
+ it "logs message with scoped fields" do
134
+ logger = TTY::Logger.new(output: output)
135
+
136
+ logger.info("Successfully deployed", app: 'myapp', env: 'prod')
137
+
138
+ expect(output.string).to eq([
139
+ "\e[32m#{styles[:info][:symbol]}\e[0m ",
140
+ "\e[32minfo\e[0m ",
141
+ "Successfully deployed ",
142
+ "\e[32mapp\e[0m=myapp \e[32menv\e[0m=prod\n"].join)
6
143
  end
7
144
  end
@@ -0,0 +1,40 @@
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
data/tty-logger.gemspec CHANGED
@@ -27,6 +27,8 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.required_ruby_version = '>= 2.0.0'
29
29
 
30
+ spec.add_dependency "pastel", "~> 0.7.0"
31
+
30
32
  spec.add_development_dependency "bundler", ">= 1.5"
31
33
  spec.add_development_dependency "rake"
32
34
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-07 00:00:00.000000000 Z
11
+ date: 2019-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.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.7.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -63,11 +77,40 @@ files:
63
77
  - LICENSE.txt
64
78
  - README.md
65
79
  - Rakefile
80
+ - examples/console.rb
81
+ - examples/error.rb
82
+ - examples/handler.rb
83
+ - examples/output.rb
84
+ - examples/override.rb
85
+ - examples/stream.rb
66
86
  - lib/tty-logger.rb
67
87
  - lib/tty/logger.rb
88
+ - lib/tty/logger/config.rb
89
+ - lib/tty/logger/event.rb
90
+ - lib/tty/logger/formatters/json.rb
91
+ - lib/tty/logger/formatters/text.rb
92
+ - lib/tty/logger/handlers/base.rb
93
+ - lib/tty/logger/handlers/console.rb
94
+ - lib/tty/logger/handlers/null.rb
95
+ - lib/tty/logger/handlers/stream.rb
96
+ - lib/tty/logger/levels.rb
68
97
  - lib/tty/logger/version.rb
69
98
  - spec/spec_helper.rb
99
+ - spec/unit/add_handler_spec.rb
100
+ - spec/unit/config_spec.rb
101
+ - spec/unit/event_spec.rb
102
+ - spec/unit/exception_spec.rb
103
+ - spec/unit/formatter_spec.rb
104
+ - spec/unit/formatters/json_spec.rb
105
+ - spec/unit/formatters/text_spec.rb
106
+ - spec/unit/handler_spec.rb
107
+ - spec/unit/handlers/custom_spec.rb
108
+ - spec/unit/handlers/null_spec.rb
109
+ - spec/unit/handlers/stream_spec.rb
110
+ - spec/unit/levels_spec.rb
111
+ - spec/unit/log_metadata_spec.rb
70
112
  - spec/unit/log_spec.rb
113
+ - spec/unit/output_spec.rb
71
114
  - tasks/console.rake
72
115
  - tasks/coverage.rake
73
116
  - tasks/spec.rake