View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.joran;
15  
16  import ch.qos.logback.classic.joran.action.ConfigurationAction;
17  import ch.qos.logback.classic.joran.action.ConsolePluginAction;
18  import ch.qos.logback.classic.joran.action.ContextNameAction;
19  import ch.qos.logback.classic.joran.action.InsertFromJNDIAction;
20  import ch.qos.logback.classic.joran.action.LevelAction;
21  import ch.qos.logback.classic.joran.action.LoggerAction;
22  import ch.qos.logback.classic.joran.action.LoggerContextListenerAction;
23  import ch.qos.logback.classic.joran.action.ReceiverAction;
24  import ch.qos.logback.classic.joran.action.RootLoggerAction;
25  import ch.qos.logback.classic.joran.sanity.IfNestedWithinSecondPhaseElementSC;
26  import ch.qos.logback.classic.model.processor.ConfigurationModelHandlerFull;
27  import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
28  import ch.qos.logback.classic.spi.ILoggingEvent;
29  import ch.qos.logback.core.joran.GenericXMLConfigurator;
30  import ch.qos.logback.core.joran.JoranConfiguratorBase;
31  import ch.qos.logback.core.joran.action.AppenderRefAction;
32  import ch.qos.logback.core.joran.action.IncludeAction;
33  import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
34  import ch.qos.logback.core.joran.spi.ElementSelector;
35  import ch.qos.logback.core.joran.spi.RuleStore;
36  import ch.qos.logback.core.model.Model;
37  import ch.qos.logback.core.model.processor.DefaultProcessor;
38  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
39  
40  /**
41   * JoranConfigurator class adds rules specific to logback-classic.
42   *
43   * @author Ceki Gülcü
44   */
45  public class JoranConfigurator extends JoranConfiguratorBase<ILoggingEvent> {
46  
47  
48  
49      @Override
50      public void addElementSelectorAndActionAssociations(RuleStore rs) {
51          // add parent rules
52          super.addElementSelectorAndActionAssociations(rs);
53  
54          rs.addRule(new ElementSelector("configuration"), () -> new ConfigurationAction());
55  
56          rs.addRule(new ElementSelector("configuration/contextName"), () -> new ContextNameAction());
57          rs.addRule(new ElementSelector("configuration/contextListener"), () -> new LoggerContextListenerAction());
58          rs.addRule(new ElementSelector("configuration/insertFromJNDI"), () -> new InsertFromJNDIAction());
59  
60          rs.addRule(new ElementSelector("configuration/logger"), () -> new LoggerAction());
61          rs.addRule(new ElementSelector("configuration/logger/level"), () -> new LevelAction());
62  
63          rs.addRule(new ElementSelector("configuration/root"), () -> new RootLoggerAction());
64          rs.addRule(new ElementSelector("configuration/root/level"), () -> new LevelAction());
65          rs.addRule(new ElementSelector("configuration/logger/appender-ref"), () -> new AppenderRefAction());
66          rs.addRule(new ElementSelector("configuration/root/appender-ref"), () -> new AppenderRefAction());
67  
68          rs.addRule(new ElementSelector("configuration/include"), () -> new IncludeAction());
69  
70          rs.addRule(new ElementSelector("configuration/consolePlugin"), () -> new ConsolePluginAction());
71  
72          rs.addRule(new ElementSelector("configuration/receiver"), () -> new ReceiverAction());
73  
74      }
75  
76  
77      @Override
78      protected void sanityCheck(Model topModel) {
79          super.sanityCheck(topModel);
80          performCheck(new IfNestedWithinSecondPhaseElementSC(), topModel);
81      }
82  
83      @Override
84      protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
85          LogbackClassicDefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
86      }
87  
88      private JoranConfigurator makeAnotherInstance() {
89          JoranConfigurator jc = new JoranConfigurator();
90          jc.setContext(context);
91          return jc;
92      }
93  
94      public void buildModelInterpretationContext() {
95          super.buildModelInterpretationContext();
96          this.modelInterpretationContext.setConfiguratorSupplier(  () -> this.makeAnotherInstance() );
97      }
98  
99      @Override
100     protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
101         ModelClassToModelHandlerLinker m = new ModelClassToModelHandlerLinker(context);
102         m.setConfigurationModelHandlerFactoryMethod(ConfigurationModelHandlerFull::makeInstance2);
103         m.link(defaultProcessor);
104     }
105 
106 
107     // The final filters in the two filter chain are rather crucial.
108     // They ensure that only Models attached to the firstPhaseFilter will
109     // be handled in the first phase and all models not previously handled
110     // in the second phase will be handled in a catch-all fallback case.
111     private void sealModelFilters(DefaultProcessor defaultProcessor) {
112         defaultProcessor.getPhaseOneFilter().denyAll();
113         defaultProcessor.getPhaseTwoFilter().allowAll();
114     }
115 
116 }