Skip to content

Commit 667a4d0

Browse files
committed
updating mock framework
enhancers now individual xml elements code cleanup based on review
1 parent 0d55036 commit 667a4d0

5 files changed

Lines changed: 231 additions & 160 deletions

File tree

google-cloud-contrib/google-cloud-logging-logback/README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Logback Appender for Google Cloud Logging
44
Logback appender `com.google.cloud.logging.logback.LoggingAppender` for
55
[Google Cloud Logging](https://cloud.google.com/logging/).
66

7-
This library allows you to use [Logback](https://logback.qos.ch/) configuration and the [Slf4j](https://www.slf4j.org/) interface to log via Google Cloud logging
7+
This library allows you to use [Logback](https://logback.qos.ch/) configuration and the [Slf4j](https://www.slf4j.org/) interface to log via Google Cloud logging.
88

99
- [Homepage](https://googlecloudplatform.github.io/google-cloud-java/)
10-
- [API Documentation](https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/logging/package-summary.html)
10+
- [API Documentation](https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/logging/logback/LoggingAppender.html)
1111

1212
> Note: This client is a work-in-progress, and may occasionally
1313
> make backwards-incompatible changes.
@@ -19,35 +19,36 @@ If you are using Maven, add this to your pom.xml file
1919
<dependency>
2020
<groupId>com.google.cloud</groupId>
2121
<artifactId>google-cloud-logging-logback</artifactId>
22-
<version>0.17.1-alpha</version>
22+
<version>0.17.2-alpha</version>
2323
</dependency>
2424
```
2525
If you are using Gradle, add this to your dependencies
2626
```Groovy
27-
compile 'com.google.cloud:google-cloud-logging-logback:0.17.1-alpha'
27+
compile 'com.google.cloud:google-cloud-logging-logback:0.17.2-alpha'
2828
```
2929
If you are using SBT, add this to your dependencies
3030
```Scala
31-
libraryDependencies += "com.google.cloud" % "google-cloud-logging-logback" % "0.17.1-alpha"
31+
libraryDependencies += "com.google.cloud" % "google-cloud-logging-logback" % "0.17.2-alpha"
3232
```
3333

3434
Usage
3535
-----
3636

3737
Add the appender to your [Logback configuration](https://logback.qos.ch/manual/configuration.html) `logback.xml`.
38-
See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter) on filtering log output and
39-
[encoders](https://logback.qos.ch/manual/encoders.html) on formatting.
38+
See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter) for information on filtering log output and
39+
[encoders](https://logback.qos.ch/manual/encoders.html) for information on formatting.
4040

4141

4242
```xml
4343
<configuration>
4444
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
4545
<!-- Optional : filter logs at or above a level -->
4646
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
47-
<level>INFO</level>
48-
</filter>
47+
<level>INFO</level>
48+
</filter>
4949
<log>application.log</log> <!-- Optional : default java.log -->
50-
<enhancers>com.example.enhancers.TestLoggingEnhancer</enhancers> <!-- Optional -->
50+
<enhancer>com.example.enhancers.TestLoggingEnhancer</enhancer> <!-- Optional -->
51+
<enhancer>com.example.enhancers.AnotherEnhancer</enhancer> <!-- Optional -->
5152
<flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
5253
</appender>
5354

@@ -89,7 +90,7 @@ Limitations
8990
-----------
9091

9192
This library is usable, but not yet complete.
92-
* Auto-reload of properties is not fully tested
93+
Open issues [here](https://github.com/GoogleCloudPlatform/google-cloud-java/issues?q=is%3Aissue+is%3Aopen+label%3A%22logging-logback%22).
9394

9495

9596
Java Versions
Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
<artifactId>google-cloud-logging-logback</artifactId>
7-
<version>0.17.3-alpha-SNAPSHOT</version>
8-
<packaging>jar</packaging>
9-
<name>Google Cloud Logging Logback Appender</name>
10-
<url>https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-logging-logback</url>
11-
<description>
12-
Logback support for Google Cloud Logging
13-
</description>
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>google-cloud-logging-logback</artifactId>
8+
<version>0.17.3-alpha-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>Google Cloud Logging Logback Appender</name>
11+
<url>https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-logging-logback</url>
12+
<description>
13+
Logback support for Google Cloud Logging
14+
</description>
1415

15-
<properties>
16-
<logback.version>1.2.3</logback.version>
17-
</properties>
16+
<properties>
17+
<logback.version>1.2.3</logback.version>
18+
</properties>
1819

19-
<parent>
20-
<groupId>com.google.cloud</groupId>
21-
<artifactId>google-cloud-contrib</artifactId>
22-
<version>0.17.3-alpha-SNAPSHOT</version>
23-
</parent>
24-
<dependencies>
25-
<dependency>
26-
<groupId>ch.qos.logback</groupId>
27-
<artifactId>logback-classic</artifactId>
28-
<version>${logback.version}</version>
29-
</dependency>
30-
<dependency>
31-
<groupId>com.google.cloud</groupId>
32-
<artifactId>google-cloud-logging</artifactId>
33-
</dependency>
34-
<!-- Test dependencies -->
35-
<dependency>
36-
<groupId>org.mockito</groupId>
37-
<artifactId>mockito-core</artifactId>
38-
<version>2.5.5</version>
39-
<scope>test</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>junit</groupId>
43-
<artifactId>junit</artifactId>
44-
<version>4.12</version>
45-
<scope>test</scope>
46-
</dependency>
47-
</dependencies>
20+
<parent>
21+
<groupId>com.google.cloud</groupId>
22+
<artifactId>google-cloud-contrib</artifactId>
23+
<version>0.17.3-alpha-SNAPSHOT</version>
24+
</parent>
25+
<dependencies>
26+
<dependency>
27+
<groupId>ch.qos.logback</groupId>
28+
<artifactId>logback-classic</artifactId>
29+
<version>${logback.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.google.cloud</groupId>
33+
<artifactId>google-cloud-logging</artifactId>
34+
</dependency>
35+
<!-- Test dependencies -->
36+
<dependency>
37+
<groupId>org.easymock</groupId>
38+
<artifactId>easymock</artifactId>
39+
<version>3.4</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>junit</groupId>
44+
<artifactId>junit</artifactId>
45+
<version>4.12</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
4849
</project>

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

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,30 @@
3131

3232
import java.util.ArrayList;
3333
import java.util.Collections;
34+
import java.util.HashSet;
3435
import java.util.List;
36+
import java.util.Set;
3537

3638
/**
3739
* <a href="https://logback.qos.ch/">Logback</a> appender for StackDriver Cloud Logging.
3840
*
3941
* <p>Appender configuration in logback.xml:
42+
*
4043
* <ul>
41-
* <li>&lt;appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender"&gt;</li>
42-
* <li>&lt;log&gt;application.log&lt;/log&gt; (Optional, defaults to "java.log" : Stackdriver log name)</li>
43-
* <li>&lt;level&gt;ERROR&lt;/level&gt; (Optional, defaults to "INFO" : logs at or above this level)</li>
44-
* <li>&lt;flushLevel&gt;WARNING&lt;/flushLevel&gt; (Optional, defaults to "ERROR")</li>
45-
* <li>&lt;resourceType&gt;&lt;/resourceType&gt; (Optional, auto detects on App Engine Flex, Standard, GCE and
46-
* GKE, defaults to "global".
47-
* See <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource
48-
* types</a></li>
49-
* <li>&lt;enhancers&gt;com.example.enhancer1, com.example.enhancer2&lt;/enhancers&gt;</li>
50-
* <li>(Optional) add custom labels to log entries using {@link LoggingEnhancer} classes.</li>
51-
* <li>&lt;/appender&gt;</li>
44+
* <li>&lt;appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender"&gt;
45+
* <li>&lt;log&gt;application.log&lt;/log&gt; (Optional, defaults to "java.log" : Stackdriver log
46+
* name)
47+
* <li>&lt;level&gt;ERROR&lt;/level&gt; (Optional, defaults to "INFO" : logs at or above this
48+
* level)
49+
* <li>&lt;flushLevel&gt;WARNING&lt;/flushLevel&gt; (Optional, defaults to "ERROR")
50+
* <li>&lt;resourceType&gt;&lt;/resourceType&gt; (Optional, auto detects on App Engine Flex,
51+
* Standard, GCE and GKE, defaults to "global". See <a
52+
* href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported resource
53+
* types</a>
54+
* <li>(Optional) add custom labels to log entries using {@link LoggingEnhancer} classes.
55+
* <li>&lt;enhancer&gt;com.example.enhancer1&lt;/enhancer&gt;
56+
* <li>&lt;enhancer&gt;com.example.enhancer2&lt;/enhancer&gt;
57+
* <li>&lt;/appender&gt;
5258
* </ul>
5359
*/
5460
public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
@@ -63,10 +69,10 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
6369
private Level flushLevel;
6470
private String log;
6571
private String resourceType;
66-
private String enhancers;
72+
private Set<String> enhancerClassNames = new HashSet<>();
6773

6874
/**
69-
* Batched logging requests get immediately flushed for logs at or above level.
75+
* Batched logging requests get immediately flushed for logs at or above this level.
7076
*
7177
* <p>Defaults to Error if not set.
7278
*
@@ -77,7 +83,7 @@ public void setFlushLevel(Level flushLevel) {
7783
}
7884

7985
/**
80-
* Sets the log filename, defaults to java.log.
86+
* Sets the log filename.
8187
*
8288
* @param log filename
8389
*/
@@ -89,21 +95,21 @@ public void setLog(String log) {
8995
* Sets the name of the monitored resource (Optional).
9096
*
9197
* <p>Must be a <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">supported</a>
92-
* resource type. gae_app, gce_instance and container are auto-detected, defaults to "global"
98+
* resource type. gae_app, gce_instance and container are auto-detected.
99+
*
100+
* <p>Defaults to "global"
93101
*
94-
* @param resourceType name of monitored resource
102+
* @param resourceType name of the monitored resource
95103
*/
96104
public void setResourceType(String resourceType) {
97105
this.resourceType = resourceType;
98106
}
99107

100108
/**
101109
* Add extra labels using classes that implement {@link LoggingEnhancer}.
102-
*
103-
* @param enhancers list of enhancer classnames (comma separated)
104110
*/
105-
public void setEnhancers(String enhancers) {
106-
this.enhancers = enhancers;
111+
public void addEnhancer(String enhancerClassName) {
112+
this.enhancerClassNames.add(enhancerClassName);
107113
}
108114

109115
Level getFlushLevel() {
@@ -118,45 +124,53 @@ MonitoredResource getMonitoredResource(String projectId) {
118124
return MonitoredResourceUtil.getResource(projectId, resourceType);
119125
}
120126

121-
List<LoggingEnhancer> getEnhancers() {
122-
String list = enhancers;
123-
try {
124-
List<LoggingEnhancer> enhancers = new ArrayList<>();
125-
if (list != null) {
126-
String[] items = list.split(",");
127-
for (String enhancerName : items) {
128-
Class<? extends LoggingEnhancer> clz =
129-
(Class<? extends LoggingEnhancer>)
130-
ClassLoader.getSystemClassLoader().loadClass(enhancerName.trim());
131-
enhancers.add(clz.newInstance());
127+
List<LoggingEnhancer> getLoggingEnhancers() {
128+
List<LoggingEnhancer> loggingEnhancers = new ArrayList<>();
129+
if (enhancerClassNames != null) {
130+
for (String enhancerClassName : enhancerClassNames) {
131+
if (enhancerClassName != null) {
132+
LoggingEnhancer enhancer = getEnhancer(enhancerClassName);
133+
if (enhancer != null) {
134+
loggingEnhancers.add(enhancer);
135+
}
132136
}
133137
}
134-
return enhancers;
138+
}
139+
return loggingEnhancers;
140+
}
141+
142+
private LoggingEnhancer getEnhancer(String enhancerClassName) {
143+
try {
144+
Class<? extends LoggingEnhancer> clz =
145+
(Class<? extends LoggingEnhancer>)
146+
ClassLoader.getSystemClassLoader().loadClass(enhancerClassName.trim());
147+
return clz.newInstance();
135148
} catch (Exception ex) {
136-
// If we cannot create the enhancers we fall back to the default
149+
// If we cannot create the enhancer we fallback to null
137150
}
138-
return Collections.emptyList();
151+
return null;
139152
}
140153

141-
/** Initialze and configure the cloud logging service. */
154+
/**
155+
* Initialize and configure the cloud logging service.
156+
*/
142157
@Override
143158
public synchronized void start() {
159+
if (isStarted()) {
160+
return;
161+
}
144162
MonitoredResource resource = getMonitoredResource(getProjectId());
145163
defaultWriteOptions =
146-
new WriteOption[] {
147-
WriteOption.logName(getLogName()),
148-
WriteOption.resource(resource)
149-
};
150-
164+
new WriteOption[]{WriteOption.logName(getLogName()), WriteOption.resource(resource)};
151165
getLogging().setFlushSeverity(severityFor(getFlushLevel()));
152166
loggingEnhancers = new ArrayList<>();
153167
List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers();
154168
loggingEnhancers.addAll(resourceEnhancers);
155-
loggingEnhancers.addAll(getEnhancers());
169+
loggingEnhancers.addAll(getLoggingEnhancers());
156170
super.start();
157171
}
158172

159-
protected String getProjectId() {
173+
String getProjectId() {
160174
return LoggingOptions.getDefaultInstance().getProjectId();
161175
}
162176

@@ -179,7 +193,7 @@ public synchronized void stop() {
179193
super.stop();
180194
}
181195

182-
protected Logging getLogging() {
196+
Logging getLogging() {
183197
if (logging == null) {
184198
synchronized (this) {
185199
if (logging == null) {
@@ -198,7 +212,8 @@ private LogEntry logEntryFor(ILoggingEvent e) {
198212
.setTimestamp(e.getTimeStamp())
199213
.setSeverity(severityFor(level));
200214

201-
builder.addLabel(LEVEL_NAME_KEY, level.toString())
215+
builder
216+
.addLabel(LEVEL_NAME_KEY, level.toString())
202217
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()));
203218

204219
if (loggingEnhancers != null) {
@@ -218,19 +233,19 @@ private LogEntry logEntryFor(ILoggingEvent e) {
218233
*/
219234
private static Severity severityFor(Level level) {
220235
switch (level.toInt()) {
221-
// TRACE
236+
// TRACE
222237
case 5000:
223238
return Severity.DEBUG;
224-
// DEBUG
239+
// DEBUG
225240
case 10000:
226241
return Severity.DEBUG;
227-
// INFO
242+
// INFO
228243
case 20000:
229244
return Severity.INFO;
230-
// WARNING
245+
// WARNING
231246
case 30000:
232247
return Severity.WARNING;
233-
// ERROR
248+
// ERROR
234249
case 40000:
235250
return Severity.ERROR;
236251
default:

google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/example/enhancers/AnotherTestLoggingEnhancer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package com.example.enhancers;
1718

1819
import com.google.cloud.logging.LogEntry;

0 commit comments

Comments
 (0)