View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import org.codehaus.plexus.logging.AbstractLogger;
8   import org.codehaus.plexus.logging.Logger;
9   
10  /**
11   * Fake Plexus {@link Logger} that buffers every log line in memory so
12   * tests can assert what was logged.
13   *
14   * <p>Each level prefixes its message with the matching {@code [LEVEL]}
15   * tag and appends to a single {@link StringBuilder}. {@link #toString}
16   * returns the full buffered transcript, suitable for direct equality
17   * checks in assertions.</p>
18   *
19   * @since 0.27.0
20   */
21  @SuppressWarnings("PMD.AvoidStringBufferField")
22  final class FakeLogger extends AbstractLogger {
23  
24      /**
25       * Log level tags.
26       */
27      private static final String[] TAGS = {
28          "[DEBUG] ",
29          "[INFO] ",
30          "[WARNING] ",
31          "[ERROR] ",
32          "[FATAL ERROR] ",
33      };
34  
35      /**
36       * Logged messages.
37       */
38      private final StringBuilder messages;
39  
40      FakeLogger() {
41          this(1, "fakelogger");
42      }
43  
44      FakeLogger(final int threshold, final String name) {
45          super(threshold, name);
46          this.messages = new StringBuilder();
47      }
48  
49      @Override
50      public void debug(final String message, final Throwable throwable) {
51          if (this.isDebugEnabled()) {
52              this.messages.append(FakeLogger.TAGS[0].concat(message));
53              if (throwable != null) {
54                  throwable.printStackTrace(System.out);
55              }
56          }
57      }
58  
59      @Override
60      public void info(final String message, final Throwable throwable) {
61          if (this.isInfoEnabled()) {
62              this.messages.append(FakeLogger.TAGS[1].concat(message));
63              if (throwable != null) {
64                  throwable.printStackTrace(System.out);
65              }
66          }
67      }
68  
69      @Override
70      public void warn(final String message, final Throwable throwable) {
71          if (this.isWarnEnabled()) {
72              this.messages.append(FakeLogger.TAGS[2].concat(message));
73              if (throwable != null) {
74                  throwable.printStackTrace(System.out);
75              }
76          }
77      }
78  
79      @Override
80      public void error(final String message, final Throwable throwable) {
81          if (this.isErrorEnabled()) {
82              this.messages.append(FakeLogger.TAGS[3].concat(message));
83              if (throwable != null) {
84                  throwable.printStackTrace(System.out);
85              }
86          }
87      }
88  
89      @Override
90      public void fatalError(final String message, final Throwable throwable) {
91          if (this.isFatalErrorEnabled()) {
92              this.messages.append(FakeLogger.TAGS[4].concat(message));
93              if (throwable != null) {
94                  throwable.printStackTrace(System.out);
95              }
96          }
97      }
98  
99      @Override
100     public Logger getChildLogger(final String name) {
101         return this;
102     }
103 
104     @Override
105     public String toString() {
106         return this.messages.toString();
107     }
108 }