Log4j

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

Requirements

To perform this procedure, you need the following information:

  • Your Observe Customer ID

  • Your Observe data stream token

  • Ability to restart your application

Note

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

Configuring 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 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.

Observe Only

Send the log data to one destination, Observe, 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

   <?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>