Skip to content

Commit ad25bee

Browse files
skjolberpongad
authored andcommitted
---
yaml --- r: 9129 b: refs/heads/master c: ccb39e8 h: refs/heads/master i: 9127: e596634
1 parent 7c1ac7b commit ad25bee

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 4170d856cc0edd9621e539f4377d727d0e61299a
2+
refs/heads/master: ccb39e86e26e2a51661f0cf0917515d79c73b565
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 8e9b065ba06cd7a4af306aaea1010aade81670e0
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

trunk/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
@@ -67,12 +67,14 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
6767

6868
private volatile Logging logging;
6969
private List<LoggingEnhancer> loggingEnhancers;
70+
private List<LoggingEventEnhancer> loggingEventEnhancers;
7071
private WriteOption[] defaultWriteOptions;
7172

7273
private Level flushLevel;
7374
private String log;
7475
private String resourceType;
7576
private Set<String> enhancerClassNames = new HashSet<>();
77+
private Set<String> loggingEventEnhancerClassNames = new HashSet<>();
7678

7779
/**
7880
* Batched logging requests get immediately flushed for logs at or above this level.
@@ -114,6 +116,11 @@ public void setResourceType(String resourceType) {
114116
public void addEnhancer(String enhancerClassName) {
115117
this.enhancerClassNames.add(enhancerClassName);
116118
}
119+
120+
public void addLoggingEventEnhancer(String enhancerClassName) {
121+
this.loggingEventEnhancerClassNames.add(enhancerClassName);
122+
}
123+
117124

118125
Level getFlushLevel() {
119126
return (flushLevel != null) ? flushLevel : Level.ERROR;
@@ -128,11 +135,19 @@ MonitoredResource getMonitoredResource(String projectId) {
128135
}
129136

130137
List<LoggingEnhancer> getLoggingEnhancers() {
131-
List<LoggingEnhancer> loggingEnhancers = new ArrayList<>();
132-
if (enhancerClassNames != null) {
133-
for (String enhancerClassName : enhancerClassNames) {
138+
return getEnhancers(enhancerClassNames);
139+
}
140+
141+
List<LoggingEventEnhancer> getLoggingEventEnhancers() {
142+
return getEnhancers(loggingEventEnhancerClassNames);
143+
}
144+
145+
<T> List<T> getEnhancers(Set<String> classNames) {
146+
List<T> loggingEnhancers = new ArrayList<>();
147+
if (classNames != null) {
148+
for (String enhancerClassName : classNames) {
134149
if (enhancerClassName != null) {
135-
LoggingEnhancer enhancer = getEnhancer(enhancerClassName);
150+
T enhancer = getEnhancer(enhancerClassName);
136151
if (enhancer != null) {
137152
loggingEnhancers.add(enhancer);
138153
}
@@ -142,10 +157,10 @@ List<LoggingEnhancer> getLoggingEnhancers() {
142157
return loggingEnhancers;
143158
}
144159

145-
private LoggingEnhancer getEnhancer(String enhancerClassName) {
160+
private <T> T getEnhancer(String enhancerClassName) {
146161
try {
147-
Class<? extends LoggingEnhancer> clz =
148-
(Class<? extends LoggingEnhancer>)
162+
Class<T> clz =
163+
(Class<T>)
149164
Loader.loadClass(enhancerClassName.trim());
150165
return clz.newInstance();
151166
} catch (Exception ex) {
@@ -170,6 +185,9 @@ public synchronized void start() {
170185
List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers();
171186
loggingEnhancers.addAll(resourceEnhancers);
172187
loggingEnhancers.addAll(getLoggingEnhancers());
188+
loggingEventEnhancers = new ArrayList<>();
189+
loggingEventEnhancers.addAll(getLoggingEventEnhancers());
190+
173191
super.start();
174192
}
175193

@@ -227,6 +245,12 @@ private LogEntry logEntryFor(ILoggingEvent e) {
227245
}
228246
}
229247

248+
if (loggingEventEnhancers != null) {
249+
for (LoggingEventEnhancer enhancer : loggingEventEnhancers) {
250+
enhancer.enhanceLogEntry(builder, e);
251+
}
252+
}
253+
230254
return builder.build();
231255
}
232256

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)