Send application logs to Observe by Snowflake with Log4j. For more about configuring and using Log4j, see the Log4j Documentation.

Requirements

To perform this procedure, you need the following information:

  • Your Observe by Snowflake Customer ID
  • Your Observe by Snowflake data stream token
  • Ability to restart your application
📘

Note

Some Observe by Snowflake instances may optionally use a name instead of Customer ID; if this is the case for your instance, contact your Observe by Snowflake data engineer to discuss implementation. A stem name will work as is, but a DNS redirect name may require client configuration.

Configure Log4j via file

This section assumes you have already configured your application logging and are editing a working configuration file.

  1. Open the log4j2.xml file for your application.

  2. Add the following HTTP appender to your log4j.xml file, inserting your Customer ID into the URL and Bearer_Token into the value attribute of the Authorization node.

   <Appenders>
      <Http name="Observe" url="https://{OBSERVE_CUSTOMER}.collect.observeinc.com/v1/http/log4j">
         <JsonLayout compact="true" eventEol="true" properties="true" />
         <Property name="Content-Type" value="application/json" />
         <Property name="Authorization" value="Bearer ${OBSERVE_TOKEN}" />
      </Http>
   </Appenders>
  1. Add an AppenderRef inside the AsyncRoot node for the Observe by Snowflake HTTP appender from the previous step.
   <AsyncRoot level="INFO">
       <AppenderRef ref="Observe" />
   </AsyncRoot>
  1. Restart the application to add the changes to log4j2.xml.

Alternative variable handling

After creating the xml configuration file, you can pass the needed variables to Log4J as command line arguments. For instance:

java -jar myapp.jar -DOBSERVE_TOKEN=${OBSERVE_TOKEN}

This can be useful in containerized deployments.

Example Log4j configurations

The following examples use the HTTP appender to send data to Observe by Snowflake.

Observe by Snowflake only

Send the log data to one destination, Observe by Snowflake, using the HTTP appender.

   <?xml version="1.0" encoding="utf-8"?>
   <Configuration>
   <Appenders>
      <Http name="Observe" url="https://{OBSERVE_CUSTOMER}.collect.observeinc.com/v1/http/log4j">
         <JsonLayout compact="true" eventEol="true" properties="true" />
         <Property name="Content-Type" value="application/json" />
         <Property name="Authorization" value="Bearer ${OBSERVE_TOKEN}" />
      </Http>
   </Appenders>
   <Loggers>
      <AsyncLogger name="org.application.service.http" level="WARN"/>
      <AsyncLogger name="org.application.extension.http" level="WARN"/>
      <AsyncLogger name="org.application.runtime.core.internal.processor.LoggerMessageProcessor" level="INFO"/>
      <AsyncRoot level="INFO">
         <AppenderRef ref="Observe" />
      </AsyncRoot>
   </Loggers>

Multiple log appenders

Use both the file appender and the HTTP appender to send logs to two destinations:

  • A local file
  • Observe by Snowflake
   <?xml version="1.0" encoding="utf-8"?>
   <Configuration>
    <Appenders>
        <RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}web-service-consumer.log" 
                 filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}web-service-consumer-%i.log">
            <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
            <SizeBasedTriggeringPolicy size="10 MB" />
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <Http name="Observe" url="https://{OBSERVE_CUSTOMER}.collect.observeinc.com/v1/http/log4j">
         <JsonLayout compact="true" eventEol="true" properties="true" />
         <Property name="Content-Type" value="application/json" />
         <Property name="Authorization" value="Bearer ${OBSERVE_TOKEN}" />
      </Http>
    </Appenders>
    <Loggers>
      <!-- Http Logger shows wire traffic on DEBUG. -->
      <AsyncLogger name="org.application.service.http" level="WARN"/>
      <AsyncLogger name="org.application.extension.http" level="WARN"/>

        <!-- Mule classes -->        
        <AsyncLogger name="org.application" level="INFO"/>
        <AsyncLogger name="com.application" level="INFO"/>

        <AsyncRoot level="INFO">
            <AppenderRef ref="file" />
            <AppenderRef ref="Observe" />
      </AsyncRoot>
    </Loggers>
   </Configuration>

Did this page help you?