1 package org.apache.jetspeed.services.logging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import javax.servlet.ServletConfig;
21 import javax.servlet.ServletContext;
22
23
24 import org.apache.jetspeed.services.resources.JetspeedResources;
25
26
27 import org.apache.log4j.LogManager;
28 import org.apache.log4j.Logger;
29 import org.apache.log4j.PropertyConfigurator;
30 import org.apache.log4j.xml.DOMConfigurator;
31
32
33 import org.apache.turbine.Turbine;
34 import org.apache.turbine.services.InitializationException;
35 import org.apache.turbine.services.TurbineBaseService;
36
37 /***
38 * The default implementation of the logging service in Jetspeed.
39 *
40 * This service initializes the underlying logging implementation,
41 * and acts as a factory for loggers.
42 * The current implementation uses Log4J.
43 *
44 * @see org.apache.log4j.LogManager
45 * @see org.apache.log4j.Logger
46 * @author <a href="mailto:harald@ommang.com">Harald Ommang</a>
47 */
48 public class JetspeedLogFactoryService extends TurbineBaseService
49 {
50
51 public String SERVICE_NAME = "JetspeedLogFactoryService";
52 private static final String CONFIG_LOG4J_PROPERTIES = "log4j.properties";
53 private static final String CONFIG_LOG4J_PROPERTIES_DEFAULT = "/WEB-INF/conf/log4j.properties";
54 private static final String CONFIG_LOG4J_AND_WATCH = "log4j.configureAndWatch";
55 private static final boolean CONFIG_LOG4J_AND_WATCH_DEFAULT = true;
56 private static final String CONFIG_LOG4J_WATCHINTERVAL = "log4j.watchInterval";
57 private static final long CONFIG_LOG4J_WATCHINTERVAL_DEFAULT = 60000L;
58 private ServletContext context;
59 /***
60 * Flag to check for initilization. Needed to make time of init more robust.
61 * Also, cannot access the init in parent class from static method
62 */
63 private static boolean initDone = false;
64
65 /***
66 * Default constructor
67 */
68 public JetspeedLogFactoryService()
69 {
70 context = null;
71 }
72
73 /***
74 * Initializes the service by getting the servlet configuration from Turbine
75 *
76 * @throws InitializationException Initialization failed
77 */
78 public void init() throws InitializationException
79 {
80 ServletConfig conf = Turbine.getTurbineServletConfig();
81 if(conf != null)
82 {
83 init(conf);
84 }
85 }
86
87 /***
88 * Initializes the service with the given configuration
89 * Initializes the underlying logging implementation, Log4J
90 *
91 * @param config The ServletConfiguration from Turbine
92 *
93 * @throws InitializationException Initialization failed
94 */
95 public void init(ServletConfig config) throws InitializationException
96 {
97 context = config.getServletContext();
98 String log4jProperties = JetspeedResources.getString(CONFIG_LOG4J_PROPERTIES, CONFIG_LOG4J_PROPERTIES_DEFAULT);
99 if(log4jProperties != null)
100 {
101 try
102 {
103 String fileName = Turbine.getRealPath(log4jProperties);
104 boolean watch = JetspeedResources.getBoolean(CONFIG_LOG4J_AND_WATCH, CONFIG_LOG4J_AND_WATCH_DEFAULT);
105 long watchInterval = JetspeedResources.getLong(CONFIG_LOG4J_WATCHINTERVAL, CONFIG_LOG4J_WATCHINTERVAL_DEFAULT);
106 System.setProperty("webappRoot", context.getRealPath("/"));
107
108
109 if(fileName.endsWith(".properties"))
110 {
111 if(watch)
112 {
113
114 PropertyConfigurator.configureAndWatch(fileName, watchInterval);
115 }
116 else
117 {
118 PropertyConfigurator.configure(fileName);
119 }
120 }
121 else
122 {
123 if(watch)
124 {
125
126 DOMConfigurator.configureAndWatch(fileName, watchInterval);
127 }
128 else
129 {
130 DOMConfigurator.configure(fileName);
131 }
132 }
133 }
134 catch(Exception e)
135 {
136 throw new InitializationException("Failed to load " + log4jProperties + " - " + e.toString());
137 }
138 }
139 setInit(true);
140 initDone = true;
141 }
142
143 /***
144 * The actual Factory method that gets the appropriate logger from Log4j and
145 * wraps it in a JetspeedLogger
146 */
147 public static JetspeedLogger getLogger(String loggerName)
148 {
149
150 if (!initDone)
151 {
152 synchronized (JetspeedLogFactoryService.class)
153 {
154 if (!initDone)
155 {
156 try
157 {
158 new JetspeedLogFactoryService().init();
159 }
160 catch(Exception e)
161 {
162 System.err.println("Init failed no logging available" + e.getMessage());
163 e.printStackTrace();
164 }
165 }
166 }
167 }
168 Logger newLog = LogManager.getLogger(loggerName);
169 JetspeedLogger newLogger = new JetspeedLogger(newLog);
170 return newLogger;
171 }
172 }
173
174