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.cli.logging;
20  
21  import org.apache.maven.logging.ProjectBuildLogAppender;
22  import org.codehaus.plexus.logging.Logger;
23  
24  /**
25   * Adapt an SLF4J logger to a Plexus logger, ignoring Plexus logger API parts that are not classical and
26   * probably not really used.
27   *
28   * @since 3.1.0
29   */
30  public class Slf4jLogger implements Logger {
31  
32      private org.slf4j.Logger logger;
33      private String projectId;
34  
35      public Slf4jLogger(org.slf4j.Logger logger) {
36          this.logger = logger;
37          this.projectId = ProjectBuildLogAppender.getProjectId();
38      }
39  
40      public void debug(String message) {
41          setMdc();
42          logger.debug(message);
43      }
44  
45      public void debug(String message, Throwable throwable) {
46          setMdc();
47          logger.debug(message, throwable);
48      }
49  
50      public boolean isDebugEnabled() {
51          return logger.isDebugEnabled();
52      }
53  
54      public void info(String message) {
55          setMdc();
56          logger.info(message);
57      }
58  
59      public void info(String message, Throwable throwable) {
60          setMdc();
61          logger.info(message, throwable);
62      }
63  
64      public boolean isInfoEnabled() {
65          return logger.isInfoEnabled();
66      }
67  
68      public void warn(String message) {
69          setMdc();
70          logger.warn(message);
71      }
72  
73      public void warn(String message, Throwable throwable) {
74          setMdc();
75          logger.warn(message, throwable);
76      }
77  
78      public boolean isWarnEnabled() {
79          return logger.isWarnEnabled();
80      }
81  
82      public void error(String message) {
83          setMdc();
84          logger.error(message);
85      }
86  
87      public void error(String message, Throwable throwable) {
88          setMdc();
89          logger.error(message, throwable);
90      }
91  
92      public boolean isErrorEnabled() {
93          return logger.isErrorEnabled();
94      }
95  
96      public void fatalError(String message) {
97          setMdc();
98          logger.error(message);
99      }
100 
101     public void fatalError(String message, Throwable throwable) {
102         setMdc();
103         logger.error(message, throwable);
104     }
105 
106     public boolean isFatalErrorEnabled() {
107         return logger.isErrorEnabled();
108     }
109 
110     /**
111      * <b>Warning</b>: ignored (always return <code>0 == Logger.LEVEL_DEBUG</code>).
112      */
113     public int getThreshold() {
114         return 0;
115     }
116 
117     /**
118      * <b>Warning</b>: ignored.
119      */
120     public void setThreshold(int threshold) {}
121 
122     /**
123      * <b>Warning</b>: ignored (always return <code>null</code>).
124      */
125     public Logger getChildLogger(String name) {
126         return null;
127     }
128 
129     public String getName() {
130         return logger.getName();
131     }
132 
133     private void setMdc() {
134         if (projectId != null && ProjectBuildLogAppender.getProjectId() == null) {
135             ProjectBuildLogAppender.setProjectId(projectId);
136         }
137     }
138 }