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

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

Go to download

SlickLog.com logback 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.logback;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.slicklog.remotelogger.Constants;
import com.slicklog.remotelogger.SlickLogConnectionBuilder;
import com.slicklog.remotelogger.SlickLogRemoteLogger;
import com.slicklog.remotelogger.model.SlickLog;

/**
 *
 * @author Torindo Nesci
 */
public class SlickLogAppender extends UnsynchronizedAppenderBase implements Constants {

  private volatile Layout mLayout;
  private volatile boolean mDebug = false;
  private volatile boolean mSpawnNewThreads = false;
  private volatile SlickLog mSlickLog;
  private volatile SlickLogRemoteLogger mSlickLogLogger;
  private volatile boolean mStarted = false;
  private volatile String mLineSeparator = "\n";

  @Override
  public void start() {
    mSlickLogLogger = getRemoteLogger();
    if (mSlickLogLogger == null) {
      return;
    }
    setLineSeparator();
    mStarted = true;
    super.start();
    addInfo("SlickLog appender started.");
  }


  private SlickLogRemoteLogger getRemoteLogger() {
    try {
      LogbackInternalLogger logger = new LogbackInternalLogger(this, mDebug);
      SlickLogConnectionBuilder connBuilder = new SlickLogConnectionBuilder(mSlickLog, logger);
      SlickLogRemoteLogger remoteLogger =  new SlickLogRemoteLogger(connBuilder, logger, mSpawnNewThreads);
      remoteLogger.init();
      return remoteLogger;
    } catch (Throwable cause) {
      addError("Could Not get SlickLogRemoteLogger: " + cause);
    }
    return null;
  }

  @Override
  protected void append(ILoggingEvent e) {
    if (!isStarted()) {
      addWarn("Slicklog Appender Not Started.");
      return;
    }
    String logRecord = mLayout.doLayout(e);
    if (!logRecord.endsWith(mLineSeparator)) {
      logRecord = logRecord + mLineSeparator;
    }
    mSlickLogLogger.enqueueRecord(logRecord);
  }

  public void setLayout(Layout layout) {
    mLayout = layout;
  }

  public void setSlickLog(SlickLog slicklog) {
    addInfo("Read Slicklog Configuration: " + slicklog);
    mSlickLog = slicklog;
  }

  public void setDebug(boolean debug) {
    addInfo("Read Debug: " + debug);
    mDebug = debug;
  }
  
  public void setSpawnNewThreads(boolean spawnNewThreads) {
    addInfo("Read SpawnNewThreads: " + spawnNewThreads);
    mSpawnNewThreads = spawnNewThreads;
  }

  @Override
  public boolean isStarted() {
    if (mStarted) {
      return super.isStarted();
    }
    return false;
  }

  private void setLineSeparator() {
    String ls = System.getProperty("line.separator");
    if (ls != null) {
      mLineSeparator = ls;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy