View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.jpl;
19  
20  import java.lang.System.Logger;
21  import java.util.MissingResourceException;
22  import java.util.Objects;
23  import java.util.ResourceBundle;
24  import java.util.function.Supplier;
25  
26  import org.apache.logging.log4j.spi.ExtendedLogger;
27  
28  /**
29   * JPL {@link Logger logger} implementation that uses Log4j.
30   * Implement all default {@link Logger} methods to ensure proper class resolution
31   *
32   * @since 2.14
33   */
34  public class Log4jSystemLogger implements Logger {
35  
36      private final ExtendedLogger logger;
37  
38      private static final String FQCN = Log4jSystemLogger.class.getName();
39  
40      public Log4jSystemLogger(final ExtendedLogger logger) {
41          this.logger = logger;
42      }
43  
44      @Override
45      public String getName() {
46          return logger.getName();
47      }
48  
49      @Override
50      public boolean isLoggable(final Level level) {
51          return logger.isEnabled(getLevel(level));
52      }
53  
54      @Override
55      public void log(Level level, String msg) {
56          log(level, (ResourceBundle) null, msg, (Object[]) null);
57      }
58  
59      @Override
60      public void log(Level level, Supplier<String> msgSupplier) {
61          Objects.requireNonNull(msgSupplier);
62          if (isLoggable(Objects.requireNonNull(level))) {
63              log(level, (ResourceBundle) null, msgSupplier.get(), (Object[]) null);
64          }
65      }
66  
67      @Override
68      public void log(Level level, Object obj) {
69          Objects.requireNonNull(obj);
70          if (isLoggable(Objects.requireNonNull(level))) {
71              log(level, (ResourceBundle) null, obj.toString(), (Object[]) null);
72          }
73      }
74  
75      @Override
76      public void log(Level level, String msg, Throwable thrown) {
77          log(level, null, msg, thrown);
78      }
79  
80      @Override
81      public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
82          Objects.requireNonNull(msgSupplier);
83          if (isLoggable(Objects.requireNonNull(level))) {
84              log(level, null, msgSupplier.get(), thrown);
85          }
86      }
87  
88      @Override
89      public void log(Level level, String format, Object... params) {
90          log(level, null, format, params);
91      }
92  
93      @Override
94      public void log(final Level level, final ResourceBundle bundle, final String msg, final Throwable thrown) {
95          logger.logIfEnabled(FQCN, getLevel(level), null, getResource(bundle, msg), thrown);
96      }
97  
98      @Override
99      public void log(final Level level, final ResourceBundle bundle, final String format, final Object... params) {
100         logger.logIfEnabled(FQCN, getLevel(level), null, getResource(bundle, format), params);
101     }
102 
103     private static org.apache.logging.log4j.Level getLevel(final Level level) {
104         switch (level) {
105             case OFF:
106                 return org.apache.logging.log4j.Level.OFF;
107             case ERROR:
108                 return org.apache.logging.log4j.Level.ERROR;
109             case WARNING:
110                 return org.apache.logging.log4j.Level.WARN;
111             case INFO:
112                 return org.apache.logging.log4j.Level.INFO;
113             case DEBUG:
114                 return org.apache.logging.log4j.Level.DEBUG;
115             case TRACE:
116                 return org.apache.logging.log4j.Level.TRACE;
117             case ALL:
118                 return org.apache.logging.log4j.Level.ALL;
119         }
120         return org.apache.logging.log4j.Level.ERROR;
121     }
122 
123     private static String getResource(ResourceBundle bundle, String msg) {
124         if (bundle == null || msg == null) {
125             return msg;
126         }
127         try {
128             return bundle.getString(msg);
129         } catch (MissingResourceException e) {
130             // ignore
131             return msg;
132         } catch (ClassCastException ex) {
133             return bundle.getObject(msg).toString();
134         }
135     }
136 }