View Javadoc

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