void connect(InetAddress address, int port) { if(this.address == null) return; try { // First, close the previous connection if any. cleanUp(); oos = new ObjectOutputStream(new Socket(address, port).getOutputStream()); } catch(IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } String msg = "Could not connect to remote log4j server at [" +address.getHostName()+"]."; if(reconnectionDelay > 0) { msg += " We will try again later."; fireConnector(); // fire the connector thread } else { msg += " We are not retrying."; errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE); } LogLog.error(msg); } }
/** Connects to remote server at <code>host</code> and <code>port</code>. */ public SocketAppender(String host, int port) { this.port = port; this.address = getAddressByName(host); this.remoteHost = host; connect(address, port); }
/** Connects to remote server at <code>address</code> and <code>port</code>. */ public SocketAppender(InetAddress address, int port) { this.address = address; this.remoteHost = address.getHostName(); this.port = port; connect(address, port); }
/** * The <b>RemoteHost</b> option takes a string value which should be * the host name of the server where a {@link SocketNode} is * running. * */ public void setRemoteHost(String host) { address = getAddressByName(host); remoteHost = host; }
/** * Close this appender. * * <p>This will mark the appender as closed and call then {@link * #cleanUp} method. * */ synchronized public void close() { if(closed) return; this.closed = true; if (advertiseViaMulticastDNS) { zeroConf.unadvertise(); } cleanUp(); }
LogLog.warn("Detected problem with connection: "+e); if(reconnectionDelay > 0) { fireConnector(); } else { errorHandler.error("Detected problem with connection, not reconnecting.", e,
@Override public AppenderSkeleton apply(MonitorLocation loc) { int defaultPort = Integer.parseUnsignedInt(Property.MONITOR_LOG4J_PORT.getDefaultValue()); HostAndPort remote = HostAndPort.fromString(loc.getLocation()); SocketAppender socketAppender = new SocketAppender(); socketAppender.setApplication(System.getProperty("accumulo.application", "unknown")); socketAppender.setRemoteHost(remote.getHost()); socketAppender.setPort(remote.getPortOrDefault(defaultPort)); return socketAppender; } }
@Override public void init(SubsetConfiguration conf) { String host = conf.getString(HOST_PROPERTY); if (host != null) { this.host = host; } String port = conf.getString(PORT_PROPERTY); if (port != null) { this.port = Integer.parseInt(port); } String context = conf.getString(CONTEXT); if (context != null) { this.context = context; } PatternLayout layout = new PatternLayout("%d{ISO8601} %p %c: %m%n"); org.apache.log4j.net.SocketAppender appender = new org.apache.log4j.net.SocketAppender(this.host, this.port); appender.setName("chukwa.metrics." + this.context); appender.setLayout(layout); Logger logger = Logger.getLogger("chukwa.metrics." + this.context); logger.setAdditivity(false); logger.addAppender(appender); appender.activateOptions(); out = logger; }
/** * Creates a new appender. */ public AsyncSocketAppender() { socketAppender = new SocketAppender(); }
@Override public void append(final LoggingEvent event) { // Lazy attachment, to avoid calling non-final method in constructor if (!isAttached(socketAppender)) { addAppender(socketAppender); } // Lazy activation / connection too, to allow setting host and port if (activated.compareAndSet(false, true)) { socketAppender.activateOptions(); } super.append(event); }
/** Connect to the specified <b>RemoteHost</b> and <b>Port</b>. */ public void activateOptions() { connect(address, port); }
/** * The <b>RemoteHost</b> option takes a string value which should be * the host name of the server where a {@link SocketNode} is * running. * */ public void setRemoteHost(String host) { address = getAddressByName(host); remoteHost = host; }
/** * Close this appender. * * <p>This will mark the appender as closed and call then {@link * #cleanUp} method. * */ synchronized public void close() { if(closed) return; this.closed = true; cleanUp(); }
LogLog.warn("Detected problem with connection: "+e); if(reconnectionDelay > 0) { fireConnector();
PatternLayout layout = new PatternLayout("%d{ISO8601} %p %c: %m%n"); org.apache.log4j.net.SocketAppender appender = new org.apache.log4j.net.SocketAppender(host, port); appender.setName("chukwa.metrics." + contextName); appender.setLayout(layout); appender.activateOptions(); out = logger;
static { org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger(); final SocketAppender appender = new SocketAppender("192.168.1.4", 4445); root.addAppender(appender); } private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MyClass.class); static { logger.info("Hello logger"); }
/** Connects to remote server at <code>host</code> and <code>port</code>. */ public SocketAppender(String host, int port) { this.port = port; this.address = getAddressByName(host); this.remoteHost = host; connect(address, port); }
void connect(InetAddress address, int port) { if(this.address == null) return; try { // First, close the previous connection if any. cleanUp(); oos = new ObjectOutputStream(new Socket(address, port).getOutputStream()); } catch(IOException e) { String msg = "Could not connect to remote log4j server at [" +address.getHostName()+"]."; if(reconnectionDelay > 0) { msg += " We will try again later."; fireConnector(); // fire the connector thread } LogLog.error(msg, e); } }
/** Connects to remote server at <code>address</code> and <code>port</code>. */ public SocketAppender(InetAddress address, int port) { this.address = address; this.remoteHost = address.getHostName(); this.port = port; connect(address, port); }