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  package org.apache.logging.slf4j;
18  
19  import java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  import org.apache.logging.log4j.MarkerManager;
23  import org.slf4j.IMarkerFactory;
24  import org.slf4j.Marker;
25  
26  /**
27   *
28   */
29  public class Log4jMarkerFactory implements IMarkerFactory {
30  
31      private final ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<String, Marker>();
32  
33      /**
34       * Return a Log4j Marker that is compatible with SLF4J.
35       * @param name The name of the Marker.
36       * @return A Marker.
37       */
38      @Override
39      public Marker getMarker(final String name) {
40          if (name == null) {
41              throw new IllegalArgumentException("Marker name must not be null");
42          }
43          Marker marker = markerMap.get(name);
44          if (marker != null) {
45              return marker;
46          }
47          final org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name);
48          marker = new Log4jMarker(log4jMarker);
49          final Marker existing = markerMap.putIfAbsent(name, marker);
50          return existing == null ? marker : existing;
51      }
52  
53      /**
54       * Returns true if the Marker exists.
55       * @param name The Marker name.
56       * @return true if the Marker exists, false otherwise.
57       */
58      @Override
59      public boolean exists(final String name) {
60          return markerMap.containsKey(name);
61      }
62  
63      /**
64       * Log4j does not support detached Markers. This method always returns false.
65       * @param name The Marker name.
66       * @return false
67       */
68      @Override
69      public boolean detachMarker(final String name) {
70          return false;
71      }
72  
73      /**
74       * Log4j does not support detached Markers for performance reasons. The returned Marker is attached.
75       * @param name The Marker name.
76       * @return
77       */
78      @Override
79      public Marker getDetachedMarker(final String name) {
80          return getMarker(name);
81      }
82  
83  
84  }