Skip to content

Commit a612603

Browse files
committed
Logger support for non-block message
Previously, only messages passed to the logger via a block would be printed. Due to the wonky logger API, messages and prognames are interchangeable and this was not taken into account in the implementation. Since the progname is fixed at logger creation time, we were discarding any other value that might be provided (which at times could be the actual message). The new implementation uses the input message or input progname, whichever is not nil, as the message. [#63104902]
1 parent 2065e5a commit a612603

3 files changed

Lines changed: 125 additions & 59 deletions

File tree

java-buildpack.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
<orderEntry type="sourceFolder" forTests="false" />
271271
<orderEntry type="library" scope="PROVIDED" name="addressable (v2.3.5, rbenv: 1.9.3-p484) [gem]" level="application" />
272272
<orderEntry type="library" scope="PROVIDED" name="ast (v1.1.0, rbenv: 1.9.3-p484) [gem]" level="application" />
273-
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, rbenv: 1.9.3-p484) [gem]" level="application" />
273+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.5.1, rbenv: 1.9.3-p484) [gem]" level="application" />
274274
<orderEntry type="library" scope="PROVIDED" name="codeclimate-test-reporter (v0.2.0, rbenv: 1.9.3-p484) [gem]" level="application" />
275275
<orderEntry type="library" scope="PROVIDED" name="crack (v0.4.1, rbenv: 1.9.3-p484) [gem]" level="application" />
276276
<orderEntry type="library" scope="PROVIDED" name="debugger-ruby_core_source (v1.2.4, rbenv: 1.9.3-p484) [gem]" level="application" />

lib/java_buildpack/logging/delegating_logger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def initialize(klass, delegates)
3535
#
3636
# @param [Logger::Severity] severity the severity of the message
3737
# @param [String] message the message
38-
# @param [String] progname ignored
38+
# @param [String] progname the message when passed in as a parameter
3939
# @yield evaluated for the message
4040
def add(severity, message = nil, progname = nil, &block)
41-
@delegates.each { |delegate| delegate.add severity, message, @klass, &block }
41+
@delegates.each { |delegate| delegate.add severity, message || progname, @klass, &block }
4242
end
4343

4444
end

spec/java_buildpack/logging/logger_factory_spec.rb

Lines changed: 122 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
require 'java_buildpack/logging/logger_factory'
2121
require 'java_buildpack/util/configuration_utils'
2222

23-
describe JavaBuildpack::Logging::LoggerFactory do
23+
describe JavaBuildpack::Logging::LoggerFactory, :focus do
2424
include_context 'console_helper'
2525
include_context 'logging_helper'
2626

@@ -31,83 +31,125 @@
3131

3232
trigger
3333

34-
expect(log_contents).to match /DEBUG debug-message/
35-
expect(log_contents).to match /INFO info-message/
36-
expect(log_contents).to match /WARN warn-message/
37-
expect(log_contents).to match /ERROR error-message/
38-
expect(log_contents).to match /FATAL fatal-message/
34+
expect(log_contents).to match /DEBUG block-debug-message/
35+
expect(log_contents).to match /INFO block-info-message/
36+
expect(log_contents).to match /WARN block-warn-message/
37+
expect(log_contents).to match /ERROR block-error-message/
38+
expect(log_contents).to match /FATAL block-fatal-message/
39+
40+
expect(log_contents).to match /DEBUG param-debug-message/
41+
expect(log_contents).to match /INFO param-info-message/
42+
expect(log_contents).to match /WARN param-warn-message/
43+
expect(log_contents).to match /ERROR param-error-message/
44+
expect(log_contents).to match /FATAL param-fatal-message/
3945
end
4046

4147
it 'should log all levels to console when JBP_LOG_LEVEL set to DEBUG',
4248
log_level: 'DEBUG' do
4349

4450
trigger
4551

46-
expect(stderr.string).to match /DEBUG debug-message/
47-
expect(stderr.string).to match /INFO info-message/
48-
expect(stderr.string).to match /WARN warn-message/
49-
expect(stderr.string).to match /ERROR error-message/
50-
expect(stderr.string).to match /FATAL fatal-message/
52+
expect(stderr.string).to match /DEBUG block-debug-message/
53+
expect(stderr.string).to match /INFO block-info-message/
54+
expect(stderr.string).to match /WARN block-warn-message/
55+
expect(stderr.string).to match /ERROR block-error-message/
56+
expect(stderr.string).to match /FATAL block-fatal-message/
57+
58+
expect(stderr.string).to match /DEBUG param-debug-message/
59+
expect(stderr.string).to match /INFO param-info-message/
60+
expect(stderr.string).to match /WARN param-warn-message/
61+
expect(stderr.string).to match /ERROR param-error-message/
62+
expect(stderr.string).to match /FATAL param-fatal-message/
5163
end
5264

5365
it 'should log all levels above INFO to console when JBP_LOG_LEVEL set to INFO',
5466
log_level: 'INFO' do
5567

5668
trigger
5769

58-
expect(stderr.string).not_to match /DEBUG debug-message/
59-
expect(stderr.string).to match /INFO info-message/
60-
expect(stderr.string).to match /WARN warn-message/
61-
expect(stderr.string).to match /ERROR error-message/
62-
expect(stderr.string).to match /FATAL fatal-message/
70+
expect(stderr.string).not_to match /DEBUG block-debug-message/
71+
expect(stderr.string).to match /INFO block-info-message/
72+
expect(stderr.string).to match /WARN block-warn-message/
73+
expect(stderr.string).to match /ERROR block-error-message/
74+
expect(stderr.string).to match /FATAL block-fatal-message/
75+
76+
expect(stderr.string).not_to match /DEBUG param-debug-message/
77+
expect(stderr.string).to match /INFO param-info-message/
78+
expect(stderr.string).to match /WARN param-warn-message/
79+
expect(stderr.string).to match /ERROR param-error-message/
80+
expect(stderr.string).to match /FATAL param-fatal-message/
6381
end
6482

6583
it 'should log all levels above WARN to console when JBP_LOG_LEVEL set to WARN',
6684
log_level: 'WARN' do
6785

6886
trigger
6987

70-
expect(stderr.string).not_to match /DEBUG debug-message/
71-
expect(stderr.string).not_to match /INFO info-message/
72-
expect(stderr.string).to match /WARN warn-message/
73-
expect(stderr.string).to match /ERROR error-message/
74-
expect(stderr.string).to match /FATAL fatal-message/
88+
expect(stderr.string).not_to match /DEBUG block-debug-message/
89+
expect(stderr.string).not_to match /INFO block-info-message/
90+
expect(stderr.string).to match /WARN block-warn-message/
91+
expect(stderr.string).to match /ERROR block-error-message/
92+
expect(stderr.string).to match /FATAL block-fatal-message/
93+
94+
expect(stderr.string).not_to match /DEBUG param-debug-message/
95+
expect(stderr.string).not_to match /INFO param-info-message/
96+
expect(stderr.string).to match /WARN param-warn-message/
97+
expect(stderr.string).to match /ERROR param-error-message/
98+
expect(stderr.string).to match /FATAL param-fatal-message/
7599
end
76100

77101
it 'should log all levels above ERROR to console when JBP_LOG_LEVEL set to ERROR',
78102
log_level: 'ERROR' do
79103

80104
trigger
81105

82-
expect(stderr.string).not_to match /DEBUG debug-message/
83-
expect(stderr.string).not_to match /INFO info-message/
84-
expect(stderr.string).not_to match /WARN warn-message/
85-
expect(stderr.string).to match /ERROR error-message/
86-
expect(stderr.string).to match /FATAL fatal-message/
106+
expect(stderr.string).not_to match /DEBUG block-debug-message/
107+
expect(stderr.string).not_to match /INFO block-info-message/
108+
expect(stderr.string).not_to match /WARN block-warn-message/
109+
expect(stderr.string).to match /ERROR block-error-message/
110+
expect(stderr.string).to match /FATAL block-fatal-message/
111+
112+
expect(stderr.string).not_to match /DEBUG param-debug-message/
113+
expect(stderr.string).not_to match /INFO param-info-message/
114+
expect(stderr.string).not_to match /WARN param-warn-message/
115+
expect(stderr.string).to match /ERROR param-error-message/
116+
expect(stderr.string).to match /FATAL param-fatal-message/
87117
end
88118

89119
it 'should log FATAL to console when JBP_LOG_LEVEL set to FATAL',
90120
log_level: 'FATAL' do
91121

92122
trigger
93123

94-
expect(stderr.string).not_to match /DEBUG debug-message/
95-
expect(stderr.string).not_to match /INFO info-message/
96-
expect(stderr.string).not_to match /WARN warn-message/
97-
expect(stderr.string).not_to match /ERROR error-message/
98-
expect(stderr.string).to match /FATAL fatal-message/
124+
expect(stderr.string).not_to match /DEBUG block-debug-message/
125+
expect(stderr.string).not_to match /INFO block-info-message/
126+
expect(stderr.string).not_to match /WARN block-warn-message/
127+
expect(stderr.string).not_to match /ERROR block-error-message/
128+
expect(stderr.string).to match /FATAL block-fatal-message/
129+
130+
expect(stderr.string).not_to match /DEBUG param-debug-message/
131+
expect(stderr.string).not_to match /INFO param-info-message/
132+
expect(stderr.string).not_to match /WARN param-warn-message/
133+
expect(stderr.string).not_to match /ERROR param-error-message/
134+
expect(stderr.string).to match /FATAL param-fatal-message/
99135
end
100136

101137
it 'should log all levels to console when $DEBUG set',
102138
:debug do
103139

104140
trigger
105141

106-
expect(stderr.string).to match /DEBUG debug-message/
107-
expect(stderr.string).to match /INFO info-message/
108-
expect(stderr.string).to match /WARN warn-message/
109-
expect(stderr.string).to match /ERROR error-message/
110-
expect(stderr.string).to match /FATAL fatal-message/
142+
expect(stderr.string).to match /DEBUG block-debug-message/
143+
expect(stderr.string).to match /INFO block-info-message/
144+
expect(stderr.string).to match /WARN block-warn-message/
145+
expect(stderr.string).to match /ERROR block-error-message/
146+
expect(stderr.string).to match /FATAL block-fatal-message/
147+
148+
expect(stderr.string).to match /DEBUG param-debug-message/
149+
expect(stderr.string).to match /INFO param-info-message/
150+
expect(stderr.string).to match /WARN param-warn-message/
151+
expect(stderr.string).to match /ERROR param-error-message/
152+
expect(stderr.string).to match /FATAL param-fatal-message/
111153

112154
end
113155

@@ -116,11 +158,17 @@
116158

117159
trigger
118160

119-
expect(stderr.string).to match /DEBUG debug-message/
120-
expect(stderr.string).to match /INFO info-message/
121-
expect(stderr.string).to match /WARN warn-message/
122-
expect(stderr.string).to match /ERROR error-message/
123-
expect(stderr.string).to match /FATAL fatal-message/
161+
expect(stderr.string).to match /DEBUG block-debug-message/
162+
expect(stderr.string).to match /INFO block-info-message/
163+
expect(stderr.string).to match /WARN block-warn-message/
164+
expect(stderr.string).to match /ERROR block-error-message/
165+
expect(stderr.string).to match /FATAL block-fatal-message/
166+
167+
expect(stderr.string).to match /DEBUG param-debug-message/
168+
expect(stderr.string).to match /INFO param-info-message/
169+
expect(stderr.string).to match /WARN param-warn-message/
170+
expect(stderr.string).to match /ERROR param-error-message/
171+
expect(stderr.string).to match /FATAL param-fatal-message/
124172

125173
end
126174

@@ -140,11 +188,17 @@
140188
it 'should log all levels to console when default_log_level set to DEBUG in configuration file' do
141189
trigger
142190

143-
expect(stderr.string).to match /DEBUG debug-message/
144-
expect(stderr.string).to match /INFO info-message/
145-
expect(stderr.string).to match /WARN warn-message/
146-
expect(stderr.string).to match /ERROR error-message/
147-
expect(stderr.string).to match /FATAL fatal-message/
191+
expect(stderr.string).to match /DEBUG block-debug-message/
192+
expect(stderr.string).to match /INFO block-info-message/
193+
expect(stderr.string).to match /WARN block-warn-message/
194+
expect(stderr.string).to match /ERROR block-error-message/
195+
expect(stderr.string).to match /FATAL block-fatal-message/
196+
197+
expect(stderr.string).to match /DEBUG param-debug-message/
198+
expect(stderr.string).to match /INFO param-info-message/
199+
expect(stderr.string).to match /WARN param-warn-message/
200+
expect(stderr.string).to match /ERROR param-error-message/
201+
expect(stderr.string).to match /FATAL param-fatal-message/
148202
end
149203
end
150204

@@ -159,11 +213,17 @@
159213
it 'should log all levels above INFO to console when no configuration has been set' do
160214
trigger
161215

162-
expect(stderr.string).not_to match /DEBUG debug-message/
163-
expect(stderr.string).to match /INFO info-message/
164-
expect(stderr.string).to match /WARN warn-message/
165-
expect(stderr.string).to match /ERROR error-message/
166-
expect(stderr.string).to match /FATAL fatal-message/
216+
expect(stderr.string).not_to match /DEBUG block-debug-message/
217+
expect(stderr.string).to match /INFO block-info-message/
218+
expect(stderr.string).to match /WARN block-warn-message/
219+
expect(stderr.string).to match /ERROR block-error-message/
220+
expect(stderr.string).to match /FATAL block-fatal-message/
221+
222+
expect(stderr.string).not_to match /DEBUG param-debug-message/
223+
expect(stderr.string).to match /INFO param-info-message/
224+
expect(stderr.string).to match /WARN param-warn-message/
225+
expect(stderr.string).to match /ERROR param-error-message/
226+
expect(stderr.string).to match /FATAL param-fatal-message/
167227
end
168228
end
169229

@@ -185,11 +245,17 @@
185245
end
186246

187247
def trigger
188-
logger.debug { 'debug-message' }
189-
logger.info { 'info-message' }
190-
logger.warn { 'warn-message' }
191-
logger.error { 'error-message' }
192-
logger.fatal { 'fatal-message' }
248+
logger.debug { 'block-debug-message' }
249+
logger.info { 'block-info-message' }
250+
logger.warn { 'block-warn-message' }
251+
logger.error { 'block-error-message' }
252+
logger.fatal { 'block-fatal-message' }
253+
254+
logger.debug 'param-debug-message'
255+
logger.info 'param-info-message'
256+
logger.warn 'param-warn-message'
257+
logger.error 'param-error-message'
258+
logger.fatal 'param-fatal-message'
193259
end
194260

195261
end

0 commit comments

Comments
 (0)