001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.net; 015 016import java.io.BufferedInputStream; 017import java.io.IOException; 018import java.net.Socket; 019import java.net.SocketAddress; 020 021import ch.qos.logback.classic.Logger; 022import ch.qos.logback.classic.LoggerContext; 023import ch.qos.logback.classic.net.server.HardenedLoggingEventInputStream; 024import ch.qos.logback.classic.spi.ILoggingEvent; 025 026// Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu> 027 028/** 029 * Read {@link ILoggingEvent} objects sent from a remote client using Sockets 030 * (TCP). These logging events are logged according to local policy, as if they 031 * were generated locally. 032 * 033 * <p> 034 * For example, the socket node might decide to log events to a local file and 035 * also resent them to a second socket node. 036 * 037 * @author Ceki Gülcü 038 * @author Sébastien Pennec 039 * 040 * @since 0.8.4 041 */ 042public class SocketNode implements Runnable { 043 044 Socket socket; 045 LoggerContext context; 046 HardenedLoggingEventInputStream hardenedLoggingEventInputStream; 047 SocketAddress remoteSocketAddress; 048 049 Logger logger; 050 boolean closed = false; 051 SimpleSocketServer socketServer; 052 053 public SocketNode(SimpleSocketServer socketServer, Socket socket, LoggerContext context) { 054 this.socketServer = socketServer; 055 this.socket = socket; 056 remoteSocketAddress = socket.getRemoteSocketAddress(); 057 this.context = context; 058 logger = context.getLogger(SocketNode.class); 059 } 060 061 // public 062 // void finalize() { 063 // System.err.println("-------------------------Finalize called"); 064 // System.err.flush(); 065 // } 066 067 public void run() { 068 069 try { 070 hardenedLoggingEventInputStream = new HardenedLoggingEventInputStream( 071 new BufferedInputStream(socket.getInputStream())); 072 } catch (Exception e) { 073 logger.error("Could not open ObjectInputStream to " + socket, e); 074 closed = true; 075 } 076 077 ILoggingEvent event; 078 Logger remoteLogger; 079 080 try { 081 while (!closed) { 082 // read an event from the wire 083 event = (ILoggingEvent) hardenedLoggingEventInputStream.readObject(); 084 // get a logger from the hierarchy. The name of the logger is taken to 085 // be the name contained in the event. 086 remoteLogger = context.getLogger(event.getLoggerName()); 087 // apply the logger-level filter 088 if (remoteLogger.isEnabledFor(event.getLevel())) { 089 // finally log the event as if was generated locally 090 remoteLogger.callAppenders(event); 091 } 092 } 093 } catch (java.io.EOFException e) { 094 logger.info("Caught java.io.EOFException closing connection."); 095 } catch (java.net.SocketException e) { 096 logger.info("Caught java.net.SocketException closing connection."); 097 } catch (IOException e) { 098 logger.info("Caught java.io.IOException: " + e); 099 logger.info("Closing connection."); 100 } catch (Exception e) { 101 logger.error("Unexpected exception. Closing connection.", e); 102 } 103 104 socketServer.socketNodeClosing(this); 105 close(); 106 } 107 108 void close() { 109 if (closed) { 110 return; 111 } 112 closed = true; 113 if (hardenedLoggingEventInputStream != null) { 114 try { 115 hardenedLoggingEventInputStream.close(); 116 } catch (IOException e) { 117 logger.warn("Could not close connection.", e); 118 } finally { 119 hardenedLoggingEventInputStream = null; 120 } 121 } 122 } 123 124 @Override 125 public String toString() { 126 return this.getClass().getName() + remoteSocketAddress.toString(); 127 } 128}