Skip to content

Commit

Permalink
feat: Allow customizing written log entries by exposing parameters vi…
Browse files Browse the repository at this point in the history
…a 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>
  • Loading branch information
losalex and gcf-owl-bot[bot] committed Jan 14, 2022
1 parent eee8612 commit 87d259f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .readme-partials.yaml
Expand Up @@ -25,6 +25,9 @@ custom_content: |
<!-- Optional: defaults to the default credentials of the environment -->
<credentialsFile>/path/to/credentials/file</credentialsFile>
<!-- Optional: defaults to the project id obtained during authentication process. Project id is also used to construct resource name of the log entries -->
<logDestinationProjectId>String</logDestinationProjectId>
<!-- Optional: add custom labels to log entries using LoggingEnhancer classes -->
<enhancer>com.example.enhancers.TestLoggingEnhancer</enhancer>
<enhancer>com.example.enhancers.AnotherEnhancer</enhancer>
Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,9 @@ See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter
<!-- Optional: defaults to the default credentials of the environment -->
<credentialsFile>/path/to/credentials/file</credentialsFile>

<!-- Optional: defaults to the project id obtained during authentication process. Project id is also used to construct resource name of the log entries -->
<logDestinationProjectId>String</logDestinationProjectId>

<!-- Optional: add custom labels to log entries using LoggingEnhancer classes -->
<enhancer>com.example.enhancers.TestLoggingEnhancer</enhancer>
<enhancer>com.example.enhancers.AnotherEnhancer</enhancer>
Expand Down
Expand Up @@ -75,6 +75,9 @@
* &lt;!-- Optional: defaults to the default credentials of the environment --&gt;
* &lt;credentialsFile&gt;/path/to/credentials/file&lt;/credentialsFile&gt;
*
* &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;
* &lt;logDestinationProjectId&gt;String&lt;/logDestinationProjectId&gt;
*
* &lt;!-- Optional: add custom labels to log entries using {@link LoggingEnhancer} classes --&gt;
* &lt;enhancer&gt;com.example.enhancers.TestLoggingEnhancer&lt/enhancer&gt;
* &lt;enhancer&gt;com.example.enhancers.AnotherEnhancer&lt/enhancer&gt;
Expand All @@ -101,6 +104,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private String log;
private String resourceType;
private String credentialsFile;
private String logDestinationProjectId;
private Synchronicity writeSyncFlag = Synchronicity.ASYNC;
private final Set<String> enhancerClassNames = new HashSet<>();
private final Set<String> loggingEventEnhancerClassNames = new HashSet<>();
Expand Down Expand Up @@ -151,6 +155,16 @@ public void setCredentialsFile(String credentialsFile) {
this.credentialsFile = credentialsFile;
}

/**
* Sets project ID to be used to customize log destination name for written log entries.
*
* @param projectId The project ID to be used to construct the resource destination name for log
* entries.
*/
public void setLogDestinationProjectId(String projectId) {
this.logDestinationProjectId = projectId;
}

/**
* Define synchronization mode for writing log entries.
*
Expand Down Expand Up @@ -293,15 +307,12 @@ public void flush() {
/** Gets the {@link LoggingOptions} to use for this {@link LoggingAppender}. */
protected LoggingOptions getLoggingOptions() {
if (loggingOptions == null) {
if (Strings.isNullOrEmpty(credentialsFile)) {
loggingOptions = LoggingOptions.getDefaultInstance();
} else {
LoggingOptions.Builder builder = LoggingOptions.newBuilder();
builder.setProjectId(logDestinationProjectId);
if (!Strings.isNullOrEmpty(credentialsFile)) {
try {
loggingOptions =
LoggingOptions.newBuilder()
.setCredentials(
GoogleCredentials.fromStream(new FileInputStream(credentialsFile)))
.build();
builder.setCredentials(
GoogleCredentials.fromStream(new FileInputStream(credentialsFile)));
} catch (IOException e) {
throw new RuntimeException(
String.format(
Expand All @@ -310,6 +321,7 @@ protected LoggingOptions getLoggingOptions() {
e);
}
}
loggingOptions = builder.build();
}
return loggingOptions;
}
Expand Down
Expand Up @@ -50,6 +50,10 @@
@RunWith(EasyMockRunner.class)
public class LoggingAppenderTest {
private final String projectId = "test-project";
private final String credentialFileProjectId = "project-12345";
private final String overridenProjectId = "some-project-id";
private final String dummyCredentialsFile =
"src/test/java/com/google/cloud/logging/logback/dummy-credentials.json";
private Logging logging;
private LoggingAppender loggingAppender;

Expand Down Expand Up @@ -273,6 +277,23 @@ public void testCreateLoggingOptions() {
}
}

@Test
public void testCreateLoggingOptionsWithCredentials() {
// Try to build LoggingOptions with file based credentials.
LoggingAppender appender = new LoggingAppender();
appender.setCredentialsFile(dummyCredentialsFile);
assertThat(appender.getLoggingOptions().getProjectId()).isEqualTo(credentialFileProjectId);
}

@Test
public void testCreateLoggingOptionsWithDestination() {
// Try to build LoggingOptions with file based credentials.
LoggingAppender appender = new LoggingAppender();
appender.setCredentialsFile(dummyCredentialsFile);
appender.setLogDestinationProjectId(overridenProjectId);
assertThat(appender.getLoggingOptions().getProjectId()).isEqualTo(overridenProjectId);
}

private LoggingEvent createLoggingEvent(Level level, long timestamp) {
LoggingEvent loggingEvent = new LoggingEvent();
loggingEvent.setMessage("this is a test");
Expand Down
@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "project-12345",
"private_key_id": "12345",
"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",
"client_email": "project-12345@appspot.gserviceaccount.com",
"client_id": "123456789012345678901",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/project-12345%40appspot.gserviceaccount.com"
}

0 comments on commit 87d259f

Please sign in to comment.