Skip to content

Commit 6530782

Browse files
feat: enable auto-population of missing metadata in logs and printing structured logs to stdout (#654)
* feat: add support for metadata auto-population and redirection to STDOUT (#649) Add configuration to opt-out metadata auto-population. Add configuration to redirect appender output to stdout. Add relevant unit tests. Refactor unit tests. Improve javadoc comments for methods. Remove warnings. Make methods with no modifier to be private. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8f99e75 commit 6530782

File tree

6 files changed

+411
-258
lines changed

6 files changed

+411
-258
lines changed

.readme-partials.yaml

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ custom_content: |
1919
<!-- Optional: defaults to "ERROR" -->
2020
<flushLevel>WARNING</flushLevel>
2121
22-
<!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See supported resource types -->
22+
<!-- Optional: defaults to ASYNC -->
23+
<writeSynchronicity>SYNC</writeSynchronicity>
24+
25+
<!-- Optional: defaults to true -->
26+
<autoPopulateMetadata>false</autoPopulateMetadata>
27+
28+
<!-- Optional: defaults to false -->
29+
<redirectToStdout>true</redirectToStdout>
30+
31+
<!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". -->
2332
<resourceType></resourceType>
2433
2534
<!-- Optional: defaults to the default credentials of the environment -->
@@ -39,7 +48,7 @@ custom_content: |
3948
</configuration>
4049
```
4150
42-
In your code :
51+
In your code:
4352
4453
```java
4554
import org.slf4j.Logger;
@@ -57,4 +66,31 @@ custom_content: |
5766
testLogger.log("test");
5867
}
5968
}
60-
```
69+
```
70+
71+
### Populate log entries with metadata
72+
73+
The library provides multiple ways to enrich log entries with additional information.
74+
You can select one or more ways to customize log entries that will be written into Cloud Logging.
75+
76+
* Manually update a log entry using [`LoggingEnhancer`]
77+
(https://github.com/googleapis/java-logging/blob/main/google-cloud-logging/src/main/java/com/google/cloud/logging/.
78+
* Use [`ILoggingEvent`](https://logback.qos.ch/apidocs/ch/qos/logback/classic/spi/ILoggingEvent.html) to update a log entry with [`LoggingEventEnhancer`]
79+
(https://github.com/googleapis/java-logging-logback/blob/main/src/main/java/com/google/cloud/logging/logback/LoggingEventEnhancer.java).
80+
* Enable [auto-population](https://github.com/googleapis/java-logging#auto-population-of-log-entrys-metadata) of the `LogEntry` metadata using the `autoPopulateMetadata` configuration flag.
81+
82+
#### Optimize log ingestion
83+
84+
By default, the appender will ingest log entries asynchronously by calling Logging API.
85+
Multiple calls may be aggregated before being sent to improve use of API quota and bandwidth.
86+
You can set the `writeSynchronicity` configuration flag to `SYNC` if they want to ingest log entries synchronously.
87+
Note that configuring synchronous ingestion will probably result in performance penalties to your applications.
88+
If you plan to deploy your application in one of Google Cloud managed environments (e.g. Cloud Run, Cloud Function or App Engine),
89+
you can leverage the support provided by the implicit logging agent and the [structured logging](https://cloud.google.com/logging/docs/structured-logging) feature.
90+
To use it, set the `redirectToStdout` configuration flag to `true`.
91+
This flag instructs the appender to print the log entries to `stdout` instead of ingesting them using Logging API.
92+
The log entries are printed using the [structured logging Json format](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields).
93+
In result, the logging agent will be responsible for ingesting the logs to Logging API.
94+
Note that using the structured logging Json format you cannot control the log name where the logs will be ingested.
95+
The logs will be ingested into the project that hosts the environment where your application is running.
96+
The configuration `logDestinationProjectId` will be ignored.

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter
9090
<!-- Optional: defaults to "ERROR" -->
9191
<flushLevel>WARNING</flushLevel>
9292

93-
<!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See supported resource types -->
93+
<!-- Optional: defaults to ASYNC -->
94+
<writeSynchronicity>SYNC</writeSynchronicity>
95+
96+
<!-- Optional: defaults to true -->
97+
<autoPopulateMetadata>false</autoPopulateMetadata>
98+
99+
<!-- Optional: defaults to false -->
100+
<redirectToStdout>true</redirectToStdout>
101+
102+
<!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". -->
94103
<resourceType></resourceType>
95104

96105
<!-- Optional: defaults to the default credentials of the environment -->
@@ -110,7 +119,7 @@ See [Logback filters](https://logback.qos.ch/manual/filters.html#thresholdFilter
110119
</configuration>
111120
```
112121

113-
In your code :
122+
In your code:
114123

115124
```java
116125
import org.slf4j.Logger;
@@ -130,6 +139,34 @@ public class TestLogger {
130139
}
131140
```
132141

142+
### Populate log entries with metadata
143+
144+
The library provides multiple ways to enrich log entries with additional information.
145+
You can select one or more ways to customize log entries that will be written into Cloud Logging.
146+
147+
* Manually update a log entry using [`LoggingEnhancer`]
148+
(https://github.com/googleapis/java-logging/blob/main/google-cloud-logging/src/main/java/com/google/cloud/logging/.
149+
* Use [`ILoggingEvent`](https://logback.qos.ch/apidocs/ch/qos/logback/classic/spi/ILoggingEvent.html) to update a log entry with [`LoggingEventEnhancer`]
150+
(https://github.com/googleapis/java-logging-logback/blob/main/src/main/java/com/google/cloud/logging/logback/LoggingEventEnhancer.java).
151+
* Enable [auto-population](https://github.com/googleapis/java-logging#auto-population-of-log-entrys-metadata) of the `LogEntry` metadata using the `autoPopulateMetadata` configuration flag.
152+
153+
#### Optimize log ingestion
154+
155+
By default, the appender will ingest log entries asynchronously by calling Logging API.
156+
Multiple calls may be aggregated before being sent to improve use of API quota and bandwidth.
157+
You can set the `writeSynchronicity` configuration flag to `SYNC` if they want to ingest log entries synchronously.
158+
Note that configuring synchronous ingestion will probably result in performance penalties to your applications.
159+
If you plan to deploy your application in one of Google Cloud managed environments (e.g. Cloud Run, Cloud Function or App Engine),
160+
you can leverage the support provided by the implicit logging agent and the [structured logging](https://cloud.google.com/logging/docs/structured-logging) feature.
161+
To use it, set the `redirectToStdout` configuration flag to `true`.
162+
This flag instructs the appender to print the log entries to `stdout` instead of ingesting them using Logging API.
163+
The log entries are printed using the [structured logging Json format](https://cloud.google.com/logging/docs/structured-logging#special-payload-fields).
164+
In result, the logging agent will be responsible for ingesting the logs to Logging API.
165+
Note that using the structured logging Json format you cannot control the log name where the logs will be ingested.
166+
The logs will be ingested into the project that hosts the environment where your application is running.
167+
The configuration `logDestinationProjectId` will be ignored.
168+
169+
133170

134171

135172
## Samples

0 commit comments

Comments
 (0)