View Javadoc
1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.logging;
48
49 import com.rhi.architecture.batch.ApplicationConfig;
50 import com.rhi.architecture.resource.InitializationException;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.text.SimpleDateFormat;
55 import java.util.Date;
56 import java.util.Enumeration;
57 import java.util.Properties;
58
59 import org.apache.log4j.Appender;
60 import org.apache.log4j.Logger;
61 import org.apache.log4j.FileAppender;
62 import org.apache.log4j.PropertyConfigurator;
63
64 /***
65 * Log4J Factory
66 *
67 * @author Pete McKinstry
68 * @version 1.0
69 */
70 public final class Log4JFactory extends BasicLog4JFactory {
71
72 /***
73 * file appender config key - required since the file appender is
74 * added programmatically. (because path is not known)
75 */
76 public static final String FILE_APPENDER = "Log4j.FileAppender";
77 /***
78 * path where logfile should be written.
79 */
80 public static final String LOGFILE_PATH = "Log.path";
81
82 private static boolean loaded = false;
83
84 /***
85 * default constructor - protected
86 *
87 * @since 1.0
88 */
89 public Log4JFactory() {
90 super();
91 }
92
93 /***
94 * Perform whatever initialization is required of the resource.
95 * This method should only be called once per application instance.
96 *
97 * @param p
98 * @see com.rhi.architecture.resource.Resource#init(Properties)
99 *
100 * @since 1.2
101 * @throws InitializationException
102 */
103 public void init(Properties p) throws InitializationException {
104 /* in the case of multi-process applications, the init() may
105 * be called more than once. Ensure that log4j only creates
106 * one file by only configuring it one time per application. */
107 super.init(p);
108 if (!loaded) {
109 // configure log4j if a log4j config file is provided in the
110 // parc configuration. If not, ignore configuration on the assumption
111 // that log4j will handle it internally.
112 String log4j_properties = p.getProperty(LOG4J_PROPERTYFILE);
113 if (log4j_properties != null) {
114 // create a temporary properties object into which the Log4J
115 // properties from the file will be loaded for passing to the
116 // Log4J configurator
117 Properties tempProps = new Properties();
118 InputStream is = Thread.currentThread()
119 .getContextClassLoader().getResourceAsStream(log4j_properties);
120 if (is != null) {
121 try {
122 // create an input stream for reading the properties from
123 // the file and load it into the temporary properties object
124 is.available();
125 tempProps.load(is);
126 } catch (IOException ioe) {
127 throw new InitializationException("File not found in classpath");
128 }
129 }
130 else {
131 throw new InitializationException("File not found in classpath");
132 }
133 PropertyConfigurator.configure(tempProps);
134 }
135 // log4j initialized.
136 loaded = true;
137 }
138 }
139
140 // ************ Utility Methods *******************
141
142 /***
143 * helper method to set logfile name.
144 *
145 * @since 1.0
146 * @throws IOException
147 * @throws InitializationException
148 */
149 private void setLogfileName()
150 throws IOException, InitializationException {
151
152 Properties p = getProperties();
153
154 // create category
155 String applicationName = p.getProperty(ApplicationConfig.PROG_NAME);
156 if (applicationName == null) {
157 throw new InitializationException(
158 "missing config setting for: " + ApplicationConfig.PROG_NAME);
159 }
160 Logger tmp = Logger.getLogger(applicationName);
161
162 FileAppender fileAppender = getFileAppender(tmp);
163 if (fileAppender == null) {
164 printInfo(tmp);
165 printAllInfo();
166 throw new InitializationException(
167 "FileAppender= "
168 + p.getProperty(FILE_APPENDER)
169 + " not found. log4j can't be initialized.");
170 }
171 String path = getProperties().getProperty(LOGFILE_PATH);
172 if (path == null) {
173 throw new InitializationException(
174 "missing config setting for: " + LOGFILE_PATH);
175 }
176 // make sure log.path has a trailing slash.
177 String path_sep = System.getProperty("file.separator");
178 if (path_sep == null) {
179 throw new InitializationException(
180 "code or JVM error. "
181 + "System class doesn't have file.separator property.");
182 }
183 if (path.endsWith(path_sep) == false) {
184 path += path_sep;
185 }
186
187 StringBuffer filename = new StringBuffer();
188 filename.append(path);
189 // PRD/EST/DEV/etc...
190 filename.append(p.getProperty(ApplicationConfig.ENVIRONMENT, "").
191 substring(0,3));
192 filename.append(
193 p.getProperty(ApplicationConfig.PROG_NAME, "unknown"));
194 SimpleDateFormat formatter = new SimpleDateFormat("_MMddyy_HHmmss");
195 filename.append(
196 formatter.format(new Date(System.currentTimeMillis())));
197 filename.append(".log");
198 // filename = <environment><program name>_<date>_<time>.log
199 // example: ESTMJP_HRogst_061702_090400.log
200 // Log4j 1.2
201 fileAppender.setFile(filename.toString());
202 fileAppender.setAppend(true);
203 // Log4j 1.1
204 //fileAppender.setFile(filename.toString(), true);
205 }
206
207 /***
208 * Get File Appender
209 * @param cat
210 * @return
211 */
212 private FileAppender getFileAppender(Logger cat) {
213 String appenderName = getProperties().getProperty(FILE_APPENDER);
214 if (appenderName == null) {
215 return null;
216 }
217 return (FileAppender) cat.getAppender(appenderName);
218 }
219
220 /***
221 * Debugging methods.
222 * @param cat
223 */
224 private void printInfo(Logger cat) {
225 System.err.println("Category = " + cat.getName());
226 Enumeration enum = cat.getAllAppenders();
227 int count = 0;
228 while (enum.hasMoreElements()) {
229 ++count;
230 Appender app = (Appender) enum.nextElement();
231 System.err.println("Appender = " + app.getName());
232 }
233 System.err.println("Appender count = " + count);
234 }
235
236 /***
237 * Debugging methods.
238 * @deprecated
239 */
240 private void printAllInfo() {
241 Enumeration catEnum = Logger.getCurrentCategories();
242 int catCount = 0;
243 while (catEnum.hasMoreElements()) {
244 ++catCount;
245 Logger cat = (Logger) catEnum.nextElement();
246 printInfo(cat);
247 }
248 Logger root = Logger.getRootLogger();
249 printInfo(root);
250 System.err.println("Category count = " + catCount);
251 }
252
253 /***
254 * Returns the loaded flag to ensure the logfactory isn't double
255 * initialized.
256 *
257 * @return boolean
258 */
259 protected boolean isLoaded() {
260 return loaded;
261 }
262
263 }
This page was automatically generated by Maven