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