001/* 002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package de.mnl.osgi.log4j2osgi; 018 019import de.mnl.osgi.lf4osgi.core.LoggerFacade; 020import de.mnl.osgi.lf4osgi.core.LoggerFacadeManager; 021import org.apache.logging.log4j.Level; 022import org.apache.logging.log4j.Marker; 023import org.apache.logging.log4j.message.Message; 024import org.apache.logging.log4j.message.MessageFactory; 025import org.apache.logging.log4j.spi.AbstractLogger; 026import org.osgi.service.log.LoggerFactory; 027 028/** 029 * The OsgiLogger. 030 */ 031public class OsgiLogger extends AbstractLogger implements LoggerFacade { 032 private static final long serialVersionUID = 1L; 033 private final OsgiLoggerContext context; 034 private org.osgi.service.log.Logger delegee; 035 036 /** 037 * Instantiates a new OSGi logger. 038 * 039 * @param context the context 040 * @param name the name 041 * @param messageFactory the message factory 042 */ 043 public OsgiLogger(OsgiLoggerContext context, final String name, 044 final MessageFactory messageFactory) { 045 super(name, messageFactory); 046 this.context = context; 047 LoggerFacadeManager.registerFacade(this); 048 } 049 050 /** 051 * Instantiates a new OSGi logger. 052 * 053 * @param context the context 054 * @param name the name 055 */ 056 public OsgiLogger(OsgiLoggerContext context, final String name) { 057 super(name); 058 this.context = context; 059 LoggerFacadeManager.registerFacade(this); 060 } 061 062 @Override 063 public void loggerFactoryUpdated(LoggerFactory factory) { 064 delegee = factory.getLogger(context.getBundle(), name, 065 org.osgi.service.log.Logger.class); 066 } 067 068 @Override 069 public Level getLevel() { 070 if (delegee.isTraceEnabled()) { 071 return Level.TRACE; 072 } 073 if (delegee.isDebugEnabled()) { 074 return Level.DEBUG; 075 } 076 if (delegee.isInfoEnabled()) { 077 return Level.INFO; 078 } 079 if (delegee.isWarnEnabled()) { 080 return Level.WARN; 081 } 082 if (delegee.isErrorEnabled()) { 083 return Level.ERROR; 084 } 085 // Option: throw new IllegalStateException("Unknown SLF4JLevel"); 086 // Option: return Level.ALL; 087 return Level.OFF; 088 } 089 090 @Override 091 public boolean isEnabled(final Level level, final Marker marker, 092 final Message data, final Throwable thr) { 093 return isEnabledFor(level, marker); 094 } 095 096 @Override 097 public boolean isEnabled(final Level level, final Marker marker, 098 final CharSequence data, final Throwable thr) { 099 return isEnabledFor(level, marker); 100 } 101 102 @Override 103 public boolean isEnabled(final Level level, final Marker marker, 104 final Object data, final Throwable thr) { 105 return isEnabledFor(level, marker); 106 } 107 108 @Override 109 public boolean isEnabled(final Level level, final Marker marker, 110 final String data) { 111 return isEnabledFor(level, marker); 112 } 113 114 @Override 115 public boolean isEnabled(final Level level, final Marker marker, 116 final String data, final Object... params) { 117 return isEnabledFor(level, marker); 118 } 119 120 @Override 121 public boolean isEnabled(final Level level, final Marker marker, 122 final String message, final Object param) { 123 return isEnabledFor(level, marker); 124 } 125 126 @Override 127 public boolean isEnabled(final Level level, final Marker marker, 128 final String message, final Object param1, 129 final Object param2) { 130 return isEnabledFor(level, marker); 131 } 132 133 @Override 134 public boolean isEnabled(final Level level, final Marker marker, 135 final String message, final Object param1, 136 final Object param2, final Object param3) { 137 return isEnabledFor(level, marker); 138 } 139 140 @Override 141 public boolean isEnabled(final Level level, final Marker marker, 142 final String message, final Object param1, final Object param2, 143 final Object param3, final Object param4) { 144 return isEnabledFor(level, marker); 145 } 146 147 @Override 148 public boolean isEnabled(final Level level, final Marker marker, 149 final String message, final Object param1, final Object param2, 150 final Object param3, final Object param4, final Object param5) { 151 return isEnabledFor(level, marker); 152 } 153 154 @Override 155 public boolean isEnabled(final Level level, final Marker marker, 156 final String message, final Object param1, final Object param2, 157 final Object param3, final Object param4, final Object param5, 158 final Object param6) { 159 return isEnabledFor(level, marker); 160 } 161 162 @Override 163 @SuppressWarnings({ "PMD.ExcessiveParameterList", 164 "PMD.AvoidDuplicateLiterals" }) 165 public boolean isEnabled(final Level level, final Marker marker, 166 final String message, final Object param1, final Object param2, 167 final Object param3, final Object param4, final Object param5, 168 final Object param6, final Object param7) { 169 return isEnabledFor(level, marker); 170 } 171 172 @Override 173 @SuppressWarnings("PMD.ExcessiveParameterList") 174 public boolean isEnabled(final Level level, final Marker marker, 175 final String message, final Object param1, final Object param2, 176 final Object param3, final Object param4, final Object param5, 177 final Object param6, final Object param7, final Object param8) { 178 return isEnabledFor(level, marker); 179 } 180 181 @Override 182 @SuppressWarnings("PMD.ExcessiveParameterList") 183 public boolean isEnabled(final Level level, final Marker marker, 184 final String message, final Object param1, final Object param2, 185 final Object param3, final Object param4, final Object param5, 186 final Object param6, final Object param7, final Object param8, 187 final Object param9) { 188 return isEnabledFor(level, marker); 189 } 190 191 @Override 192 @SuppressWarnings("PMD.ExcessiveParameterList") 193 public boolean isEnabled(final Level level, final Marker marker, 194 final String message, final Object param1, final Object param2, 195 final Object param3, final Object param4, final Object param5, 196 final Object param6, final Object param7, final Object param8, 197 final Object param9, final Object param10) { 198 return isEnabledFor(level, marker); 199 } 200 201 @Override 202 public boolean isEnabled(final Level level, final Marker marker, 203 final String data, final Throwable thr) { 204 return isEnabledFor(level, marker); 205 } 206 207 @SuppressWarnings("PMD.UnusedFormalParameter") 208 private boolean isEnabledFor(final Level level, final Marker marker) { 209 switch (level.getStandardLevel()) { 210 case DEBUG: 211 return delegee.isDebugEnabled(); 212 case TRACE: 213 return delegee.isTraceEnabled(); 214 case INFO: 215 return delegee.isInfoEnabled(); 216 case WARN: 217 return delegee.isWarnEnabled(); 218 case ERROR: 219 return delegee.isErrorEnabled(); 220 default: 221 return delegee.isErrorEnabled(); 222 } 223 } 224 225 @Override 226 @SuppressWarnings("PMD.GuardLogStatement") 227 public void logMessage(final String fqcn, final Level level, 228 final Marker marker, final Message message, final Throwable thr) { 229 switch (level.getStandardLevel()) { 230 case TRACE: 231 delegee.trace(message.getFormattedMessage(), 232 message.getParameters(), thr); 233 break; 234 case DEBUG: 235 delegee.debug(message.getFormattedMessage(), 236 message.getParameters(), thr); 237 break; 238 case INFO: 239 delegee.info(message.getFormattedMessage(), 240 message.getParameters(), thr); 241 break; 242 case WARN: 243 delegee.warn(message.getFormattedMessage(), 244 message.getParameters(), thr); 245 break; 246 case ERROR: 247 delegee.error(message.getFormattedMessage(), 248 message.getParameters(), thr); 249 break; 250 default: 251 delegee.error(message.getFormattedMessage(), 252 message.getParameters(), thr); 253 break; 254 255 } 256 } 257 258}