View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.slf4j;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  
24  import org.slf4j.ILoggerFactory;
25  import org.slf4j.Logger;
26  
27  /**
28   * LogFactory for Maven which can create a simple logger or one which, if set, fails the build on a severity threshold.
29   */
30  public class MavenLoggerFactory implements org.apache.maven.logging.api.LogLevelRecorder, ILoggerFactory {
31      final DefaultLogLevelRecorder logLevelRecorder = new DefaultLogLevelRecorder();
32      final ConcurrentMap<String, Logger> loggerMap = new ConcurrentHashMap<>();
33  
34      public MavenLoggerFactory() {
35          MavenSimpleLogger.lazyInit();
36      }
37  
38      @Override
39      public boolean hasReachedMaxLevel() {
40          return logLevelRecorder.metThreshold();
41      }
42  
43      @Override
44      public Level getMaxLevelReached() {
45          return null;
46      }
47  
48      @Override
49      public Level getMaxLevelAllowed() {
50          return null;
51      }
52  
53      @Override
54      public void setMaxLevelAllowed(Level level) {
55          this.logLevelRecorder.setMaxLevelAllowed(level);
56      }
57      /**
58       * Return an appropriate {@link Logger} instance by name.
59       */
60      @Override
61      public Logger getLogger(String name) {
62          return loggerMap.computeIfAbsent(name, this::getNewLoggingInstance);
63      }
64  
65      @Override
66      public void reset() {
67          logLevelRecorder.reset();
68      }
69  
70      protected Logger getNewLoggingInstance(String name) {
71          return new MavenFailOnSeverityLogger(name, logLevelRecorder);
72      }
73  
74      public void reconfigure() {
75          SimpleLoggerConfiguration config = MavenSimpleLogger.CONFIG_PARAMS;
76          config.init();
77          loggerMap.values().forEach(l -> {
78              if (l instanceof MavenSimpleLogger msl) {
79                  msl.configure(config.defaultLogLevel);
80              }
81          });
82          logLevelRecorder.reset();
83      }
84  }