1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17
18 package org.apache.logging.log4j.appserver.jetty;
19
20 import org.apache.logging.log4j.Level;
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.spi.ExtendedLogger;
23 import org.apache.logging.log4j.spi.LoggerContext;
24 import org.eclipse.jetty.util.log.AbstractLogger;
25 import org.eclipse.jetty.util.log.Logger;
26
27 /**
28 * Provides a native Apache Log4j 2 logger for Eclipse Jetty logging.
29 *
30 * <p>
31 * To direct Jetty to use this class, set the system property {{org.eclipse.jetty.util.log.class}} to this class name.
32 * </p>
33 *
34 * <p>
35 * From the command line with:
36 * </p>
37 * <pre>-Dorg.eclipse.jetty.util.log.class = org.apache.logging.log4j.appserver.jetty.Log4j2Logger</pre>
38 *
39 * <p>
40 * Programmatically with:
41 * </p>
42 * <pre>System.setProperty("org.eclipse.jetty.util.log.class", "org.apache.logging.log4j.appserver.jetty.Log4j2Logger");</pre>
43 *
44 * @since Apache Log4j 2.10.0
45 */
46 public class Log4j2Logger extends AbstractLogger {
47
48 private static final String PARENT_FQCN = AbstractLogger.class.getName();
49 /**
50 * Internal LogManager. Applications call AbstractLogger's getLogger() method so that class must be used
51 * as the parent to locate the caller's ClassLoader.
52 */
53 private static class PrivateManager extends LogManager {
54
55 public static LoggerContext getContext() {
56 final ClassLoader cl = AbstractLogger.class.getClassLoader();
57 return getContext(PARENT_FQCN, cl, false);
58 }
59
60 public static ExtendedLogger getLogger(final String name) {
61 return getContext().getLogger(name);
62 }
63 }
64
65 static final String FQCN = Log4j2Logger.class.getName();
66 private final ExtendedLogger logger;
67
68 private final String name;
69
70 public Log4j2Logger() {
71 this("");
72 }
73
74 public Log4j2Logger(final String name) {
75 super();
76 this.name = name;
77 this.logger = PrivateManager.getLogger(name);
78 }
79
80 /*
81 * (non-Javadoc)
82 *
83 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Object[])
84 */
85 @Override
86 public void debug(final String msg, final Object... args) {
87 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, args);
88 }
89
90 /*
91 * (non-Javadoc)
92 *
93 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Throwable)
94 */
95 @Override
96 public void debug(final String msg, final Throwable thrown) {
97 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, thrown);
98 }
99
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 }