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.jul;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.IdentityHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  import java.util.concurrent.ConcurrentMap;
28  
29  import org.apache.logging.log4j.Level;
30  
31  /**
32   * Default implementation of LevelConverter strategy.
33   * <p>
34   * Since 2.4, supports custom JUL levels by mapping them to their closest mapped neighbour.
35   * </p>
36   * 
37   * @since 2.1
38   */
39  public class DefaultLevelConverter implements LevelConverter {
40  
41      static final class JulLevelComparator implements Comparator<java.util.logging.Level> {
42          @Override
43          public int compare(final java.util.logging.Level level1, final java.util.logging.Level level2) {
44              return Integer.compare(level1.intValue(), level2.intValue());
45          }
46      }
47  
48      private final ConcurrentMap<java.util.logging.Level, Level> julToLog4j = new ConcurrentHashMap<>(9);
49      private final Map<Level, java.util.logging.Level> log4jToJul = new IdentityHashMap<>(10);
50      private final List<java.util.logging.Level> sortedJulLevels = new ArrayList<>(9);
51  
52      public DefaultLevelConverter() {
53          // Map JUL to Log4j
54          mapJulToLog4j(java.util.logging.Level.ALL, Level.ALL);
55          mapJulToLog4j(java.util.logging.Level.FINEST, LevelTranslator.FINEST);
56          mapJulToLog4j(java.util.logging.Level.FINER, Level.TRACE);
57          mapJulToLog4j(java.util.logging.Level.FINE, Level.DEBUG);
58          mapJulToLog4j(java.util.logging.Level.CONFIG, LevelTranslator.CONFIG);
59          mapJulToLog4j(java.util.logging.Level.INFO, Level.INFO);
60          mapJulToLog4j(java.util.logging.Level.WARNING, Level.WARN);
61          mapJulToLog4j(java.util.logging.Level.SEVERE, Level.ERROR);
62          mapJulToLog4j(java.util.logging.Level.OFF, Level.OFF);
63          // Map Log4j to JUL
64          mapLog4jToJul(Level.ALL, java.util.logging.Level.ALL);
65          mapLog4jToJul(LevelTranslator.FINEST, java.util.logging.Level.FINEST);
66          mapLog4jToJul(Level.TRACE, java.util.logging.Level.FINER);
67          mapLog4jToJul(Level.DEBUG, java.util.logging.Level.FINE);
68          mapLog4jToJul(LevelTranslator.CONFIG, java.util.logging.Level.CONFIG);
69          mapLog4jToJul(Level.INFO, java.util.logging.Level.INFO);
70          mapLog4jToJul(Level.WARN, java.util.logging.Level.WARNING);
71          mapLog4jToJul(Level.ERROR, java.util.logging.Level.SEVERE);
72          mapLog4jToJul(Level.FATAL, java.util.logging.Level.SEVERE);
73          mapLog4jToJul(Level.OFF, java.util.logging.Level.OFF);
74          // Sorted Java levels
75          sortedJulLevels.addAll(julToLog4j.keySet());
76          Collections.sort(sortedJulLevels, new JulLevelComparator());
77  
78      }
79  
80      private long distance(final java.util.logging.Level javaLevel, final java.util.logging.Level customJavaLevel) {
81          return Math.abs((long) customJavaLevel.intValue() - (long) javaLevel.intValue());
82      }
83  
84      /*
85       * TODO consider making public for advanced configuration.
86       */
87      private void mapJulToLog4j(final java.util.logging.Level julLevel, final Level level) {
88          julToLog4j.put(julLevel, level);
89      }
90  
91      /*
92       * TODO consider making public for advanced configuration.
93       */
94      private void mapLog4jToJul(final Level level, final java.util.logging.Level julLevel) {
95          log4jToJul.put(level, julLevel);
96      }
97  
98      private Level nearestLevel(final java.util.logging.Level customJavaLevel) {
99          long prevDist = Long.MAX_VALUE;
100         java.util.logging.Level prevLevel = null;
101         for (final java.util.logging.Level mappedJavaLevel : sortedJulLevels) {
102             final long distance = distance(customJavaLevel, mappedJavaLevel);
103             if (distance > prevDist) {
104                 return julToLog4j.get(prevLevel);
105             }
106             prevDist = distance;
107             prevLevel = mappedJavaLevel;
108         }
109         return julToLog4j.get(prevLevel);
110     }
111 
112     @Override
113     public java.util.logging.Level toJavaLevel(final Level level) {
114         return log4jToJul.get(level);
115     }
116 
117     @Override
118     public Level toLevel(final java.util.logging.Level javaLevel) {
119         if (javaLevel == null) {
120             return null;
121         }
122         final Level level = julToLog4j.get(javaLevel);
123         if (level != null) {
124             return level;
125         }
126         final Level nearestLevel = nearestLevel(javaLevel);
127         julToLog4j.put(javaLevel, nearestLevel);
128         return nearestLevel;
129     }
130 }