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

com.catchpoint.trace.log.integrations.log4j1.CatchpointAppender Maven / Gradle / Ivy

There is a newer version: 0.0.20
Show newest version
package com.catchpoint.trace.log.integrations.log4j1;

import com.catchpoint.trace.common.report.MonitoringDataReporter;
import com.catchpoint.trace.common.sample.Sampler;
import com.catchpoint.trace.log.LogSupport;
import com.catchpoint.trace.log.initialize.LogSupportInitializer;
import com.catchpoint.trace.log.model.Log;
import com.catchpoint.trace.log.model.LogBuilder;
import com.catchpoint.trace.log.model.MutableLog;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import java.util.HashMap;
import java.util.Map;

/**
 * {@link  org.apache.log4j.Appender} implementation which appends log events to Catchpoint.
 *
 * @author serkan
 */
public class CatchpointAppender extends AppenderSkeleton {

    private static final Map LOG_LEVEL_CODE_MAP =
            new HashMap() {{
                put(Level.TRACE, 0);
                put(Level.DEBUG, 1);
                put(Level.INFO, 2);
                put(Level.WARN, 3);
                put(Level.ERROR, 4);
                put(Level.FATAL, 5);
            }};

    private final MonitoringDataReporter logReporter;
    private final Sampler logSampler;

    public CatchpointAppender() {
        this(null, null);
    }

    public CatchpointAppender(MonitoringDataReporter logReporter) {
        this(logReporter, null);
    }

    public CatchpointAppender(Sampler logSampler) {
        this(null, logSampler);
    }

    public CatchpointAppender(MonitoringDataReporter logReporter, Sampler logSampler) {
        this.logReporter = logReporter;
        this.logSampler = logSampler;
    }

    static int getLogLevelCode(Level level) {
        if (level == null) {
            return -1;
        }
        Integer logLevelCode = LOG_LEVEL_CODE_MAP.get(level);
        if (logLevelCode == null) {
            return -1;
        }
        return logLevelCode;
    }

    @Override
    protected void append(LoggingEvent event) {
        if (!LogSupportInitializer.isLogSupportActive()) {
            return;
        }

        Log log = createLog(event);
        if (log instanceof MutableLog) {
            LogSupport.associateWithTraceIfAvailable((MutableLog) log);
        }

        boolean report = true;
        if (logSampler != null) {
            report = logSampler.isSampled(log);
        } else {
            Sampler sampler = LogSupport.getLogSampler();
            if (sampler != null) {
                report = sampler.isSampled(log);
            }
        }

        if (report) {
            if (logReporter != null) {
                LogSupport.reportLog(logReporter, log);
            } else {
                LogSupport.reportLog(log);
            }
        }
    }

    protected Log createLog(LoggingEvent event) {
        Object message = event.getMessage();

        LogBuilder logBuilder = LogSupport.builder();

        logBuilder.withLogTimestamp(event.getTimeStamp());
        if (message != null) {
            logBuilder.withLogMessage(message.toString());
        } else {
            logBuilder.withLogMessage("");
        }
        logBuilder.withLogContextName(event.getLoggerName());
        logBuilder.withLogLevel(event.getLevel().toString());
        logBuilder.withLogLevelCode(getLogLevelCode(event.getLevel()));

        if (message instanceof TaggedMessage) {
            logBuilder.withTags(((TaggedMessage) message).getTags());
        }

        ThrowableInformation throwableInformation = event.getThrowableInformation();
        if (throwableInformation != null) {
            Throwable throwable = throwableInformation.getThrowable();
            if (throwable != null) {
                logBuilder.withErrorTag(throwable);
            }
        }

        return logBuilder.build();
    }

    @Override
    public void close() {
    }

    @Override
    public boolean requiresLayout() {
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy