001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.slf4j; 018 019 import java.util.concurrent.ConcurrentHashMap; 020 import java.util.concurrent.ConcurrentMap; 021 022 import org.apache.logging.log4j.MarkerManager; 023 import org.slf4j.IMarkerFactory; 024 import org.slf4j.Marker; 025 026 /** 027 * 028 */ 029 public class Log4jMarkerFactory implements IMarkerFactory { 030 031 private final ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<String, Marker>(); 032 033 /** 034 * Return a Log4j Marker that is compatible with SLF4J. 035 * @param name The name of the Marker. 036 * @return A Marker. 037 */ 038 @Override 039 public Marker getMarker(final String name) { 040 if (name == null) { 041 throw new IllegalArgumentException("Marker name must not be null"); 042 } 043 Marker marker = markerMap.get(name); 044 if (marker != null) { 045 return marker; 046 } 047 final org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name); 048 marker = new Log4jMarker(log4jMarker); 049 final Marker existing = markerMap.putIfAbsent(name, marker); 050 return existing == null ? marker : existing; 051 } 052 053 /** 054 * Returns true if the Marker exists. 055 * @param name The Marker name. 056 * @return true if the Marker exists, false otherwise. 057 */ 058 @Override 059 public boolean exists(final String name) { 060 return markerMap.containsKey(name); 061 } 062 063 /** 064 * Log4j does not support detached Markers. This method always returns false. 065 * @param name The Marker name. 066 * @return false 067 */ 068 @Override 069 public boolean detachMarker(final String name) { 070 return false; 071 } 072 073 /** 074 * Log4j does not support detached Markers for performance reasons. The returned Marker is attached. 075 * @param name The Marker name. 076 * @return 077 */ 078 @Override 079 public Marker getDetachedMarker(final String name) { 080 return getMarker(name); 081 } 082 083 084 }