Package ch.qos.logback.classic.joran.action

Source Code of ch.qos.logback.classic.joran.action.ConsolePluginAction

package ch.qos.logback.classic.joran.action;

import org.xml.sax.Attributes;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.net.SocketAppender;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.InterpretationContext;

public class ConsolePluginAction extends Action {

  private static final String PORT_ATTR = "port";
  private static final Integer DEFAULT_PORT = 4321;

  @Override
  public void begin(InterpretationContext ec, String name, Attributes attributes)
      throws ActionException {
    String portStr = attributes.getValue(PORT_ATTR);
    Integer port = null;
   
    if (portStr == null) {
      port = DEFAULT_PORT;
    } else {
      try {
        port = Integer.valueOf(portStr);
      } catch (NumberFormatException ex) {
        addError("Port " + portStr
            + " in ConsolePlugin config is not a correct number");
      }
    }

    LoggerContext lc = (LoggerContext)ec.getContext();
    SocketAppender appender = new SocketAppender();
    appender.setContext(lc);
    appender.setIncludeCallerData(true);
    appender.setRemoteHost("localhost");
    appender.setPort(port.intValue());
    appender.start();
    Logger root = lc.getLogger(LoggerContext.ROOT_NAME);
    root.addAppender(appender);
   
    addInfo("Sending LoggingEvents to the plugin using port " + port);
  }

  @Override
  public void end(InterpretationContext ec, String name) throws ActionException {

  }
}
TOP

Related Classes of ch.qos.logback.classic.joran.action.ConsolePluginAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.