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

com.quartzdesk.api.agent.log.logback.ClassicLogbackInterceptionAppender 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.agent.Agent;
import com.quartzdesk.agent.IAgent;
import com.quartzdesk.agent.api.domain.model.log.LoggingEventPriority;
import com.quartzdesk.agent.api.scheduler.common.log.IExecutingJobLoggingInterceptor;
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.ClassicLogbackInterceptionAppender">
 *   <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 statically bound to the QuartzDesk JVM Agent API and to the QuartzDesk domain object * API. Therefore this appender requires a JVM with an installed QuartzDesk JVM Agent. * * @version $Id:$ * @see IExecutingJobLoggingInterceptor */ @Deprecated public class ClassicLogbackInterceptionAppender extends AppenderBase { private static final LoggingEventPriority DEFAULT_LOGGING_INTERCEPTOR_EVENT_PRIORITY = LoggingEventPriority.INFO; protected Layout layout; private IExecutingJobLoggingInterceptor loggingInterceptor; /** * Creates a new {@link ClassicLogbackInterceptionAppender} instance. */ public ClassicLogbackInterceptionAppender() { setName( "QuartzDesk-" + ClassicLogbackInterceptionAppender.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() { IAgent agent = Agent.getInstance(); addInfo( "Starting " + ClassicLogbackInterceptionAppender.class.getName() + " for " + agent ); loggingInterceptor = agent.getRuntime().getExecutingJobLoggingInterceptor(); 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; com.quartzdesk.agent.api.domain.model.log.LoggingEvent loggingInterceptorEvent = new com.quartzdesk.agent.api.domain.model.log.LoggingEvent() .withPriority( convertLevel2Priority( loggingEvent.getLevel() ) ) .withMessage( layout.doLayout( eventObject ) ); loggingInterceptor.intercept( threadId, loggingInterceptorEvent ); } } /** * Returns the {@link LoggingEventPriority} for the specified Logback level. * * @param level a log4j level. * @return the {@link LoggingEventPriority} for the specified Logback level. */ protected LoggingEventPriority convertLevel2Priority( Level level ) { if ( level.equals( Level.TRACE ) ) return LoggingEventPriority.TRACE; else if ( level.equals( Level.DEBUG ) ) return LoggingEventPriority.DEBUG; else if ( level.equals( Level.INFO ) ) return LoggingEventPriority.INFO; else if ( level.equals( Level.WARN ) ) return LoggingEventPriority.WARN; else if ( level.equals( Level.ERROR ) ) return LoggingEventPriority.ERROR; else { addWarn( "Cannot map logging level: " + level + ". Using default logging interceptor event priority: " + DEFAULT_LOGGING_INTERCEPTOR_EVENT_PRIORITY ); return DEFAULT_LOGGING_INTERCEPTOR_EVENT_PRIORITY; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy