-
Notifications
You must be signed in to change notification settings - Fork 1k
/
LogbackLogLevelConfigurerSpec.groovy
166 lines (136 loc) · 5.57 KB
/
LogbackLogLevelConfigurerSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package io.micronaut.logging
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import com.github.stefanbirkner.systemlambda.SystemLambda
import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.env.yaml.YamlPropertySourceLoader
import io.micronaut.context.exceptions.BeanInstantiationException
import io.micronaut.context.exceptions.ConfigurationException
import org.slf4j.LoggerFactory
import spock.lang.Specification
import spock.lang.Unroll
class LogbackLogLevelConfigurerSpec extends Specification {
@Unroll
void 'test that log levels on logger "#loggerName" can be configured via properties'() {
given:
((Logger) LoggerFactory.getLogger('foo.bar1')).setLevel(Level.DEBUG)
((Logger) LoggerFactory.getLogger('foo.bar2')).setLevel(Level.DEBUG)
((Logger) LoggerFactory.getLogger('foo.bar3')).setLevel(Level.ERROR)
((Logger) LoggerFactory.getLogger('foo.barBaz')).setLevel(Level.WARN)
((Logger) LoggerFactory.getLogger('ignoring.error')).setLevel(Level.INFO)
when:
ApplicationContext context = ApplicationContext.run(
[
'logger.levels.aaa.bbb.ccc' : 'ERROR',
'logger.levels.foo.bar2' : 'INFO',
'logger.levels.foo.bar3' : '',
'logger.levels.foo.barBaz' : 'INFO',
'logger.levels.ignoring.error': 'OFF',
]
)
then:
((Logger) LoggerFactory.getLogger(loggerName)).getLevel() == expectedLevel
cleanup:
context.close()
where:
loggerName | expectedLevel
'foo.bar1' | Level.DEBUG
'foo.bar2' | Level.INFO
'foo.bar3' | null
'aaa.bbb.ccc' | Level.ERROR
'foo.barBaz' | Level.INFO
'ignoring.error' | Level.OFF
}
void 'test that log level OFF without quotes does not get treated as boolean false'() {
given:
def map = new YamlPropertySourceLoader().read("myconfig.yml", '''
logger:
levels:
io.annoying.class: OFF
'''.bytes)
when:
ApplicationContext context = ApplicationContext.builder()
.propertySources(PropertySource.of(map))
.start()
then:
((Logger) LoggerFactory.getLogger("io.annoying.class")).getLevel() == Level.OFF
cleanup:
context.close()
}
void 'test that log level ON throws BeanInstantiationException with nested cause of ConfigurationException'() {
given:
def map = new YamlPropertySourceLoader().read("myconfig.yml", '''
logger:
levels:
io.annoying.class: ON
'''.bytes)
when:
ApplicationContext.builder()
.propertySources(PropertySource.of(map))
.start()
then:
BeanInstantiationException beanInstantiationException = thrown(BeanInstantiationException)
beanInstantiationException.cause.cause instanceof ConfigurationException
}
void 'test that log levels can be configured via environment variables'() {
given:
((Logger) LoggerFactory.getLogger('foo.bar1')).setLevel(Level.DEBUG)
((Logger) LoggerFactory.getLogger('foo.bar2')).setLevel(Level.DEBUG)
when:
ApplicationContext context = ApplicationContext.builder().build()
SystemLambda.withEnvironmentVariable("LOGGER_LEVELS_FOO_BAR2", "INFO")
.execute(() -> {
context.start()
})
then:
((Logger) LoggerFactory.getLogger(loggerName)).getLevel() == expectedLevel
cleanup:
context.close()
where:
loggerName | expectedLevel
'foo.bar1' | Level.DEBUG
'foo.bar2' | Level.INFO
}
void 'test that log levels set in application.yaml can be overridden by environment variables'() {
given:
def map = new YamlPropertySourceLoader().read("application.yml", '''
logger:
levels:
foo.bar3: ERROR
'''.bytes)
((Logger) LoggerFactory.getLogger('foo.bar3')).setLevel(Level.DEBUG)
when:
// Use same order as application.yaml to ensure it loads before environment variable property source
ApplicationContext context = ApplicationContext.builder()
.propertySources(PropertySource.of("application", map, YamlPropertySourceLoader.DEFAULT_POSITION))
.build()
SystemLambda.withEnvironmentVariable("LOGGER_LEVELS_FOO_BAR3", "INFO")
.execute(() -> {
context.start()
})
then:
((Logger) LoggerFactory.getLogger(loggerName)).getLevel() == expectedLevel
cleanup:
context.close()
where:
loggerName | expectedLevel
'foo.bar3' | Level.INFO
}
void 'logging refresh is properly called on application start'() {
given:
ApplicationContext context = ApplicationContext.builder()
.build()
def loggingSystem = context.getBean(LoggingSystem.class)
def spy = Spy(loggingSystem)
when:
SystemLambda.withEnvironmentVariable("LOGGER_LEVELS_FOO_BAR3", "INFO")
.execute(() -> {
context.start()
})
then:
1 * spy.refresh()
cleanup:
context.close()
}
}