public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent>
Logback appender for Google Cloud Logging.
Appender configuration in logback.xml
:
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender"> <!-- Optional: filter logs at and above this level --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <!-- Optional: defaults to"java.log"
--> <log>application.log</log> <!-- Optional: defaults to"ERROR"
--> <flushLevel>WARNING</flushLevel> <!-- Optional: defaults toASYNC
--> <writeSynchronicity>SYNC</writeSynchronicity> <!-- Optional: defaults totrue
--> <autoPopulateMetadata>false</autoPopulateMetadata> <!-- Optional: defaults tofalse
--> <redirectToStdout>true</redirectToStdout> <!-- Optional: auto detects on App Engine Flex, Standard, GCE and GKE, defaults to "global". See supported resource types --> <resourceType></resourceType> <!-- 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> <!-- Optional: specifies if a batch's valid entries should be written even if some other entry failed due to an error. Defaults totrue
--> <partialSuccess>true</partialSuccess> </appender>
Inheritance
java.lang.Object > ch.qos.logback.core.spi.ContextAwareBase > ch.qos.logback.core.UnsynchronizedAppenderBase > LoggingAppenderStatic Fields
JAVA_LOGBACK_LIBRARY_NAME
public static final String JAVA_LOGBACK_LIBRARY_NAME
Type | Description |
String |
Constructors
LoggingAppender()
public LoggingAppender()
Methods
addEnhancer(String enhancerClassName)
public void addEnhancer(String enhancerClassName)
Add extra labels using classes that implement LoggingEnhancer.
Name | Description |
enhancerClassName | String |
addLoggingEventEnhancer(String enhancerClassName)
public void addLoggingEventEnhancer(String enhancerClassName)
Name | Description |
enhancerClassName | String |
append(ILoggingEvent e)
protected void append(ILoggingEvent e)
Name | Description |
e | ch.qos.logback.classic.spi.ILoggingEvent |
flush()
public void flush()
Flushes any pending asynchronous logging writes.
getLoggingOptions()
protected LoggingOptions getLoggingOptions()
Gets the LoggingOptions to use for this LoggingAppender.
Type | Description |
com.google.cloud.logging.LoggingOptions |
getWriteSynchronicity()
public Synchronicity getWriteSynchronicity()
Returns the current value of the ingestion mode.
The method is deprecated. Use appender configuration to setup the ingestion
Type | Description |
com.google.cloud.logging.Synchronicity | a Synchronicity value of the ingestion module. |
setAutoPopulateMetadata(boolean flag)
public void setAutoPopulateMetadata(boolean flag)
Sets the automatic population of metadata fields for ingested logs.
Default to true
.
Name | Description |
flag | boolean the metadata auto-population flag. |
setCredentialsFile(String credentialsFile)
public void setCredentialsFile(String credentialsFile)
Sets the path to the https://cloud.google.com/iam/docs/creating-managing-service-account-keys. If not set the appender will use GoogleCredentials#getApplicationDefault() to authenticate.
Name | Description |
credentialsFile | String the path to the credentials file. |
setFlushLevel(Level flushLevel)
public void setFlushLevel(Level flushLevel)
Sets a threshold for log severity level to flush all log entries that were batched so far.
Defaults to Error.
Name | Description |
flushLevel | ch.qos.logback.classic.Level Logback log level |
setLog(String log)
public void setLog(String log)
Sets the LOG_ID part of the https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.log_name for which the logs are ingested.
Name | Description |
log | String LOG_ID part of the name |
setLogDestinationProjectId(String projectId)
public void setLogDestinationProjectId(String projectId)
Sets project ID to be used to customize log destination name for written log entries.
Name | Description |
projectId | String The project ID to be used to construct the resource destination name for log entries. |
setPartialSuccess(boolean flag)
public void setPartialSuccess(boolean flag)
Sets the flag indicating if a batch's valid entries should be written even if some other entry failed due to an error.
Default to true
.
Name | Description |
flag | boolean the partialSuccess flag. |
setRedirectToStdout(boolean flag)
public void setRedirectToStdout(boolean flag)
Sets the redirect of the appender's output to STDOUT instead of ingesting logs to Cloud Logging using Logging API.
Default to false
.
Name | Description |
flag | boolean the redirect flag. |
setResourceType(String resourceType)
public void setResourceType(String resourceType)
Sets the name of the monitored resource (Optional). If not define the appender will try to identify the resource type automatically. Currently support resource types include "gae_app", "gce_instance", "k8s_container", "cloud_run_revision" and "cloud_function". If the appender fails to identify the resource type it will be set to "global".
Must be a one of the supported resource types.
Name | Description |
resourceType | String the name of the monitored resource. |
setWriteSynchronicity(Synchronicity flag)
public void setWriteSynchronicity(Synchronicity flag)
Sets the log ingestion mode. It can be one of the Synchronicity values.
Default to Synchronicity.ASYNC
Name | Description |
flag | com.google.cloud.logging.Synchronicity the new ingestion mode. |
start()
public synchronized void start()
Initialize and configure the cloud logging service.
stop()
public synchronized void stop()