Skip to content

Commit 87d259f

Browse files
feat: Allow customizing written log entries by exposing parameters via appender configuration (#625)
* feat: Allow customizing written log entries by exposing parameters via appender configuration * Add project id override in getLoggingOptions * Add tests * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Expose project ID based resource name support * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix comment * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Address PR comments * Fix README * Fix name in README * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Revert version for logback used * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix README * Revert 0.122.9-alpha reference * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add comment to partial README * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add extra fixes * Remove WriteOptions initialization Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent eee8612 commit 87d259f

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

.readme-partials.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ custom_content: |
2525
<!-- Optional: defaults to the default credentials of the environment -->
2626
<credentialsFile>/path/to/credentials/file</credentialsFile>
2727
28+
<!-- Optional: defaults to the project id obtained during authentication process. Project id is also used to construct resource name of the log entries -->
29+
<logDestinationProjectId>String</logDestinationProjectId>
30+
2831
<!-- Optional: add custom labels to log entries using LoggingEnhancer classes -->
2932
<enhancer>com.example.enhancers.TestLoggingEnhancer</enhancer>
3033
<enhancer>com.example.enhancers.AnotherEnhancer</enhancer>

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter
9696
<!-- Optional: defaults to the default credentials of the environment -->
9797
<credentialsFile>/path/to/credentials/file</credentialsFile>
9898

99+
<!-- Optional: defaults to the project id obtained during authentication process. Project id is also used to construct resource name of the log entries -->
100+
<logDestinationProjectId>String</logDestinationProjectId>
101+
99102
<!-- Optional: add custom labels to log entries using LoggingEnhancer classes -->
100103
<enhancer>com.example.enhancers.TestLoggingEnhancer</enhancer>
101104
<enhancer>com.example.enhancers.AnotherEnhancer</enhancer>

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
* &lt;!-- Optional: defaults to the default credentials of the environment --&gt;
7676
* &lt;credentialsFile&gt;/path/to/credentials/file&lt;/credentialsFile&gt;
7777
*
78+
* &lt;!-- Optional: defaults to the project id obtained during authentication process. Project id is also used to construct resource name of the log entries --&gt;
79+
* &lt;logDestinationProjectId&gt;String&lt;/logDestinationProjectId&gt;
80+
*
7881
* &lt;!-- Optional: add custom labels to log entries using {@link LoggingEnhancer} classes --&gt;
7982
* &lt;enhancer&gt;com.example.enhancers.TestLoggingEnhancer&lt/enhancer&gt;
8083
* &lt;enhancer&gt;com.example.enhancers.AnotherEnhancer&lt/enhancer&gt;
@@ -101,6 +104,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
101104
private String log;
102105
private String resourceType;
103106
private String credentialsFile;
107+
private String logDestinationProjectId;
104108
private Synchronicity writeSyncFlag = Synchronicity.ASYNC;
105109
private final Set<String> enhancerClassNames = new HashSet<>();
106110
private final Set<String> loggingEventEnhancerClassNames = new HashSet<>();
@@ -151,6 +155,16 @@ public void setCredentialsFile(String credentialsFile) {
151155
this.credentialsFile = credentialsFile;
152156
}
153157

158+
/**
159+
* Sets project ID to be used to customize log destination name for written log entries.
160+
*
161+
* @param projectId The project ID to be used to construct the resource destination name for log
162+
* entries.
163+
*/
164+
public void setLogDestinationProjectId(String projectId) {
165+
this.logDestinationProjectId = projectId;
166+
}
167+
154168
/**
155169
* Define synchronization mode for writing log entries.
156170
*
@@ -293,15 +307,12 @@ public void flush() {
293307
/** Gets the {@link LoggingOptions} to use for this {@link LoggingAppender}. */
294308
protected LoggingOptions getLoggingOptions() {
295309
if (loggingOptions == null) {
296-
if (Strings.isNullOrEmpty(credentialsFile)) {
297-
loggingOptions = LoggingOptions.getDefaultInstance();
298-
} else {
310+
LoggingOptions.Builder builder = LoggingOptions.newBuilder();
311+
builder.setProjectId(logDestinationProjectId);
312+
if (!Strings.isNullOrEmpty(credentialsFile)) {
299313
try {
300-
loggingOptions =
301-
LoggingOptions.newBuilder()
302-
.setCredentials(
303-
GoogleCredentials.fromStream(new FileInputStream(credentialsFile)))
304-
.build();
314+
builder.setCredentials(
315+
GoogleCredentials.fromStream(new FileInputStream(credentialsFile)));
305316
} catch (IOException e) {
306317
throw new RuntimeException(
307318
String.format(
@@ -310,6 +321,7 @@ protected LoggingOptions getLoggingOptions() {
310321
e);
311322
}
312323
}
324+
loggingOptions = builder.build();
313325
}
314326
return loggingOptions;
315327
}

src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
@RunWith(EasyMockRunner.class)
5151
public class LoggingAppenderTest {
5252
private final String projectId = "test-project";
53+
private final String credentialFileProjectId = "project-12345";
54+
private final String overridenProjectId = "some-project-id";
55+
private final String dummyCredentialsFile =
56+
"src/test/java/com/google/cloud/logging/logback/dummy-credentials.json";
5357
private Logging logging;
5458
private LoggingAppender loggingAppender;
5559

@@ -273,6 +277,23 @@ public void testCreateLoggingOptions() {
273277
}
274278
}
275279

280+
@Test
281+
public void testCreateLoggingOptionsWithCredentials() {
282+
// Try to build LoggingOptions with file based credentials.
283+
LoggingAppender appender = new LoggingAppender();
284+
appender.setCredentialsFile(dummyCredentialsFile);
285+
assertThat(appender.getLoggingOptions().getProjectId()).isEqualTo(credentialFileProjectId);
286+
}
287+
288+
@Test
289+
public void testCreateLoggingOptionsWithDestination() {
290+
// Try to build LoggingOptions with file based credentials.
291+
LoggingAppender appender = new LoggingAppender();
292+
appender.setCredentialsFile(dummyCredentialsFile);
293+
appender.setLogDestinationProjectId(overridenProjectId);
294+
assertThat(appender.getLoggingOptions().getProjectId()).isEqualTo(overridenProjectId);
295+
}
296+
276297
private LoggingEvent createLoggingEvent(Level level, long timestamp) {
277298
LoggingEvent loggingEvent = new LoggingEvent();
278299
loggingEvent.setMessage("this is a test");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "project-12345",
4+
"private_key_id": "12345",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKhPSTDs4cpKfnMc\np86fCkpnuER7bGc+mGkhkw6bE+BnROfrDCFBSjrENLS5JcsenANQ1kYGt9iVW2fd\nZAWUdDoj+t7g6+fDpzY1BzPSUls421Dmu7joDPY8jSdMzFCeg7Lyj0I36bJJ7ooD\nVPW6Q0XQcb8FfBiFPAKuY4elj/YDAgMBAAECgYBo2GMWmCmbM0aL/KjH/KiTawMN\nnfkMY6DbtK9/5LjADHSPKAt5V8ueygSvI7rYSiwToLKqEptJztiO3gnls/GmFzj1\nV/QEvFs6Ux3b0hD2SGpGy1m6NWWoAFlMISRkNiAxo+AMdCi4I1hpk4+bHr9VO2Bv\nV0zKFxmgn1R8qAR+4QJBANqKxJ/qJ5+lyPuDYf5s+gkZWjCLTC7hPxIJQByDLICw\niEnqcn0n9Gslk5ngJIGQcKBXIp5i0jWSdKN/hLxwgHECQQDFKGmo8niLzEJ5sa1r\nspww8Hc2aJM0pBwceshT8ZgVPnpgmITU1ENsKpJ+y1RTjZD6N0aj9gS9UB/UXdTr\nHBezAkEAqkDRTYOtusH9AXQpM3zSjaQijw72Gs9/wx1RxOSsFtVwV6U97CLkV1S+\n2HG1/vn3w/IeFiYGfZXLKFR/pA5BAQJAbFeu6IaGM9yFUzaOZDZ8mnAqMp349t6Q\nDB5045xJxLLWsSpfJE2Y12H1qvO1XUzYNIgXq5ZQOHBFbYA6txBy/QJBAKDRQN47\n6YClq9652X+1lYIY/h8MxKiXpVZVncXRgY6pbj4pmWEAM88jra9Wq6R77ocyECzi\nXCqi18A/sl6ymWc=\n-----END PRIVATE KEY-----\n",
6+
"client_email": "[email protected]",
7+
"client_id": "123456789012345678901",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/project-12345%40appspot.gserviceaccount.com"
12+
}

0 commit comments

Comments
 (0)