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        this.name = name;
076        this.logger = PrivateManager.getLogger(name);
077    }
078
079    /*
080     * (non-Javadoc)
081     *
082     * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Object[])
083     */
084    @Override
085    public void debug(final String msg, final Object... args) {
086        logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, args);
087    }
088
089    /*
090     * (non-Javadoc)
091     *
092     * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Throwable)
093     */
094    @Override
095    public void debug(final String msg, final Throwable thrown) {
096        logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, thrown);
097    }
098
099    /*
100     * (non-Javadoc)
101     *
102     * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.Throwable)
103     */
104    @Override
105    public void debug(final Throwable thrown) {
106        logger.logIfEnabled(FQCN, Level.DEBUG, null, (Object) null, thrown);
107    }
108
109    /*
110     * (non-Javadoc)
111     *
112     * @see org.eclipse.jetty.util.log.Logger#getName()
113     */
114    @Override
115    public String getName() {
116        return name;
117    }
118
119    /*
120     * (non-Javadoc)
121     *
122     * @see org.eclipse.jetty.util.log.Logger#ignore(java.lang.Throwable)
123     */
124    @Override
125    public void ignore(final Throwable ignored) {
126        // Really do nothing
127    }
128
129    /*
130     * (non-Javadoc)
131     *
132     * @see org.eclipse.jetty.util.log.Logger#info(java.lang.String, java.lang.Object[])
133     */
134    @Override
135    public void info(final String msg, final Object... args) {
136        logger.logIfEnabled(FQCN, Level.INFO, null, msg, args);
137    }
138
139    /*
140     * (non-Javadoc)
141     *
142     * @see org.eclipse.jetty.util.log.Logger#info(java.lang.String, java.lang.Throwable)
143     */
144    @Override
145    public void info(final String msg, final Throwable thrown) {
146        logger.logIfEnabled(FQCN, Level.INFO, null, msg, thrown);
147    }
148
149    /*
150     * (non-Javadoc)
151     *
152     * @see org.eclipse.jetty.util.log.Logger#info(java.lang.Throwable)
153     */
154    @Override
155    public void info(final Throwable thrown) {
156        logger.logIfEnabled(FQCN, Level.INFO, null, (Object) null, thrown);
157    }
158
159    /*
160     * (non-Javadoc)
161     *
162     * @see org.eclipse.jetty.util.log.Logger#isDebugEnabled()
163     */
164    @Override
165    public boolean isDebugEnabled() {
166        return logger.isDebugEnabled();
167    }
168
169    /*
170     * (non-Javadoc)
171     *
172     * @see org.eclipse.jetty.util.log.AbstractLogger#newLogger(java.lang.String)
173     */
174    @Override
175    protected Logger newLogger(final String fullname) {
176        return new Log4j2Logger(fullname);
177    }
178
179    /*
180     * (non-Javadoc)
181     *
182     * @see org.eclipse.jetty.util.log.Logger#setDebugEnabled(boolean)
183     */
184    @Override
185    public void setDebugEnabled(final boolean enabled) {
186        warn("setDebugEnabled not implemented");
187    }
188
189    /*
190     * (non-Javadoc)
191     *
192     * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.String, java.lang.Object[])
193     */
194    @Override
195    public void warn(final String msg, final Object... args) {
196        logger.logIfEnabled(FQCN, Level.WARN, null, msg, args);
197    }
198
199    /*
200     * (non-Javadoc)
201     *
202     * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.String, java.lang.Throwable)
203     */
204    @Override
205    public void warn(final String msg, final Throwable thrown) {
206        logger.logIfEnabled(FQCN, Level.WARN, null, msg, thrown);
207    }
208
209    /*
210     * (non-Javadoc)
211     *
212     * @see org.eclipse.jetty.util.log.Logger#warn(java.lang.Throwable)
213     */
214    @Override
215    public void warn(final Throwable thrown) {
216        logger.logIfEnabled(FQCN, Level.WARN, null, (Object) null, thrown);
217    }
218
219}