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

com.quartzdesk.api.agent.log.logback.LogbackInterceptionAppender Maven / Gradle / Ivy

Go to download

QuartzDesk Public API library required for QuartzDesk Standard and Enterprise edition installations. This library must be placed on the classpath of the Quartz scheduler based application that is managed by QuartzDesk. It is important that this library is loaded by the same classloader that loads the Quartz scheduler API used by the application.

The newest version!
/*
 * Copyright (c) 2013-2024 QuartzDesk.com. All Rights Reserved.
 * QuartzDesk.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.quartzdesk.api.agent.log.logback;

import com.quartzdesk.api.agent.log.LoggingInterceptorWrapper;
import com.quartzdesk.api.agent.log.LoggingInterceptorWrapperException;
import com.quartzdesk.api.agent.log.WorkerThreadLoggingInterceptorRegistry;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;

/**
 * Implementation of a Logback appender that passes the log events to the QuartzDesk JVM agent that intercepts log
 * messages produced by executed jobs.
 * 
 * ==== Example logback.xml ====
 * ...
 *
 * <!--
 *   Appender that passes all received log events to the QuartzDesk agent to intercept
 *   log events produced by executed Quartz jobs.
 * -->
 * <appender name="QUARTZDESK_JVM_AGENT" class="com.quartzdesk.api.agent.log.logback.LogbackInterceptionAppender">
 *   <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
 *     <level>TRACE</level>
 *   </filter>
 *
 *   <layout class="ch.qos.logback.classic.PatternLayout">
 *     <pattern>[%date] %.-1level [%thread] [%logger:%line] - %msg%n</pattern>
 *   </layout>
 * </appender>
 *
 * ...
 *
 * <root level="WARN">
 *   ...
 *   <appender-ref ref="QUARTZDESK_JVM_AGENT"/>
 * </root>
 * 
* * This implementation is not statically bound to the QuartzDesk JVM Agent API, nor to the domain object API. * Therefore it is safe to use this appender on JVMs running without an installed QuartzDesk JVM Agent. * * @version $Id:$ * @see LoggingInterceptorWrapper */ public class LogbackInterceptionAppender extends AppenderBase { protected Layout layout; private LoggingInterceptorWrapper loggingInterceptor; /** * Creates a new {@link LogbackInterceptionAppender} instance. */ public LogbackInterceptionAppender() { setName( "QuartzDesk-" + LogbackInterceptionAppender.class.getSimpleName() ); } /** * Returns the layout used by this appender. * * @return the layout used by this appender. */ public Layout getLayout() { return layout; } /** * Sets the layout to be used by this appender. * * @param layout the layout. */ public void setLayout( Layout layout ) { this.layout = layout; } /** * Starts this appender. */ @Override public void start() { addInfo( "Starting " + LogbackInterceptionAppender.class.getName() ); try { loggingInterceptor = LoggingInterceptorWrapper.create(); } catch ( LoggingInterceptorWrapperException e ) { // addWarn( "Cannot initialize " + LoggingInterceptorWrapper.class.getName() + ". Appender " + getClass().getName() + // " will be disabled.", e ); addWarn( "Cannot initialize " + LoggingInterceptorWrapper.class.getName() + ". Appender " + getClass().getName() + " will be disabled. Cause: " + e.getMessage() ); } super.start(); } /** * Stops this appender. */ @Override public void stop() { loggingInterceptor = null; super.stop(); } /** * If the QuartzDesk agent's logging interceptor wishes to intercept the specified logging event, then this method * formats the log event using the provided pattern layout and passes the result to the logging interceptor. * * @param eventObject a logging event object. */ @Override protected void append( E eventObject ) { /* * The LoggingEvent object does not contain the emitter's thread ID so we assume the emitter * is the current thread. */ Long threadId = Thread.currentThread().getId(); /* * The emitter thread can be a worker thread (a thread created / used by the main job thread). */ Long jobThreadId = WorkerThreadLoggingInterceptorRegistry.INSTANCE.getJobThreadForWorkerThread( threadId ); if ( jobThreadId != null ) { threadId = jobThreadId; } if ( loggingInterceptor != null && loggingInterceptor.isIntercepting( threadId ) ) { LoggingEvent loggingEvent = (LoggingEvent) eventObject; LoggingInterceptorWrapper.MessagePriority priority = convertLevel2Priority( loggingEvent.getLevel() ); String message = layout.doLayout( eventObject ); loggingInterceptor.intercept( threadId, priority, message ); } } /** * Returns the {@link LoggingInterceptorWrapper.MessagePriority} for the specified Logback level. * * @param level a log4j level. * @return the {@link LoggingInterceptorWrapper.MessagePriority} for the specified Logback level. */ protected LoggingInterceptorWrapper.MessagePriority convertLevel2Priority( Level level ) { if ( level.equals( Level.TRACE ) ) return LoggingInterceptorWrapper.MessagePriority.TRACE; else if ( level.equals( Level.DEBUG ) ) return LoggingInterceptorWrapper.MessagePriority.DEBUG; else if ( level.equals( Level.INFO ) ) return LoggingInterceptorWrapper.MessagePriority.INFO; else if ( level.equals( Level.WARN ) ) return LoggingInterceptorWrapper.MessagePriority.WARN; else if ( level.equals( Level.ERROR ) ) return LoggingInterceptorWrapper.MessagePriority.ERROR; else { addWarn( "Cannot map logging level: " + level + ". Using default message priority: " + LoggingInterceptorWrapper.DEFAULT_MESSAGE_PRIORITY ); return LoggingInterceptorWrapper.DEFAULT_MESSAGE_PRIORITY; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy