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.cling.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 final org.slf4j.Logger logger;
33      private final String projectId;
34  
35      public Slf4jLogger(org.slf4j.Logger logger) {
36          this.logger = logger;
37          this.projectId = ProjectBuildLogAppender.getProjectId();
38      }
39  
40      @Override
41      public void debug(String message) {
42          setMdc();
43          logger.debug(message);
44      }
45  
46      @Override
47      public void debug(String message, Throwable throwable) {
48          setMdc();
49          logger.debug(message, throwable);
50      }
51  
52      @Override
53      public boolean isDebugEnabled() {
54          return logger.isDebugEnabled();
55      }
56  
57      @Override
58      public void info(String message) {
59          setMdc();
60          logger.info(message);
61      }
62  
63      @Override
64      public void info(String message, Throwable throwable) {
65          setMdc();
66          logger.info(message, throwable);
67      }
68  
69      @Override
70      public boolean isInfoEnabled() {
71          return logger.isInfoEnabled();
72      }
73  
74      @Override
75      public void warn(String message) {
76          setMdc();
77          logger.warn(message);
78      }
79  
80      @Override
81      public void warn(String message, Throwable throwable) {
82          setMdc();
83          logger.warn(message, throwable);
84      }
85  
86      @Override
87      public boolean isWarnEnabled() {
88          return logger.isWarnEnabled();
89      }
90  
91      @Override
92      public void error(String message) {
93          setMdc();
94          logger.error(message);
95      }
96  
97      @Override
98      public void error(String message, Throwable throwable) {
99          setMdc();
100         logger.error(message, throwable);
101     }
102 
103     @Override
104     public boolean isErrorEnabled() {
105         return logger.isErrorEnabled();
106     }
107 
108     @Override
109     public void fatalError(String message) {
110         setMdc();
111         logger.error(message);
112     }
113 
114     @Override
115     public void fatalError(String message, Throwable throwable) {
116         setMdc();
117         logger.error(message, throwable);
118     }
119 
120     @Override
121     public boolean isFatalErrorEnabled() {
122         return logger.isErrorEnabled();
123     }
124 
125     /**
126      * <b>Warning</b>: ignored (always return <code>0 == Logger.LEVEL_DEBUG</code>).
127      */
128     @Override
129     public int getThreshold() {
130         return 0;
131     }
132 
133     /**
134      * <b>Warning</b>: ignored.
135      */
136     @Override
137     public void setThreshold(int threshold) {}
138 
139     /**
140      * <b>Warning</b>: ignored (always return <code>null</code>).
141      */
142     @Override
143     public Logger getChildLogger(String name) {
144         return null;
145     }
146 
147     @Override
148     public String getName() {
149         return logger.getName();
150     }
151 
152     private void setMdc() {
153         if (projectId != null && ProjectBuildLogAppender.getProjectId() == null) {
154             ProjectBuildLogAppender.setProjectId(projectId);
155         }
156     }
157 }