学前背景:
1. Weblogic部署war并不像tomcat一样会把war解包,所以硬盘物理文件夹结构并不存在.
2. log4j的配置文件有properties文件和xml格式两种.
解决办法,使用一个load-on-start=0的log4jinit servlet来完成log4j的初始化:
如果使用xml格式的log4j的配置文件,没有直接的办法实现初试话,我的解决办法:
1
package
log;
2
3 import org.apache.log4j.LogManager;
4 import org.apache.log4j.xml.DOMConfigurator;
5
6 import java.io.InputStream;
7
8 import javax.xml.parsers.FactoryConfigurationError;
9
10
11 /**
12 *
13 *
14 * @author
15 */
16 public class StreamedDOMConfigurator extends DOMConfigurator {
17
18 // ~ Methods ================================================================
19
20 public static void configure(InputStream fis)
21 throws FactoryConfigurationError {
22
23 try {
24
25 new DOMConfigurator().doConfigure(fis,
26 LogManager.getLoggerRepository());
27 } finally {
28
29 if (fis != null ) {
30
31 try {
32
33 fis.close();
34 } catch (java.io.IOException e) {
35
36 System.err.println( " Could not close [ " + fis + " ]. " );
37 }
38 }
39 }
40 }
41 }
42
2
3 import org.apache.log4j.LogManager;
4 import org.apache.log4j.xml.DOMConfigurator;
5
6 import java.io.InputStream;
7
8 import javax.xml.parsers.FactoryConfigurationError;
9
10
11 /**
12 *
13 *
14 * @author
15 */
16 public class StreamedDOMConfigurator extends DOMConfigurator {
17
18 // ~ Methods ================================================================
19
20 public static void configure(InputStream fis)
21 throws FactoryConfigurationError {
22
23 try {
24
25 new DOMConfigurator().doConfigure(fis,
26 LogManager.getLoggerRepository());
27 } finally {
28
29 if (fis != null ) {
30
31 try {
32
33 fis.close();
34 } catch (java.io.IOException e) {
35
36 System.err.println( " Could not close [ " + fis + " ]. " );
37 }
38 }
39 }
40 }
41 }
42
然后:
1
package
servlet;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5
6 import javax.servlet.http.HttpServlet;
7 import javax.xml.parsers.FactoryConfigurationError;
8
9 import Config;
10 import log.StreamedDOMConfigurator;
11 import util.StringUtil;
12
13
14 /**
15 *
16 *
17 */
18 public class Log4jInitServlet extends HttpServlet {
19
20 // ~ Methods ================================================================
21
22 public void init() {
23
24 String log4jFileLoc = Config.getInstance()
25 .getLog4jFileLoc();
26
27 if (StringUtil.isEmpty(log4jFileLoc)) {
28
29 log4jFileLoc = getInitParameter( " log4j-init-file " );
30 }
31
32 if (log4jFileLoc != null ) {
33
34 try {
35 StreamedDOMConfigurator.configure( new FileInputStream(log4jFileLoc));
36 } catch (FileNotFoundException e) {
37
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 } catch (FactoryConfigurationError e) {
41
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 }
45
46
47 }
48 }
49 }
50
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5
6 import javax.servlet.http.HttpServlet;
7 import javax.xml.parsers.FactoryConfigurationError;
8
9 import Config;
10 import log.StreamedDOMConfigurator;
11 import util.StringUtil;
12
13
14 /**
15 *
16 *
17 */
18 public class Log4jInitServlet extends HttpServlet {
19
20 // ~ Methods ================================================================
21
22 public void init() {
23
24 String log4jFileLoc = Config.getInstance()
25 .getLog4jFileLoc();
26
27 if (StringUtil.isEmpty(log4jFileLoc)) {
28
29 log4jFileLoc = getInitParameter( " log4j-init-file " );
30 }
31
32 if (log4jFileLoc != null ) {
33
34 try {
35 StreamedDOMConfigurator.configure( new FileInputStream(log4jFileLoc));
36 } catch (FileNotFoundException e) {
37
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 } catch (FactoryConfigurationError e) {
41
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 }
45
46
47 }
48 }
49 }
50
如果使用properties格式的,那就简单了,网上就有解决办法:
1
//
Properties props = new Properties();
2 // InputStream is = getServletContext()
3 // .getResourceAsStream(log4jFileLoc);
4 // InputStream istream = null;
5 // try {
6 // istream = new FileInputStream(log4jFileLoc);
7 // istream = ClassLoader.getSystemResourceAsStream(log4jFileLoc);
8 // istream = new FileInputStream(log4jFileLoc);
9 // System.out.println(istream);
10 // props.load(istream);
11 // props.list(System.out);
12 // istream.close();
2 // InputStream is = getServletContext()
3 // .getResourceAsStream(log4jFileLoc);
4 // InputStream istream = null;
5 // try {
6 // istream = new FileInputStream(log4jFileLoc);
7 // istream = ClassLoader.getSystemResourceAsStream(log4jFileLoc);
8 // istream = new FileInputStream(log4jFileLoc);
9 // System.out.println(istream);
10 // props.load(istream);
11 // props.list(System.out);
12 // istream.close();