0
# Logging Factories
1
2
Core logging system configuration and lifecycle management providing the main entry points for setting up logging in Dropwizard applications. The LoggingFactory interface serves as the primary SPI for implementing custom logging configurations.
3
4
## Capabilities
5
6
### LoggingFactory Interface
7
8
Main SPI interface for configuring logging systems with pluggable implementations supporting Jackson-based polymorphic deserialization.
9
10
```java { .api }
11
/**
12
* Main SPI interface for configuring logging systems
13
*/
14
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = DefaultLoggingFactory.class)
15
public interface LoggingFactory extends Discoverable {
16
/**
17
* Configure the logging system with metrics integration
18
* @param metricRegistry the metrics registry for logging metrics collection
19
* @param name the application name for logging context
20
*/
21
void configure(MetricRegistry metricRegistry, String name);
22
23
/**
24
* Flush all log messages without disabling logging
25
*/
26
void stop();
27
28
/**
29
* Reset logging to sane defaults, useful for testing
30
*/
31
void reset();
32
}
33
```
34
35
### DefaultLoggingFactory
36
37
Default implementation of LoggingFactory that handles standard Dropwizard logging configuration with full YAML configuration support.
38
39
```java { .api }
40
/**
41
* Default implementation handling standard Dropwizard logging configuration
42
*/
43
@JsonTypeName("default")
44
public class DefaultLoggingFactory implements LoggingFactory {
45
private String level = "INFO";
46
private Map<String, JsonNode> loggers = new LinkedHashMap<>();
47
private List<AppenderFactory<ILoggingEvent>> appenders = new ArrayList<>();
48
49
/**
50
* Set the root logging level
51
* @param level the logging level (TRACE, DEBUG, INFO, WARN, ERROR, OFF)
52
*/
53
public void setLevel(String level);
54
55
/**
56
* Get the root logging level
57
* @return the current logging level
58
*/
59
public String getLevel();
60
61
/**
62
* Set logger-specific configurations
63
* @param loggers map of logger names to their configurations
64
*/
65
public void setLoggers(Map<String, JsonNode> loggers);
66
67
/**
68
* Get logger-specific configurations
69
* @return map of logger configurations
70
*/
71
public Map<String, JsonNode> getLoggers();
72
73
/**
74
* Set the list of appenders
75
* @param appenders list of appender factories
76
*/
77
public void setAppenders(List<AppenderFactory<ILoggingEvent>> appenders);
78
79
/**
80
* Get the list of appenders
81
* @return list of configured appender factories
82
*/
83
public List<AppenderFactory<ILoggingEvent>> getAppenders();
84
85
/**
86
* Convert string to Logback Level with error handling
87
* @param text the level name
88
* @return the corresponding Logback Level
89
* @throws IllegalArgumentException if level is invalid
90
*/
91
public static Level toLevel(String text);
92
93
@Override
94
public void configure(MetricRegistry metricRegistry, String name);
95
96
@Override
97
public void stop();
98
99
@Override
100
public void reset();
101
}
102
```
103
104
**Usage Example:**
105
106
```java
107
DefaultLoggingFactory loggingFactory = new DefaultLoggingFactory();
108
loggingFactory.setLevel("INFO");
109
110
// Configure specific loggers
111
Map<String, JsonNode> loggers = new HashMap<>();
112
ObjectNode dbLogger = JsonNodeFactory.instance.objectNode();
113
dbLogger.put("level", "DEBUG");
114
loggers.put("org.hibernate.SQL", dbLogger);
115
loggingFactory.setLoggers(loggers);
116
117
// Add appenders
118
List<AppenderFactory<ILoggingEvent>> appenders = new ArrayList<>();
119
ConsoleAppenderFactory<ILoggingEvent> consoleAppender = new ConsoleAppenderFactory<>();
120
appenders.add(consoleAppender);
121
loggingFactory.setAppenders(appenders);
122
123
// Configure the system
124
MetricRegistry metrics = new MetricRegistry();
125
loggingFactory.configure(metrics, "MyApp");
126
```
127
128
### ExternalLoggingFactory
129
130
No-op logging factory implementation for scenarios where logging is configured externally (e.g., via logback.xml).
131
132
```java { .api }
133
/**
134
* No-op logging factory for external logging configuration
135
*/
136
@JsonTypeName("external")
137
public class ExternalLoggingFactory implements LoggingFactory {
138
@Override
139
public void configure(MetricRegistry metricRegistry, String name) {
140
// No-op - logging configured externally
141
}
142
143
@Override
144
public void stop() {
145
// No-op
146
}
147
148
@Override
149
public void reset() {
150
// No-op
151
}
152
}
153
```
154
155
**Usage Example:**
156
157
```java
158
// Used when logging is configured via logback.xml or similar
159
ExternalLoggingFactory loggingFactory = new ExternalLoggingFactory();
160
MetricRegistry metrics = new MetricRegistry();
161
loggingFactory.configure(metrics, "MyApp"); // Does nothing
162
```
163
164
### LoggerConfiguration
165
166
Configuration class for individual logger settings allowing fine-grained control over specific loggers.
167
168
```java { .api }
169
/**
170
* Configuration for individual loggers
171
*/
172
public class LoggerConfiguration {
173
private String level = "INFO";
174
private List<AppenderFactory<ILoggingEvent>> appenders = new ArrayList<>();
175
private boolean additive = true;
176
177
/**
178
* Set the logging level for this logger
179
* @param level the logging level
180
*/
181
public void setLevel(String level);
182
183
/**
184
* Get the logging level
185
* @return the current logging level
186
*/
187
public String getLevel();
188
189
/**
190
* Set specific appenders for this logger
191
* @param appenders list of appender factories
192
*/
193
public void setAppenders(List<AppenderFactory<ILoggingEvent>> appenders);
194
195
/**
196
* Get the appenders for this logger
197
* @return list of appender factories
198
*/
199
public List<AppenderFactory<ILoggingEvent>> getAppenders();
200
201
/**
202
* Set whether this logger inherits appenders from parent loggers
203
* @param additive true to inherit parent appenders, false otherwise
204
*/
205
public void setAdditive(boolean additive);
206
207
/**
208
* Check if this logger inherits appenders from parent loggers
209
* @return true if additive, false otherwise
210
*/
211
public boolean isAdditive();
212
}
213
```
214
215
**Usage Example:**
216
217
```java
218
LoggerConfiguration dbLoggerConfig = new LoggerConfiguration();
219
dbLoggerConfig.setLevel("DEBUG");
220
dbLoggerConfig.setAdditive(false);
221
222
// Add specific appenders for database logging
223
FileAppenderFactory<ILoggingEvent> dbFileAppender = new FileAppenderFactory<>();
224
dbFileAppender.setCurrentLogFilename("./logs/database.log");
225
dbLoggerConfig.setAppenders(Arrays.asList(dbFileAppender));
226
```