Skip to content

Commit

Permalink
move Configurator interface to logback-core, add support for configur…
Browse files Browse the repository at this point in the history
…ation from a serialized model ,smo file

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jun 21, 2023
1 parent d4de914 commit bd11e72
Show file tree
Hide file tree
Showing 28 changed files with 928 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package ch.qos.logback.access.joran;

import ch.qos.logback.access.joran.action.ConfigurationAction;
import ch.qos.logback.access.model.ConfigurationModel;
import ch.qos.logback.access.model.processor.ConfigurationModelHandler;
import ch.qos.logback.access.model.processor.LogbackAccessDefaultNestedComponentRegistryRules;
import ch.qos.logback.access.spi.IAccessEvent;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
Expand All @@ -24,13 +22,7 @@
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.model.AppenderModel;
import ch.qos.logback.core.model.AppenderRefModel;
import ch.qos.logback.core.model.processor.AppenderModelHandler;
import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;

/**
* This JoranConfiguratorclass adds rules specific to logback-access.
Expand All @@ -50,26 +42,12 @@ public void addElementSelectorAndActionAssociations(RuleStore rs) {

@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
super.addModelHandlerAssociations(defaultProcessor);
defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);

defaultProcessor.addAnalyser(AppenderModel.class, () ->
new RefContainerDependencyAnalyser(context, AppenderModel.class));
defaultProcessor.addAnalyser(AppenderRefModel.class, () -> new AppenderRefDependencyAnalyser(context));
ModelClassToModelHandlerLinker mham = new ModelClassToModelHandlerLinker(context);
mham.link(defaultProcessor);

sealModelFilters(defaultProcessor);
}

// The final filters in the two filter chain are rather crucial.
// They ensure that only Models attached to the firstPhaseFilter will
// be handled in the first phase and all models not previously handled
// in the second phase will be handled in a catch-all fallback case.
private void sealModelFilters(DefaultProcessor defaultProcessor) {
defaultProcessor.getPhaseOneFilter().denyAll();
defaultProcessor.getPhaseTwoFilter().allowAll();
}

@Override
protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2023, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.access.joran;

import ch.qos.logback.access.model.ConfigurationModel;
import ch.qos.logback.access.model.processor.ConfigurationModelHandler;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase;
import ch.qos.logback.core.model.AppenderModel;
import ch.qos.logback.core.model.AppenderRefModel;
import ch.qos.logback.core.model.processor.AppenderModelHandler;
import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;

/**
* For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to a
* {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance
* for logback-access.
*
* <p>Will also use links from super class.</p>
*
* @since 1.3.9/1.4.9
*/
public class ModelClassToModelHandlerLinker extends ModelClassToModelHandlerLinkerBase {

public ModelClassToModelHandlerLinker(Context context) {
super(context);
}

public void link(DefaultProcessor defaultProcessor) {
super.link(defaultProcessor);
defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);

defaultProcessor.addAnalyser(AppenderModel.class,
() -> new RefContainerDependencyAnalyser(context, AppenderModel.class));
defaultProcessor.addAnalyser(AppenderRefModel.class, () -> new AppenderRefDependencyAnalyser(context));

sealModelFilters(defaultProcessor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
package ch.qos.logback.classic;

import ch.qos.logback.classic.layout.TTLLLayout;
import ch.qos.logback.classic.spi.Configurator;
import ch.qos.logback.core.spi.Configurator;
import ch.qos.logback.classic.spi.ConfiguratorRank;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.spi.ContextAwareBase;

Expand All @@ -34,29 +35,31 @@ public class BasicConfigurator extends ContextAwareBase implements Configurator
public BasicConfigurator() {
}

public ExecutionStatus configure(LoggerContext lc) {
public ExecutionStatus configure(Context context) {
addInfo("Setting up default configuration.");

LoggerContext loggerContext = (LoggerContext) context;

ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
ca.setContext(lc);
ca.setContext(context);
ca.setName("console");
LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
encoder.setContext(lc);
encoder.setContext(context);

// same as
// PatternLayout layout = new PatternLayout();
// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
// %msg%n");
TTLLLayout layout = new TTLLLayout();

layout.setContext(lc);
layout.setContext(context);
layout.start();
encoder.setLayout(layout);

ca.setEncoder(encoder);
ca.start();

Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.addAppender(ca);

// let the caller decide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class ClassicConstants {
public static final String LOGBACK_CONTEXT_SELECTOR = "logback.ContextSelector";
public static final String CONFIG_FILE_PROPERTY = "logback.configurationFile";

/**
* property name for the model file used for configuration
* @since 1.3.9/1.4.9
*/
public static final String MODEL_CONFIG_FILE_PROPERTY = "logback.modelFile";

public static final String JNDI_CONFIGURATION_RESOURCE = JNDI_JAVA_NAMESPACE
+ "comp/env/logback/configuration-resource";
public static final String JNDI_CONTEXT_NAME = JNDI_JAVA_NAMESPACE + "comp/env/logback/context-name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,16 @@
import ch.qos.logback.classic.joran.action.ReceiverAction;
import ch.qos.logback.classic.joran.action.RootLoggerAction;
import ch.qos.logback.classic.joran.sanity.IfNestedWithinSecondPhaseElementSC;
import ch.qos.logback.classic.model.ConfigurationModel;
import ch.qos.logback.classic.model.ContextNameModel;
import ch.qos.logback.classic.model.LevelModel;
import ch.qos.logback.classic.model.LoggerContextListenerModel;
import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.classic.model.RootLoggerModel;
import ch.qos.logback.classic.model.processor.ConfigurationModelHandler;
import ch.qos.logback.classic.model.processor.ContextNameModelHandler;
import ch.qos.logback.classic.model.processor.LevelModelHandler;
import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
import ch.qos.logback.classic.model.processor.LoggerContextListenerModelHandler;
import ch.qos.logback.classic.model.processor.LoggerModelHandler;
import ch.qos.logback.classic.model.processor.RootLoggerModelHandler;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
import ch.qos.logback.core.joran.action.AppenderRefAction;
import ch.qos.logback.core.joran.action.IncludeAction;
import ch.qos.logback.core.joran.sanity.AppenderWithinAppenderSanityChecker;
import ch.qos.logback.core.joran.sanity.SanityChecker;
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.model.AppenderModel;
import ch.qos.logback.core.model.AppenderRefModel;
import ch.qos.logback.core.model.InsertFromJNDIModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.AppenderModelHandler;
import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.InsertFromJNDIModelHandler;
import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;
import ch.qos.logback.core.spi.ContextAware;

/**
* JoranConfigurator class adds rules specific to logback-classic.
Expand Down Expand Up @@ -105,32 +82,8 @@ protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegi

@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
super.addModelHandlerAssociations(defaultProcessor);
defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler::makeInstance);
defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);

defaultProcessor.addHandler(InsertFromJNDIModel.class, InsertFromJNDIModelHandler::makeInstance);

defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);
defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler::makeInstance);
defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler::makeInstance);
defaultProcessor.addHandler(LevelModel.class, LevelModelHandler::makeInstance);

defaultProcessor.addAnalyser(LoggerModel.class,
() -> new RefContainerDependencyAnalyser(context, LoggerModel.class));

defaultProcessor.addAnalyser(RootLoggerModel.class,
() -> new RefContainerDependencyAnalyser(context, RootLoggerModel.class));

defaultProcessor.addAnalyser(AppenderModel.class,
() -> new RefContainerDependencyAnalyser(context, AppenderModel.class));

defaultProcessor.addAnalyser(AppenderRefModel.class,
() -> new AppenderRefDependencyAnalyser(context));

sealModelFilters(defaultProcessor);
ModelClassToModelHandlerLinker m = new ModelClassToModelHandlerLinker(context);
m.link(defaultProcessor);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2023, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.joran;

import ch.qos.logback.classic.model.ConfigurationModel;
import ch.qos.logback.classic.model.ContextNameModel;
import ch.qos.logback.classic.model.LevelModel;
import ch.qos.logback.classic.model.LoggerContextListenerModel;
import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.classic.model.RootLoggerModel;
import ch.qos.logback.classic.model.processor.ConfigurationModelHandler;
import ch.qos.logback.classic.model.processor.ContextNameModelHandler;
import ch.qos.logback.classic.model.processor.LevelModelHandler;
import ch.qos.logback.classic.model.processor.LoggerContextListenerModelHandler;
import ch.qos.logback.classic.model.processor.LoggerModelHandler;
import ch.qos.logback.classic.model.processor.RootLoggerModelHandler;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase;
import ch.qos.logback.core.model.AppenderModel;
import ch.qos.logback.core.model.AppenderRefModel;
import ch.qos.logback.core.model.InsertFromJNDIModel;
import ch.qos.logback.core.model.processor.AppenderModelHandler;
import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.InsertFromJNDIModelHandler;
import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;

/**
* For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to a
* {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance for
* logback-classic.
*
* <p>Will also use links from super class.</p>
*
* @since 1.3.9/1.4.9
*/
public class ModelClassToModelHandlerLinker extends ModelClassToModelHandlerLinkerBase {

public ModelClassToModelHandlerLinker(Context context) {
super(context);
}

@Override
public void link(DefaultProcessor defaultProcessor) {
super.link(defaultProcessor);
defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler::makeInstance);
defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);

defaultProcessor.addHandler(InsertFromJNDIModel.class, InsertFromJNDIModelHandler::makeInstance);

defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);
defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler::makeInstance);
defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler::makeInstance);
defaultProcessor.addHandler(LevelModel.class, LevelModelHandler::makeInstance);

defaultProcessor.addAnalyser(LoggerModel.class,
() -> new RefContainerDependencyAnalyser(context, LoggerModel.class));

defaultProcessor.addAnalyser(RootLoggerModel.class,
() -> new RefContainerDependencyAnalyser(context, RootLoggerModel.class));

defaultProcessor.addAnalyser(AppenderModel.class,
() -> new RefContainerDependencyAnalyser(context, AppenderModel.class));

defaultProcessor.addAnalyser(AppenderRefModel.class, () -> new AppenderRefDependencyAnalyser(context));

sealModelFilters(defaultProcessor);

}

}
Loading

0 comments on commit bd11e72

Please sign in to comment.