1
2
3
4
5 package gboat2.base.plugin.servlet.listener;
6
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.util.logging.LogManager;
10
11 import javax.servlet.ServletContext;
12 import javax.servlet.ServletContextEvent;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.commons.lang3.SystemUtils;
16 import org.springframework.util.Assert;
17 import org.springframework.web.util.Log4jConfigListener;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class JDKLoggerConfigListener extends Log4jConfigListener {
39
40 public static final String JDK_LOG_CONFIG_KEY_PARM = "jdkLogConfigPathKey";
41
42 public void contextInitialized(ServletContextEvent event) {
43 super.contextInitialized(event);
44 setJdkLogConfigPathSystemProperty(event.getServletContext());
45 }
46
47 private void setJdkLogConfigPathSystemProperty(ServletContext servletContext) throws IllegalStateException {
48 Assert.notNull(servletContext, "ServletContext must not be null");
49 String root = servletContext.getRealPath("/");
50 String param = servletContext.getInitParameter(JDK_LOG_CONFIG_KEY_PARM);
51 param = (param == null) ? "/WEB-INF/config/logging.properties" : param;
52
53 String key = "java.util.logging.config.file";
54 String value = root + param;
55 System.setProperty(key, value);
56 servletContext.log("Set jdk log config path system property: '" + key + "' = [" + value + "]");
57
58
59 FileInputStream logStream = null;
60 try {
61 logStream = new FileInputStream(value);
62 LogManager.getLogManager().readConfiguration(logStream);
63 } catch (IOException e) {
64 servletContext.log("Use default jdk property file logging.properties in the direcory of: " + SystemUtils.getJavaHome() + "/lib");
65 } finally {
66 IOUtils.closeQuietly(logStream);
67 }
68 }
69 }