View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.model.processor;
15  
16  import java.util.List;
17  import java.util.Map;
18  
19  import ch.qos.logback.classic.model.ConfigurationModel;
20  import ch.qos.logback.core.Context;
21  import ch.qos.logback.core.model.Model;
22  import ch.qos.logback.core.model.processor.ModelHandlerBase;
23  import ch.qos.logback.core.model.processor.ModelHandlerException;
24  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
25  import ch.qos.logback.core.model.processor.PhaseIndicator;
26  import ch.qos.logback.core.model.processor.ProcessingPhase;
27  import ch.qos.logback.core.status.Status;
28  
29  /**
30   * Dependency-analysis handler for {@link ConfigurationModel} that warns when a
31   * configuration contains contradictory caller-data instructions, for example an
32   * {@code AsyncAppender}, {@code SocketAppender} or {@code SMTPAppender} with
33   * {@code includeCallerData=false} (the default) alongside an appender (or
34   * SMTP layout) whose pattern uses a caller-data converter ({@code %C},
35   * {@code %M}, {@code %L}, {@code %F}, {@code %l}, {@code %class},
36   * {@code %method}, {@code %line}, {@code %file}, {@code %caller}).
37   *
38   * <p>All work is done in {@link #postHandle} so that it runs after every child
39   * {@link ch.qos.logback.core.model.AppenderModel} has been visited by
40   * {@link CallerContradictionAnalyser}, regardless of declaration order.</p>
41   *
42   * <p>Analysis is skipped when the variable
43   * {@value CallerContradictionAnalyser#SKIP_CALLER_CONTRADICTION_ANALYSIS_PROPERTY}
44   * is set to {@code true}.</p>
45   *
46   * @since 1.6.2
47   * @see CallerContradictionAnalyser
48   */
49  @PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
50  public class CallerContradictionWarnAnalyser extends ModelHandlerBase {
51  
52      public CallerContradictionWarnAnalyser(Context context) {
53          super(context);
54      }
55  
56      @Override
57      protected Class<ConfigurationModel> getSupportedModelClass() {
58          return ConfigurationModel.class;
59      }
60  
61      @Override
62      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
63          // no-op; all work is in postHandle after children are visited
64      }
65  
66      @Override
67      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
68          if (CallerContradictionAnalyser.isSkipCallerContradictionAnalysis(mic)) {
69              return;
70          }
71  
72          Map<String, CallerInstructionLogic.Instruction> appenderNameToCallerInstructionMap =
73                  CallerContradictionAnalyser.getAppenderNameToCallerInstructionMap(mic);
74  
75          CallerInstructionLogic callerInstructionLogic = new CallerInstructionLogic();
76  
77          List<Status> messages = callerInstructionLogic.contradiction(appenderNameToCallerInstructionMap);
78  
79          messages.forEach(message -> addStatus(message));
80      }
81  }