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.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.logging.log4j.MarkerManager;
24  import org.slf4j.IMarkerFactory;
25  import org.slf4j.Marker;
26  import org.slf4j.impl.StaticMarkerBinder;
27  
28  /**
29   * Log4j/SLF4J {@link org.slf4j.Marker} type bridge.
30   */
31  public class Log4jMarker implements Marker {
32  
33      public static final long serialVersionUID = 1590472L;
34  
35      private final IMarkerFactory factory = StaticMarkerBinder.SINGLETON.getMarkerFactory();
36  
37      private final org.apache.logging.log4j.Marker marker;
38  
39      /**
40       * Constructs a Log4jMarker using an existing Log4j {@link org.apache.logging.log4j.Marker}.
41       * @param marker The Log4j Marker upon which to base this Marker.
42       */
43      public Log4jMarker(final org.apache.logging.log4j.Marker marker) {
44          this.marker = marker;
45      }
46  
47      public org.apache.logging.log4j.Marker getLog4jMarker() {
48          return marker;
49      }
50  
51      @Override
52      public void add(final Marker marker) {
53          final Marker m = factory.getMarker(marker.getName());
54          this.marker.addParents(((Log4jMarker)m).getLog4jMarker());
55      }
56  
57      @Override
58      public boolean remove(final Marker marker) {
59          return this.marker.remove(MarkerManager.getMarker(marker.getName()));
60      }
61  
62      @Override
63      public String getName() {
64          return marker.getName();
65      }
66  
67      @Override
68      public boolean hasReferences() {
69          return marker.hasParents();
70      }
71  
72      @Override
73      public boolean hasChildren() {
74          return marker.hasParents();
75      }
76  
77      @Override
78      @SuppressWarnings("rawtypes")
79      public Iterator iterator() {
80          final List<Marker> parents = new ArrayList<Marker>();
81          for (final org.apache.logging.log4j.Marker m : this.marker.getParents()) {
82              parents.add(factory.getMarker(m.getName()));
83          }
84          return parents.iterator();
85      }
86  
87      @Override
88      public boolean contains(final org.slf4j.Marker marker) {
89          return this.marker.isInstanceOf(marker.getName());
90      }
91  
92      @Override
93      public boolean contains(final String s) {
94          return this.marker.isInstanceOf(s);
95      }
96  }