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 018package org.apache.logging.log4j.appserver.jetty; 019 020import org.apache.logging.log4j.Level; 021import org.apache.logging.log4j.LogManager; 022import org.apache.logging.log4j.spi.ExtendedLogger; 023import org.apache.logging.log4j.spi.LoggerContext; 024import org.eclipse.jetty.util.log.AbstractLogger; 025import org.eclipse.jetty.util.log.Logger; 026 027/** 028 * Provides a native Apache Log4j 2 logger for Eclipse Jetty logging. 029 * 030 * <p> 031 * To direct Jetty to use this class, set the system property {{org.eclipse.jetty.util.log.class}} to this class name. 032 * </p> 033 * 034 * <p> 035 * From the command line with: 036 * </p> 037 * <pre>-Dorg.eclipse.jetty.util.log.class = org.apache.logging.log4j.appserver.jetty.Log4j2Logger</pre> 038 * 039 * <p> 040 * Programmatically with: 041 * </p> 042 * <pre>System.setProperty("org.eclipse.jetty.util.log.class", "org.apache.logging.log4j.appserver.jetty.Log4j2Logger");</pre> 043 * 044 * @since Apache Log4j 2.10.0 045 */ 046public class Log4j2Logger extends AbstractLogger { 047 048 private static final String PARENT_FQCN = AbstractLogger.class.getName(); 049 /** 050 * Internal LogManager. Applications call AbstractLogger's getLogger() method so that class must be used 051 * as the parent to locate the caller's ClassLoader. 052 */ 053 private static class PrivateManager extends LogManager { 054 055 public static LoggerContext getContext() { 056 final ClassLoader cl = AbstractLogger.class.getClassLoader(); 057 return getContext(PARENT_FQCN, cl, false); 058 } 059 060 public static ExtendedLogger getLogger(final String name) { 061 return getContext().getLogger(name); 062 } 063 } 064 065 static final String FQCN = Log4j2Logger.class.getName(); 066 private final ExtendedLogger logger; 067 068 private final String name; 069 070 public Log4j2Logger() { 071 this(""); 072 } 073 074 public Log4j2Logger(final String name) { 075 super(); 076 this.name = name; 077 this.logger = PrivateManager.getLogger(name); 078 } 079 080 /* 081 * (non-Javadoc) 082 * 083 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Object[]) 084 */ 085 @Override 086 public void debug(final String msg, final Object... args) { 087 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, args); 088 } 089 090 /* 091 * (non-Javadoc) 092 * 093 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Throwable) 094 */ 095 @Override 096 public void debug(final String msg, final Throwable thrown) { 097 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, thrown); 098 } 099 100 /* 101 * (non-Javadoc) 102 * 103 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.Throwable) 104 */ 105 @Override 106 public void debug(final Throwable thrown) { 107 logger.logIfEnabled(FQCN, Level.DEBUG, null, (Object) null, thrown); 108 } 109 110 /* 111 * (non-Javadoc) 112 * 113 * @see org.eclipse.jetty.util.log.Logger#getName() 114 */ 115 @Override 116 public String getName() { 117 return name; 118 } 119 120 /* 121 * (non-Javadoc) 122 * 123 * @see org.eclipse.jetty.util.log.Logger#ignore(java.lang.Throwable) 124 */ 125 @Override 126 public void ignore(final Throwable ignored) { 127 // Really do nothing 128 } 129 130 /* 131 * (non-Javadoc) 132 * 133 * @see org.eclipse.jetty.util.log.Logger#info(java.lang.String, java.lang.Object[]) 134 */ 135 @Override 136 public void info(final String msg, final Object... args) { 137 logger.logIfEnabled(FQCN, Level.INFO, null, msg, args); 138 } 139 140 /* 141 * (non-Javadoc) 142 * 143 * @see org.eclipse.jetty.util.log.Logger#info(java.lang.String, java.lang.Throwable) 144 */ 145 @Override 146 public void info(final String msg, final Throwable thrown) { 147 logger.logIfEnabled(FQCN, Level.INFO, null, msg, thrown); 148 } 149 150 /* 151 * (non-Javadoc) 152 * 153 * @see org.eclipse.jetty.util.log.Logger#info(java.lang.Throwable) 154 */ 155 @Override 156 public void info(final Throwable thrown) { 157 logger.logIfEnabled(FQCN, Level.INFO, null, (Object) null, thrown); 158 } 159 160 /* 161 * (non-Javadoc) 162 * 163 * @see org.eclipse.jetty.util.log.Logger#isDebugEnabled() 164 */ 165 @Override 166 public boolean isDebugEnabled() { 167 return logger.isDebugEnabled(); 168 } 169 170 /* 171 * (non-Javadoc) 172 * 173 * @see org.eclipse.jetty.util.log.AbstractLogger#newLogger(java.lang.String) 174 */ 175 @Override 176 protected Logger newLogger(final String fullname) { 177 return new Log4j2Logger(fullname); 178 } 179 180 /* 181 * (non-Javadoc) 182 * 183 * @see org.eclipse.jetty.util.log.Logger#setDebugEnabled(boolean) 184 */ 185 @Override 186 public void setDebugEnabled(final boolean enabled) { 187 warn("setDebugEnabled not implemented"); 188 } 189 190 /* 191 * (non-Javadoc) 192 * 193 * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.String, java.lang.Object[]) 194 */ 195 @Override 196 public void warn(final String msg, final Object... args) { 197 logger.logIfEnabled(FQCN, Level.WARN, null, msg, args); 198 } 199 200 /* 201 * (non-Javadoc) 202 * 203 * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.String, java.lang.Throwable) 204 */ 205 @Override 206 public void warn(final String msg, final Throwable thrown) { 207 logger.logIfEnabled(FQCN, Level.WARN, null, msg, thrown); 208 } 209 210 /* 211 * (non-Javadoc) 212 * 213 * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.Throwable) 214 */ 215 @Override 216 public void warn(final Throwable thrown) { 217 logger.logIfEnabled(FQCN, Level.WARN, null, (Object) null, thrown); 218 } 219 220}