1
14 package ch.qos.logback.classic.joran.action;
15
16 import org.xml.sax.Attributes;
17
18 import ch.qos.logback.classic.Logger;
19 import ch.qos.logback.classic.LoggerContext;
20 import ch.qos.logback.classic.net.SocketAppender;
21 import ch.qos.logback.core.joran.action.Action;
22 import ch.qos.logback.core.joran.spi.ActionException;
23 import ch.qos.logback.core.joran.spi.InterpretationContext;
24
25 public class ConsolePluginAction extends Action {
26
27 private static final String PORT_ATTR = "port";
28 private static final Integer DEFAULT_PORT = 4321;
29
30 @Override
31 public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
32 String portStr = attributes.getValue(PORT_ATTR);
33 Integer port = null;
34
35 if (portStr == null) {
36 port = DEFAULT_PORT;
37 } else {
38 try {
39 port = Integer.valueOf(portStr);
40 } catch (NumberFormatException ex) {
41 addError("Port " + portStr + " in ConsolePlugin config is not a correct number");
42 }
43 }
44
45 LoggerContext lc = (LoggerContext) ec.getContext();
46 SocketAppender appender = new SocketAppender();
47 appender.setContext(lc);
48 appender.setIncludeCallerData(true);
49 appender.setRemoteHost("localhost");
50 appender.setPort(port.intValue());
51 appender.start();
52 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
53 root.addAppender(appender);
54
55 addInfo("Sending LoggingEvents to the plugin using port " + port);
56 }
57
58 @Override
59 public void end(InterpretationContext ec, String name) throws ActionException {
60
61 }
62 }
63