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 this.name = name;
76 this.logger = PrivateManager.getLogger(name);
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Object[])
83 */
84 @Override
85 public void debug(final String msg, final Object... args) {
86 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, args);
87 }
88
89 /*
90 * (non-Javadoc)
91 *
92 * @see org.eclipse.jetty.util.log.Logger#debug(java.lang.String, java.lang.Throwable)
93 */
94 @Override
95 public void debug(final String msg, final Throwable thrown) {
96 logger.logIfEnabled(FQCN, Level.DEBUG, null, msg, thrown);
97 }
98
99 /*
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 }