# Class LoggingAppender (0.142.0-alpha)

0.142.0-alpha (latest) 0.140.0-alpha 0.137.0-alpha 0.136.0-alpha 0.134.0-alpha 0.133.0-alpha 0.132.21-alpha 0.131.17-alpha 0.130.30-alpha 0.129.10-alpha 0.128.0-alpha 0.127.16-alpha 0.126.0-alpha 0.125.3-alpha 0.124.0-alpha 0.123.12-alpha 0.122.9-alpha

    public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent>

[Logback](https://logback.qos.ch/) 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 "OFF" -->
             <flushLevel>WARN</flushLevel>

             <!-- Optional: defaults to ASYNC -->
             <writeSynchronicity>SYNC</writeSynchronicity>

             <!-- Optional: defaults to true -->
             <autoPopulateMetadata>false</autoPopulateMetadata>

             <!-- Optional: defaults to false -->
             <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 to true -->
             <partialSuccess>true</partialSuccess>

             <!-- Optional: In the asynchronous mode the call(s) to Logging API takes place asynchronously and few calls to write()
             method may be batched together to compose a single call to Logging API. In order to control the batching settings,
             the logbackBatchingSettings section can be used as shown below.
             See BatchingSettings
             for more info regarding parameters shown below -->
             <logbackBatchingSettings>
                <elementCountThreshold>100</elementCountThreshold>
                <requestByteThreshold>1000</requestByteThreshold>
                <delayThreshold>500</delayThreshold>
                <maxOutstandingElementCount>10000</maxOutstandingElementCount>
                <maxOutstandingRequestBytes>100000</maxOutstandingRequestBytes>
                <limitExceededBehavior>Ignore</limitExceededBehavior>
             </logbackBatchingSettings>
         </appender>
     
## Inheritance

java.lang.Object \> ch.qos.logback.core.spi.ContextAwareBase \> ch.qos.logback.core.UnsynchronizedAppenderBase \> LoggingAppender

## Inherited Members

ch.qos.logback.core.UnsynchronizedAppenderBase.addFilter(ch.qos.logback.core.filter.Filter\<E\>) ch.qos.logback.core.UnsynchronizedAppenderBase.append(E) ch.qos.logback.core.UnsynchronizedAppenderBase.clearAllFilters() ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(E) ch.qos.logback.core.UnsynchronizedAppenderBase.getCopyOfAttachedFiltersList() ch.qos.logback.core.UnsynchronizedAppenderBase.getFilterChainDecision(E) ch.qos.logback.core.UnsynchronizedAppenderBase.getName() ch.qos.logback.core.UnsynchronizedAppenderBase.isStarted() ch.qos.logback.core.UnsynchronizedAppenderBase.setName(java.lang.String) ch.qos.logback.core.UnsynchronizedAppenderBase.start() ch.qos.logback.core.UnsynchronizedAppenderBase.stop() ch.qos.logback.core.UnsynchronizedAppenderBase.toString() ch.qos.logback.core.spi.ContextAwareBase.addError(java.lang.String) ch.qos.logback.core.spi.ContextAwareBase.addError(java.lang.String,java.lang.Throwable) ch.qos.logback.core.spi.ContextAwareBase.addInfo(java.lang.String) ch.qos.logback.core.spi.ContextAwareBase.addInfo(java.lang.String,java.lang.Throwable) ch.qos.logback.core.spi.ContextAwareBase.addStatus(ch.qos.logback.core.status.Status) ch.qos.logback.core.spi.ContextAwareBase.addWarn(java.lang.String) ch.qos.logback.core.spi.ContextAwareBase.addWarn(java.lang.String,java.lang.Throwable) ch.qos.logback.core.spi.ContextAwareBase.getContext() ch.qos.logback.core.spi.ContextAwareBase.getDeclaredOrigin() ch.qos.logback.core.spi.ContextAwareBase.getStatusManager() ch.qos.logback.core.spi.ContextAwareBase.setContext(ch.qos.logback.core.Context) [Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Fields

### DEFAULT_INSTRUMENTATION_VERSION

    public static final String DEFAULT_INSTRUMENTATION_VERSION

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### JAVA_LOGBACK_LIBRARY_NAME

    public static final String JAVA_LOGBACK_LIBRARY_NAME

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

## Constructors

### LoggingAppender()

    public LoggingAppender()

## Methods

### addEnhancer(String enhancerClassName)

    public void addEnhancer(String enhancerClassName)

Add extra labels using classes that implement LoggingEnhancer.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `enhancerClassName` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |

### addLoggingEventEnhancer(String enhancerClassName)

    public void addLoggingEventEnhancer(String enhancerClassName)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `enhancerClassName` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |

### append(ILoggingEvent e)

    protected void append(ILoggingEvent e)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `e` | `ch.qos.logback.classic.spi.ILoggingEvent` |

**Overrides** ch.qos.logback.core.UnsynchronizedAppenderBase.append(E)

### flush() (deprecated)

    public void flush()

Flushes any pending asynchronous logging writes.

### getLoggingOptions()

    protected LoggingOptions getLoggingOptions()

Gets the LoggingOptions to use for this [LoggingAppender](https://docs.cloud.google.com/java/docs/reference/google-cloud-logging-logback/latest/com.google.cloud.logging.logback.LoggingAppender).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.cloud.logging.LoggingOptions` |   |

### getWriteSynchronicity() (deprecated)

    public Synchronicity getWriteSynchronicity()

Returns the current value of the ingestion mode.

The method is deprecated. Use appender configuration to set up the ingestion

| **Returns** ||
|---|---|
| **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`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `flag` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` the metadata auto-population flag. |

### setCredentials(GoogleCredentials credentials)

    public void setCredentials(GoogleCredentials credentials)

Sets the credential to use. If not set the appender will use GoogleCredentials#getApplicationDefault() to authenticate.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `credentials` | `com.google.auth.oauth2.GoogleCredentials` the GoogleCredentials to set |

### setCredentialsFile(String credentialsFile)

    public void setCredentialsFile(String credentialsFile)

> [!WARNING]
> **Obsolete**
>
> *This method is obsolete because of a potential security risk. Use the setCredentials() method instead*
>
> This feature is stable for usage in this major version, but may be deprecated in a future release.

This method is obsolete because of a potential security risk. Use the [#setCredentials(GoogleCredentials)](https://docs.cloud.google.com/java/docs/reference/google-cloud-logging-logback/latest/com.google.cloud.logging.logback.LoggingAppender#com_google_cloud_logging_logback_LoggingAppender_setCredentials_com_google_auth_oauth2_GoogleCredentials_) method instead.

If you know that you will be loading credential configurations of a specific type, it is
recommended to use a credential-type-specific `fromStream()` method. This will ensure that an
unexpected credential type with potential for malicious intent is not loaded unintentionally.
You might still have to do validation for certain credential types. Please follow the
recommendation for that method.

If you are loading your credential configuration from an untrusted source and have not
mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon
as possible to prevent security risks to your environment.

Regardless of the method used, it is always your responsibility to validate configurations
received from external sources.

Sets the path to the [credential
file](https://cloud.google.com/iam/docs/creating-managing-service-account-keys). If not set the appender will use GoogleCredentials#getApplicationDefault() to
authenticate.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `credentialsFile` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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 OFF.

| **Parameter** ||
|---|---|
| **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 [log
name](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.log_name) for which the logs are ingested.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `log` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `projectId` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` The project ID to be used to construct the resource destination name for log entries. |

### setLogbackBatchingSettings(LogbackBatchingSettings batchingSettings)

    public void setLogbackBatchingSettings(LogbackBatchingSettings batchingSettings)

Sets the [LogbackBatchingSettings](https://docs.cloud.google.com/java/docs/reference/google-cloud-logging-logback/latest/com.google.cloud.logging.logback.LogbackBatchingSettings) to be used for the asynchronous mode call(s) to
Logging API

Default to `null`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `batchingSettings` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-logging-logback/latest/com.google.cloud.logging.logback.LogbackBatchingSettings` the [LogbackBatchingSettings](https://docs.cloud.google.com/java/docs/reference/google-cloud-logging-logback/latest/com.google.cloud.logging.logback.LogbackBatchingSettings) to be used for asynchronous mode call(s) to Logging API |

### 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`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `flag` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` 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`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `flag` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` 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](https://cloud.google.com/logging/docs/api/v2/resource-list) resource types.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `resourceType` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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`

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `flag` | `com.google.cloud.logging.Synchronicity` the new ingestion mode. |

### start()

    public synchronized void start()

Initialize and configure the cloud logging service.
**Overrides** ch.qos.logback.core.UnsynchronizedAppenderBase.start()

### stop()

    public synchronized void stop()

**Overrides** ch.qos.logback.core.UnsynchronizedAppenderBase.stop()