Log4j

Send application logs to Observe with Log4j. For more about configuring and using Log4j, see the documentation here 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

Configuring Log4j

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

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

    <Appenders>
       <Http name="Observe" url="https://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 customer-id ingest-token" />
       </Http>
    </Appenders>
    
  3. Add an AppenderRef inside the AsyncRoot node for the Observe HTTP appender from the previous step.

    <AsyncRoot level="INFO">
        <AppenderRef ref="Observe" />
    </AsyncRoot>
    
  4. Restart the application to add the changes made to log4j2.xml.

Example Log4j Configurations

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

Observe Only

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

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Appenders>
   <Http name="Observe" url="https://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 customer-id ingest-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://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 customer-id ingest-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>