View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.inline.mojo;
15  
16  import static com.google.common.base.Preconditions.checkNotNull;
17  import static java.lang.String.format;
18  
19  import org.apache.maven.shared.utils.logging.MessageBuilder;
20  import org.apache.maven.shared.utils.logging.MessageUtils;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  final class PluginLog {
25  
26      private final Logger logger;
27  
28      PluginLog(final Class<?> clazz) {
29          checkNotNull(clazz, "clazz is null");
30          this.logger = LoggerFactory.getLogger(clazz);
31      }
32  
33      public void debug(final String fmt, final Object... args) {
34          checkNotNull(fmt, "fmt is null");
35          synchronized (logger) {
36              logger.debug(format(fmt, args));
37          }
38      }
39  
40      public void debug(final Throwable t, final String fmt, final Object... args) {
41          checkNotNull(fmt, "fmt is null");
42          checkNotNull(t, "t is null");
43          synchronized (logger) {
44              logger.debug(format(fmt, args), t);
45          }
46      }
47  
48      public void info(final String fmt, final Object... args) {
49          checkNotNull(fmt, "fmt is null");
50          synchronized (logger) {
51              logger.info(format(fmt, args));
52          }
53      }
54  
55      public void info(final Throwable t, final String fmt, final Object... args) {
56          checkNotNull(fmt, "fmt is null");
57          checkNotNull(t, "t is null");
58          synchronized (logger) {
59              logger.info(format(fmt, args), t);
60          }
61      }
62  
63      public void warn(final String fmt, final Object... args) {
64          checkNotNull(fmt, "fmt is null");
65          MessageBuilder mb = MessageUtils.buffer();
66          synchronized (logger) {
67              logger.warn(mb.warning(format(fmt, args)).toString());
68          }
69      }
70  
71      public void warn(final Throwable t, final String fmt, final Object... args) {
72          checkNotNull(fmt, "fmt is null");
73          checkNotNull(t, "t is null");
74          MessageBuilder mb = MessageUtils.buffer();
75          synchronized (logger) {
76              logger.warn(mb.warning(format(fmt, args)).toString(), t);
77          }
78      }
79  
80      public void error(final String fmt, final Object... args) {
81          checkNotNull(fmt, "fmt is null");
82          MessageBuilder mb = MessageUtils.buffer();
83          synchronized (logger) {
84              logger.error(mb.failure(format(fmt, args)).toString());
85          }
86      }
87  
88      public void error(final Throwable t, final String fmt, final Object... args) {
89          checkNotNull(fmt, "fmt is null");
90          checkNotNull(t, "t is null");
91          MessageBuilder mb = MessageUtils.buffer();
92          synchronized (logger) {
93              logger.error(mb.failure(format(fmt, args)).toString(), t);
94          }
95      }
96  
97      public void report(final boolean quiet, final String fmt, final Object... args) {
98          if (quiet) {
99              debug(fmt, args);
100         } else {
101             info(fmt, args);
102         }
103     }
104 }