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 org.apache.logging.log4j.Level; 020 import org.apache.logging.log4j.Marker; 021 import org.apache.logging.log4j.message.LoggerNameAwareMessage; 022 import org.apache.logging.log4j.message.Message; 023 import org.apache.logging.log4j.message.MessageFactory; 024 import org.apache.logging.log4j.spi.AbstractLogger; 025 import org.slf4j.MarkerFactory; 026 import org.slf4j.spi.LocationAwareLogger; 027 028 /** 029 * 030 */ 031 public class SLF4JLogger extends AbstractLogger { 032 033 private final org.slf4j.Logger logger; 034 private final LocationAwareLogger locationAwareLogger; 035 036 public SLF4JLogger(final String name, final org.slf4j.Logger logger) { 037 super(name); 038 this.logger = logger; 039 this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null; 040 } 041 042 public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) { 043 super(name, messageFactory); 044 this.logger = logger; 045 this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null; 046 } 047 048 @Override 049 protected void log(final Marker marker, final String fqcn, final Level level, final Message data, 050 final Throwable t) { 051 if (locationAwareLogger != null) { 052 if (data instanceof LoggerNameAwareMessage) { 053 ((LoggerNameAwareMessage) data).setLoggerName(getName()); 054 } 055 locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level), data.getFormattedMessage(), 056 data.getParameters(), t); 057 } else { 058 switch (level) { 059 case DEBUG : 060 logger.debug(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 061 break; 062 case TRACE : 063 logger.trace(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 064 break; 065 case INFO : 066 logger.info(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 067 break; 068 case WARN : 069 logger.warn(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 070 break; 071 case ERROR : 072 logger.error(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 073 break; 074 default : 075 logger.error(getMarker(marker), data.getFormattedMessage(), data.getParameters(), t); 076 break; 077 } 078 } 079 } 080 081 private org.slf4j.Marker getMarker(final Marker marker) { 082 if (marker == null) { 083 return null; 084 } 085 final Marker parent = marker.getParent(); 086 final org.slf4j.Marker parentMarker = parent == null ? null : getMarker(parent); 087 final org.slf4j.Marker slf4jMarker = MarkerFactory.getMarker(marker.getName()); 088 if (parentMarker != null && !slf4jMarker.contains(parentMarker)) { 089 slf4jMarker.add(parentMarker); 090 } 091 return slf4jMarker; 092 } 093 094 private int convertLevel(final Level level) { 095 switch (level) { 096 case DEBUG : 097 return LocationAwareLogger.DEBUG_INT; 098 case TRACE : 099 return LocationAwareLogger.TRACE_INT; 100 case INFO : 101 return LocationAwareLogger.INFO_INT; 102 case WARN : 103 return LocationAwareLogger.WARN_INT; 104 case ERROR : 105 return LocationAwareLogger.ERROR_INT; 106 default : 107 return LocationAwareLogger.ERROR_INT; 108 } 109 } 110 111 @Override 112 protected boolean isEnabled(final Level level, final Marker marker, final String data) { 113 return isEnabledFor(level, marker); 114 } 115 116 @Override 117 protected boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) { 118 return isEnabledFor(level, marker); 119 } 120 121 @Override 122 protected boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) { 123 return isEnabledFor(level, marker); 124 } 125 126 @Override 127 protected boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) { 128 return isEnabledFor(level, marker); 129 } 130 131 @Override 132 protected boolean isEnabled(final Level level, final Marker marker, final Message data, final Throwable t) { 133 return isEnabledFor(level, marker); 134 } 135 136 private boolean isEnabledFor(final Level level, final Marker marker) { 137 final org.slf4j.Marker slf4jMarker = getMarker(marker); 138 switch (level) { 139 case DEBUG : 140 return logger.isDebugEnabled(slf4jMarker); 141 case TRACE : 142 return logger.isTraceEnabled(slf4jMarker); 143 case INFO : 144 return logger.isInfoEnabled(slf4jMarker); 145 case WARN : 146 return logger.isWarnEnabled(slf4jMarker); 147 case ERROR : 148 return logger.isErrorEnabled(slf4jMarker); 149 default : 150 return logger.isErrorEnabled(slf4jMarker); 151 152 } 153 } 154 155 public org.slf4j.Logger getLogger() { 156 return locationAwareLogger != null ? locationAwareLogger : logger; 157 } 158 159 }