source: trunk/autoquest-runner/src/main/java/de/ugoe/cs/autoquest/log4j/Log4JLogger.java

Last change on this file was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 4.3 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.log4j;
16
17import java.util.logging.Level;
18
19import org.apache.log4j.Logger;
20import org.apache.log4j.PropertyConfigurator;
21
22import de.ugoe.cs.util.console.Console;
23import de.ugoe.cs.util.console.listener.ICommandListener;
24import de.ugoe.cs.util.console.listener.IErrorListener;
25import de.ugoe.cs.util.console.listener.IExceptionListener;
26import de.ugoe.cs.util.console.listener.ITraceListener;
27
28/**
29 * <p>
30 * Implements logging based on the log4j API.
31 * </p>
32 *
33 * @author Steffen Herbold
34 * @version 1.0
35 */
36public class Log4JLogger implements IErrorListener, ITraceListener, IExceptionListener,
37    ICommandListener
38{
39
40    /**
41     * <p>
42     * Reference to the logger.
43     * </p>
44     */
45    Logger logger;
46
47    /**
48     * <p>
49     * This is the trace level according to the Java logger API. We use this instead of the log4j
50     * levels themselves for the logging.
51     * </p>
52     */
53    Level traceLevel;
54
55    /**
56     * <p>
57     * Constructor. Creates a new Log4JLogger and registers the implemented listener with the
58     * {@link Console}.
59     * </p>
60     * @param traceLevel tracing level
61     */
62    public Log4JLogger(Level traceLevel) {
63        PropertyConfigurator.configure("data/log4j.properties");
64        logger = Logger.getLogger("de.ugoe.cs.autoquest");
65        logger.setLevel(convertToLog4JLevel(traceLevel));
66        this.traceLevel = traceLevel;
67        Console.getInstance().registerErrorListener(this);
68        Console.getInstance().registerTraceListener(this);
69        Console.getInstance().registerExceptionListener(this);
70        Console.getInstance().registerCommandListener(this);
71    }
72
73    /*
74     * (non-Javadoc)
75     *
76     * @see de.ugoe.cs.util.console.listener.ICommandListener#commandNotification (java.lang.String)
77     */
78    @Override
79    public void commandNotification(String command) {
80        logger.info("Command executed: " + command);
81    }
82
83    /*
84     * (non-Javadoc)
85     *
86     * @see de.ugoe.cs.util.console.listener.IExceptionListener#printStacktrace(java
87     * .lang.Exception)
88     */
89    @Override
90    public void logException(Exception e) {
91        logger.error("", e);
92    }
93
94    /*
95     * (non-Javadoc)
96     *
97     * @see de.ugoe.cs.util.console.listener.ITraceListener#traceMsg(java.lang.String )
98     */
99    @Override
100    public void traceMsg(String traceMessage, Level level) {
101        if( level.intValue()>=traceLevel.intValue()) {
102            logger.log(convertToLog4JLevel(level), traceMessage);
103        }
104    }
105
106    /*
107     * (non-Javadoc)
108     *
109     * @see de.ugoe.cs.util.console.listener.IErrorListener#errorMsg(java.lang.String )
110     */
111    @Override
112    public void errorMsg(String errMessage) {
113        logger.error(errMessage);
114    }
115
116    /**
117     * <p>
118     * Converts the log level described by {@link Level} into a {@link org.apache.log4j.Level}.
119     * </p>
120     *
121     * @param level
122     *            java.util.logger.Level severity
123     * @return org.apache.log4j.Level severity
124     */
125    private org.apache.log4j.Level convertToLog4JLevel(Level level) {
126        if (level == Level.OFF) {
127            return org.apache.log4j.Level.OFF;
128        }
129        if (level == Level.SEVERE) {
130            return org.apache.log4j.Level.FATAL;
131        }
132        if (level == Level.WARNING) {
133            return org.apache.log4j.Level.WARN;
134        }
135        if (level == Level.INFO || level == Level.CONFIG) {
136            return org.apache.log4j.Level.INFO;
137        }
138        // remaining levels: FINE, FINER, FINEST, ALL
139        return org.apache.log4j.Level.ALL;
140    }
141}
Note: See TracBrowser for help on using the repository browser.