Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

private volatile Logging logging;
private List<LoggingEnhancer> loggingEnhancers;
private List<LoggingEventEnhancer> loggingEventEnhancers;
private WriteOption[] defaultWriteOptions;

private Level flushLevel;
private String log;
private String resourceType;
private Set<String> enhancerClassNames = new HashSet<>();
private Set<String> loggingEventEnhancerClassNames = new HashSet<>();

/**
* Batched logging requests get immediately flushed for logs at or above this level.
Expand Down Expand Up @@ -114,6 +116,11 @@ public void setResourceType(String resourceType) {
public void addEnhancer(String enhancerClassName) {
this.enhancerClassNames.add(enhancerClassName);
}

public void addLoggingEventEnhancer(String enhancerClassName) {
this.loggingEventEnhancerClassNames.add(enhancerClassName);
}


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

List<LoggingEnhancer> getLoggingEnhancers() {
List<LoggingEnhancer> loggingEnhancers = new ArrayList<>();
if (enhancerClassNames != null) {
for (String enhancerClassName : enhancerClassNames) {
return getEnhancers(enhancerClassNames);
}

List<LoggingEventEnhancer> getLoggingEventEnhancers() {
return getEnhancers(loggingEventEnhancerClassNames);
}

<T> List<T> getEnhancers(Set<String> classNames) {
List<T> loggingEnhancers = new ArrayList<>();
if (classNames != null) {
for (String enhancerClassName : classNames) {
if (enhancerClassName != null) {
LoggingEnhancer enhancer = getEnhancer(enhancerClassName);
T enhancer = getEnhancer(enhancerClassName);
if (enhancer != null) {
loggingEnhancers.add(enhancer);
}
Expand All @@ -142,10 +157,10 @@ List<LoggingEnhancer> getLoggingEnhancers() {
return loggingEnhancers;
}

private LoggingEnhancer getEnhancer(String enhancerClassName) {
private <T> T getEnhancer(String enhancerClassName) {
try {
Class<? extends LoggingEnhancer> clz =
(Class<? extends LoggingEnhancer>)
Class<T> clz =
(Class<T>)
Loader.loadClass(enhancerClassName.trim());
return clz.newInstance();
} catch (Exception ex) {
Expand All @@ -170,6 +185,9 @@ public synchronized void start() {
List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers();
loggingEnhancers.addAll(resourceEnhancers);
loggingEnhancers.addAll(getLoggingEnhancers());
loggingEventEnhancers = new ArrayList<>();
loggingEventEnhancers.addAll(getLoggingEventEnhancers());

super.start();
}

Expand Down Expand Up @@ -227,6 +245,12 @@ private LogEntry logEntryFor(ILoggingEvent e) {
}
}

if (loggingEventEnhancers != null) {
for (LoggingEventEnhancer enhancer : loggingEventEnhancers) {
enhancer.enhanceLogEntry(builder, e);
}
}

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.logging.logback;

import com.google.cloud.logging.LogEntry;

import ch.qos.logback.classic.spi.ILoggingEvent;

/**
* An enhancer for {@linkplain ILoggingEvent} log entries.
* Used to add custom labels to the {@link LogEntry.Builder}.
*/
public interface LoggingEventEnhancer {
void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent e);
}