KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > logging > Log4jLoggingPlugin


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.logging;
23
24 import java.net.URL JavaDoc;
25
26 import org.apache.log4j.BasicConfigurator;
27 import org.apache.log4j.ConsoleAppender;
28 import org.apache.log4j.FileAppender;
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.PatternLayout;
31 import org.jboss.logging.XLevel;
32
33 /**
34  * A Log4jLoggingPlugin.
35  *
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 58108 $
38  */

39 public class Log4jLoggingPlugin extends LoggingPlugin
40 {
41    public void enableTrace(String JavaDoc name)
42    {
43       Logger.getLogger(name).setLevel(XLevel.TRACE);
44    }
45
46    public void setUp() throws Exception JavaDoc
47    {
48       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
49       String JavaDoc file = System.getProperty("log4j.configuration");
50
51       // Look for a test log4j.xml (if not in the testsuite)
52
if (file == null)
53       {
54          URL JavaDoc url = cl.getResource("test-log4j.xml");
55          if (url != null)
56          {
57             System.setProperty("log4j.configuration", "test-log4j.xml");
58             BasicConfigurator.configure();
59             return;
60          }
61       }
62       
63       // Use any automatic configuration
64
URL JavaDoc url = cl.getResource("log4j.xml");
65       if (url != null)
66       {
67          System.err.println("Found log4j.xml: "+url);
68          System.err.flush();
69          return;
70       }
71       url = cl.getResource("log4j.properties");
72       if (url != null)
73       {
74          System.err.println("Found log4j.properties: "+url);
75          System.err.flush();
76          return;
77       }
78
79       // Setup by hand
80
BasicConfigurator.resetConfiguration();
81       PatternLayout layout = new PatternLayout("%r %-5p [%c{1}] %m%n");
82       ConsoleAppender appender = new ConsoleAppender(layout);
83       BasicConfigurator.configure(appender);
84       file = System.getProperty("org.jboss.test.logfile");
85       if (file != null)
86       {
87          FileAppender fileAppender = new FileAppender(layout, file);
88          BasicConfigurator.configure(fileAppender);
89       }
90    }
91 }
92
Popular Tags