All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.slicklog.appenders.log4j.SlickLogAppender Maven / Gradle / Ivy

Go to download

SlickLog.com Log4j appender. Allows to push your logs to the the SlickLog.com platform.

There is a newer version: 1.1.2
Show newest version
/*
 * Copyright (c) 2013. SlickLog.
 */
package com.slicklog.appenders.log4j;

import com.slicklog.remotelogger.DefaultInternalLogger;
import com.slicklog.remotelogger.InternalLogger;
import com.slicklog.remotelogger.SlickLogConnectionBuilder;
import com.slicklog.remotelogger.SlickLogRemoteLogger;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

/**
 *
 * @author Torindo Nesci
 */
public class SlickLogAppender extends AppenderSkeleton {

  private volatile SlickLogRemoteLogger mRemoteLogger;
  private volatile Log4jSlickLog mSlickLog = new Log4jSlickLog();
  private volatile String mLineSeparator = "\n";
 
  @Override
  protected void append(LoggingEvent event) {
    if (mRemoteLogger == null) {
      mRemoteLogger = getRemoteLogger();
    }
    String message = (String) event.getMessage();
    if (getLayout() != null) {
      message = getLayout().format(event);
    }
    if (!message.endsWith(mLineSeparator)) {
      message = message + mLineSeparator;
    }
    mRemoteLogger.enqueueRecord(message);
  }

  @Override
  public void close() {
  }

  @Override
  public boolean requiresLayout() {
    return false;
  }
  
  public void setApiKey(String apiKey) {
    mSlickLog.setApiKey(apiKey);
    setLineSeparator();
  }

  public void setEndpoint(String endpoint) {
   mSlickLog.setEndpoint(endpoint);
  }

  public void setLogGroupName(String logGroupName) {
    mSlickLog.setLogGroupName(logGroupName);
  }

  public void setLogName(String logName) {
    mSlickLog.setLogName(logName);
  }
  
  public void setDebug(boolean debug) {
    mSlickLog.setDebug(debug);
  }
  
  public void setSpawnNewThreads(boolean spawnNewThreads) {
    mSlickLog.setSpawnNewThreads(spawnNewThreads);
  }

  private SlickLogRemoteLogger getRemoteLogger() {
    try {
      InternalLogger logger = new DefaultInternalLogger(mSlickLog.getDebug());
      SlickLogConnectionBuilder connBuilder = new SlickLogConnectionBuilder(mSlickLog, logger);
      SlickLogRemoteLogger remoteLogger = new SlickLogRemoteLogger(
              connBuilder, logger, mSlickLog.getSpawnNewThreads());
      remoteLogger.init();
      return remoteLogger;
    } catch (Throwable cause) {
      getErrorHandler().error("Could Not get SlickLogRemoteLogger: " + cause);
    }
    return null;
  }
  
  private void setLineSeparator() {
    String ls = System.getProperty("line.separator");
    if (ls != null) {
      mLineSeparator = ls;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy