1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util.log;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.text.MessageFormat;
25 import java.util.ResourceBundle;
26 import java.util.logging.Level;
27 import java.util.logging.LogManager;
28 import java.util.logging.LogRecord;
29 import java.util.regex.Pattern;
30
31
32
33
34 public class JettyLogHandler extends java.util.logging.Handler
35 {
36 public static void config()
37 {
38 ClassLoader cl = Thread.currentThread().getContextClassLoader();
39 URL url = cl.getResource("logging.properties");
40 if (url != null)
41 {
42 System.err.printf("Initializing java.util.logging from %s%n",url);
43 try (InputStream in = url.openStream())
44 {
45 LogManager.getLogManager().readConfiguration(in);
46 }
47 catch (IOException e)
48 {
49 e.printStackTrace(System.err);
50 }
51 }
52 else
53 {
54 System.err.printf("WARNING: java.util.logging failed to initialize: logging.properties not found%n");
55 }
56
57 System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.Jdk14Logger");
58 }
59
60 public JettyLogHandler()
61 {
62 if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG","false")))
63 {
64 setLevel(Level.FINEST);
65 }
66
67 if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.IGNORED","false")))
68 {
69 setLevel(Level.ALL);
70 }
71
72 System.err.printf("%s Initialized at level [%s]%n",this.getClass().getName(),getLevel().getName());
73 }
74
75 private synchronized String formatMessage(LogRecord record)
76 {
77 String msg = getMessage(record);
78
79 try
80 {
81 Object params[] = record.getParameters();
82 if ((params == null) || (params.length == 0))
83 {
84 return msg;
85 }
86
87 if (Pattern.compile("\\{\\d+\\}").matcher(msg).find())
88 {
89 return MessageFormat.format(msg,params);
90 }
91
92 return msg;
93 }
94 catch (Exception ex)
95 {
96 return msg;
97 }
98 }
99
100 private String getMessage(LogRecord record)
101 {
102 ResourceBundle bundle = record.getResourceBundle();
103 if (bundle != null)
104 {
105 try
106 {
107 return bundle.getString(record.getMessage());
108 }
109 catch (java.util.MissingResourceException ex)
110 {
111 }
112 }
113
114 return record.getMessage();
115 }
116
117 @Override
118 public void publish(LogRecord record)
119 {
120 org.eclipse.jetty.util.log.Logger JLOG = getJettyLogger(record.getLoggerName());
121
122 int level = record.getLevel().intValue();
123 if (level >= Level.OFF.intValue())
124 {
125
126 return;
127 }
128
129 Throwable cause = record.getThrown();
130 String msg = formatMessage(record);
131
132 if (level >= Level.WARNING.intValue())
133 {
134
135 if (cause != null)
136 {
137 JLOG.warn(msg,cause);
138 }
139 else
140 {
141 JLOG.warn(msg);
142 }
143 return;
144 }
145
146 if (level >= Level.INFO.intValue())
147 {
148
149 if (cause != null)
150 {
151 JLOG.info(msg,cause);
152 }
153 else
154 {
155 JLOG.info(msg);
156 }
157 return;
158 }
159
160 if (level >= Level.FINEST.intValue())
161 {
162
163 if (cause != null)
164 {
165 JLOG.debug(msg,cause);
166 }
167 else
168 {
169 JLOG.debug(msg);
170 }
171 return;
172 }
173
174 if (level >= Level.ALL.intValue())
175 {
176
177 JLOG.ignore(cause);
178 return;
179 }
180 }
181
182 private Logger getJettyLogger(String loggerName)
183 {
184 return org.eclipse.jetty.util.log.Log.getLogger(loggerName);
185 }
186
187 @Override
188 public void flush()
189 {
190
191 }
192
193 @Override
194 public void close() throws SecurityException
195 {
196
197 }
198 }