1 29 30 package echo2example.chatserver; 31 32 import java.io.PrintStream ; 33 import java.text.DateFormat ; 34 import java.util.Date ; 35 36 40 public class Log { 41 42 private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 43 44 public static final int ACTION_POST = 1; 45 public static final int ACTION_AUTH = 2; 46 public static final int ACTION_EXIT = 3; 47 48 private static PrintStream logStream = System.err; 49 50 55 public static final void setLogStream(PrintStream logStream) { 56 Log.logStream = logStream; 57 } 58 59 72 public static final void log(int action, String remoteHost, String userName, String message) { 73 if (logStream == null) { 74 return; 76 } 77 String time = DATE_FORMAT.format(new Date ()); 78 String logMessage; 79 switch (action) { 80 case ACTION_POST: 81 logMessage = "ECHO2CHAT[" + time + "] POST: remoteHost=" + remoteHost + ", userName=" + userName 82 + ", message=" + message; 83 break; 84 case ACTION_AUTH: 85 logMessage = "ECHO2CHAT[" + time + "] AUTH: remoteHost=" + remoteHost + ", userName=" + userName; 86 break; 87 case ACTION_EXIT: 88 logMessage = "ECHO2CHAT[" + time + "] EXIT: remoteHost=" + remoteHost + ", userName=" + userName; 89 break; 90 default: 91 throw new IllegalArgumentException ("Invalid action."); 92 } 93 logStream.println(logMessage); 94 } 95 } 96 | Popular Tags |