Skip to content

Commit 57d148c

Browse files
committed
Add interface for enhancing Logback events
1 parent f2f910f commit 57d148c

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
6565

6666
private volatile Logging logging;
6767
private List<LoggingEnhancer> loggingEnhancers;
68+
private List<LoggingEventEnhancer> loggingEventEnhancers;
6869
private WriteOption[] defaultWriteOptions;
6970

7071
private Level flushLevel;
7172
private String log;
7273
private String resourceType;
7374
private Set<String> enhancerClassNames = new HashSet<>();
75+
private Set<String> loggingEventEnhancerClassNames = new HashSet<>();
7476

7577
/**
7678
* Batched logging requests get immediately flushed for logs at or above this level.
@@ -112,6 +114,11 @@ public void setResourceType(String resourceType) {
112114
public void addEnhancer(String enhancerClassName) {
113115
this.enhancerClassNames.add(enhancerClassName);
114116
}
117+
118+
public void addLoggingEventEnhancer(String enhancerClassName) {
119+
this.loggingEventEnhancerClassNames.add(enhancerClassName);
120+
}
121+
115122

116123
Level getFlushLevel() {
117124
return (flushLevel != null) ? flushLevel : Level.ERROR;
@@ -126,11 +133,19 @@ MonitoredResource getMonitoredResource(String projectId) {
126133
}
127134

128135
List<LoggingEnhancer> getLoggingEnhancers() {
129-
List<LoggingEnhancer> loggingEnhancers = new ArrayList<>();
130-
if (enhancerClassNames != null) {
131-
for (String enhancerClassName : enhancerClassNames) {
136+
return getEnhancers(enhancerClassNames);
137+
}
138+
139+
List<LoggingEventEnhancer> getLoggingEventEnhancers() {
140+
return getEnhancers(loggingEventEnhancerClassNames);
141+
}
142+
143+
<T> List<T> getEnhancers(Set<String> classNames) {
144+
List<T> loggingEnhancers = new ArrayList<>();
145+
if (classNames != null) {
146+
for (String enhancerClassName : classNames) {
132147
if (enhancerClassName != null) {
133-
LoggingEnhancer enhancer = getEnhancer(enhancerClassName);
148+
T enhancer = getEnhancer(enhancerClassName);
134149
if (enhancer != null) {
135150
loggingEnhancers.add(enhancer);
136151
}
@@ -140,10 +155,10 @@ List<LoggingEnhancer> getLoggingEnhancers() {
140155
return loggingEnhancers;
141156
}
142157

143-
private LoggingEnhancer getEnhancer(String enhancerClassName) {
158+
private <T> T getEnhancer(String enhancerClassName) {
144159
try {
145-
Class<? extends LoggingEnhancer> clz =
146-
(Class<? extends LoggingEnhancer>)
160+
Class<T> clz =
161+
(Class<T>)
147162
Loader.loadClass(enhancerClassName.trim());
148163
return clz.newInstance();
149164
} catch (Exception ex) {
@@ -168,6 +183,9 @@ public synchronized void start() {
168183
List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers();
169184
loggingEnhancers.addAll(resourceEnhancers);
170185
loggingEnhancers.addAll(getLoggingEnhancers());
186+
loggingEventEnhancers = new ArrayList<>();
187+
loggingEventEnhancers.addAll(getLoggingEventEnhancers());
188+
171189
super.start();
172190
}
173191

@@ -223,6 +241,12 @@ private LogEntry logEntryFor(ILoggingEvent e) {
223241
}
224242
}
225243

244+
if (loggingEventEnhancers != null) {
245+
for (LoggingEventEnhancer enhancer : loggingEventEnhancers) {
246+
enhancer.enhanceLogEntry(builder, e);
247+
}
248+
}
249+
226250
return builder.build();
227251
}
228252

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2017 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging.logback;
18+
19+
import com.google.cloud.logging.LogEntry;
20+
21+
import ch.qos.logback.classic.spi.ILoggingEvent;
22+
23+
/**
24+
* An enhancer for {@linkplain ILoggingEvent} log entries.
25+
* Used to add custom labels to the {@link LogEntry.Builder}.
26+
*/
27+
public interface LoggingEventEnhancer {
28+
void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent e);
29+
}

0 commit comments

Comments
 (0)