01: package logging;
02:
03: import org.apache.log4j.AppenderSkeleton;
04: import org.apache.log4j.Level;
05: import org.apache.log4j.spi.ErrorCode;
06: import org.apache.log4j.spi.LoggingEvent;
07: import org.apache.log4j.spi.ThrowableInformation;
08: import org.eclipse.core.runtime.ILog;
09: import org.eclipse.core.runtime.Status;
10:
11: /**
12: * PluginLogAppender
13: * This class is a custom Log4J appender that sends Log4J events to
14: * the Eclipse plug-in log.
15: * @author Manoel Marques
16: */
17: public class PluginLogAppender extends AppenderSkeleton {
18:
19: private ILog pluginLog;
20:
21: /**
22: * Sets the Eclipse log instance
23: * @param log plug-in log
24: */
25: void setLog(ILog pluginLog) {
26: this .pluginLog = pluginLog;
27: }
28:
29: /**
30: * Log event happened.
31: * Translates level to status instance codes:
32: * level > Level.ERROR - Status.ERROR
33: * level > Level.WARN - Status.WARNING
34: * level > Level.DEBUG - Status.INFO
35: * default - Status.OK
36: * @param event LoggingEvent instance
37: */
38: public void append(LoggingEvent event) {
39:
40: if (this .layout == null) {
41: this .errorHandler.error("Missing layout for appender "
42: + this .name, null, ErrorCode.MISSING_LAYOUT);
43: return;
44: }
45:
46: String text = this .layout.format(event);
47:
48: Throwable thrown = null;
49: if (this .layout.ignoresThrowable()) {
50: ThrowableInformation info = event.getThrowableInformation();
51: if (info != null)
52: thrown = info.getThrowable();
53: }
54:
55: Level level = event.getLevel();
56: int severity = Status.OK;
57:
58: if (level.toInt() >= Level.ERROR_INT)
59: severity = Status.ERROR;
60: else if (level.toInt() >= Level.WARN_INT)
61: severity = Status.WARNING;
62: else if (level.toInt() >= Level.DEBUG_INT)
63: severity = Status.INFO;
64:
65: this .pluginLog.log(new Status(severity, this .pluginLog
66: .getBundle().getSymbolicName(), level.toInt(), text,
67: thrown));
68: }
69:
70: /**
71: * Closes this appender
72: */
73: public void close() {
74: this .closed = true;
75: }
76:
77: /**
78: * Checks if this appender requires layout
79: * @return true if layout is required.
80: */
81: public boolean requiresLayout() {
82: return true;
83: }
84: }
|