1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.appserver.tomcat;
18
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.net.URL;
22
23 import org.apache.juli.logging.Log;
24 import org.apache.logging.log4j.Level;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.spi.ExtendedLogger;
27 import org.apache.logging.log4j.spi.LoggerContext;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class TomcatLogger implements Log {
45
46 private static final long serialVersionUID = 1L;
47 private static final String FQCN = TomcatLogger.class.getName();
48 private static final String[] FILE_NAMES = {
49 "log4j2-tomcat.xml", "log4j2-tomcat.json", "log4j2-tomcat.yaml", "log4j2-tomcat.yml",
50 "log4j2-tomcat.properties"
51 };
52
53 private final ExtendedLogger logger;
54
55
56
57
58 public TomcatLogger() {
59 logger = null;
60 }
61
62
63
64
65
66 public TomcatLogger(final String name) {
67 this.logger = PrivateManager.getLogger(name);
68 }
69
70 @Override
71 public boolean isDebugEnabled() {
72 return logger.isDebugEnabled();
73 }
74
75 @Override
76 public boolean isErrorEnabled() {
77 return logger.isErrorEnabled();
78 }
79
80 @Override
81 public boolean isFatalEnabled() {
82 return logger.isFatalEnabled();
83 }
84
85 @Override
86 public boolean isInfoEnabled() {
87 return logger.isInfoEnabled();
88 }
89
90 @Override
91 public boolean isTraceEnabled() {
92 return logger.isTraceEnabled();
93 }
94
95 @Override
96 public boolean isWarnEnabled() {
97 return logger.isWarnEnabled();
98 }
99
100 @Override
101 public void trace(final Object o) {
102 logger.logIfEnabled(FQCN, Level.TRACE, null, o, null);
103 }
104
105 @Override
106 public void trace(final Object o, final Throwable throwable) {
107 logger.logIfEnabled(FQCN, Level.TRACE, null, o, throwable);
108 }
109
110 @Override
111 public void debug(final Object o) {
112 logger.logIfEnabled(FQCN, Level.DEBUG, null, o, null);
113 }
114
115 @Override
116 public void debug(final Object o, final Throwable throwable) {
117 logger.logIfEnabled(FQCN, Level.DEBUG, null, o, throwable);
118 }
119
120 @Override
121 public void info(final Object o) {
122 logger.logIfEnabled(FQCN, Level.INFO, null, o, null);
123 }
124
125 @Override
126 public void info(final Object o, final Throwable throwable) {
127 logger.logIfEnabled(FQCN, Level.INFO, null, o, throwable);
128 }
129
130 @Override
131 public void warn(final Object o) {
132 logger.logIfEnabled(FQCN, Level.WARN, null, o, null);
133 }
134
135 @Override
136 public void warn(final Object o, final Throwable throwable) {
137 logger.logIfEnabled(FQCN, Level.WARN, null, o, throwable);
138 }
139
140 @Override
141 public void error(final Object o) {
142 logger.logIfEnabled(FQCN, Level.ERROR, null, o, null);
143 }
144
145 @Override
146 public void error(final Object o, final Throwable throwable) {
147 logger.logIfEnabled(FQCN, Level.ERROR, null, o, throwable);
148 }
149
150 @Override
151 public void fatal(final Object o) {
152 logger.logIfEnabled(FQCN, Level.FATAL, null, o, null);
153 }
154
155 @Override
156 public void fatal(final Object o, final Throwable throwable) {
157 logger.logIfEnabled(FQCN, Level.FATAL, null, o, throwable);
158 }
159
160
161
162
163 private static class PrivateManager extends LogManager {
164
165 public static LoggerContext getContext() {
166 final ClassLoader cl = TomcatLogger.class.getClassLoader();
167 URI uri = null;
168 for (final String fileName : FILE_NAMES) {
169 try {
170 final URL url = cl.getResource(fileName);
171 if (url != null) {
172 uri = url.toURI();
173 break;
174 }
175 } catch (final URISyntaxException ex) {
176
177 }
178 }
179 if (uri == null) {
180 return getContext(FQCN, cl, false);
181 }
182 return getContext(FQCN, cl, false, uri, "Tomcat");
183 }
184
185 public static ExtendedLogger getLogger(final String name) {
186 return getContext().getLogger(name);
187 }
188 }
189 }