Results 1 to 2 of 2
  1. #1
    vrk's Avatar
    vrk
    vrk is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Question Maintaining logs of a chat session

    Hi All!

    I have a chat application to simply send the instant messages, which is working fine. And now i want to maintain a log using this apps.
    this apps is developed in java swing.
    I am unable to append the chat messages to a logger class in order to maintain the log tht should be in XML format.

    Plsss suggest me...how to proceed..

    Thanks in Advance!
    Vrk.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    I've written a logger in my application. Perhaps its code can help.
    Messages are sent to the log with calls like
    Util.log.severe("A terrible thing has happened");
    Variable log and class TagLogger are declared in Util.java.
    The default logger simply writes the log message to the console.
    Java Code:
    	static public class TagLogger extends Logger {
    		public TagLogger () {
    			super("com.physpics", null);
    		}
    		// interim logger until frame is created
    		@Override
    		public void log(Level level, String msg)
    			{ writeMesssageToConsole(level, msg); }
    	}
    Also in Utils.java is static code to create an instance of TagLogger
    and assign it to a global variable.
    Java Code:
    	static public TagLogger log;
    	public static void setLog(TagLogger newLog) {
    		log = newLog; 
    	}
    	static {   // establish an initial TagLogger; it writes to the console
    		setLog(new TagLogger());
    	}
    The main class of the application creates and installs a new TagLogger.
    It writes the message to both the console and a message area in the application:
    Java Code:
    		// create a logger that also sends the message to the messages panel
    		Util.setLog(new Util.TagLogger() {
    			@Override
    			public void log(Level level, String msg) {
    				// show message in application message area
    				frame.exposeError(msg, level);
    				// show message on console
    				Util.writeMesssageToConsole(level, msg, Tagger.this);
    			}
    		});
    The writeMessageToConsole method is declared in Util.java thus:
    Java Code:
    	/**
    	 * Extracts the last element of a fully dotted class name.
    	 * @param dotted A dotted class name.
    	 * @return the last segment of the name
    	 */
    	static String classSimpleName(String dotted) {
    		int dotx = dotted.lastIndexOf('.');
    		if (dotx < 0) return dotted;
    		return dotted.substring(dotx+1);
    	}
    
    	public static void writeMesssageToConsole(Level level, String msg) {
    		String msgInfo = "";
    		StackTraceElement[] elements = new Exception().getStackTrace();
    		msgInfo = elements[2].getMethodName();
    		msgInfo += " from "
    				+ classSimpleName(elements[3].getClassName())
    				+ "." + elements[3].getMethodName() + "()";
    		if (level.intValue() >= Level.WARNING.intValue())
    			System.err.println("*** " + msgInfo + "\n" + "      " + msg);
    		else
    			System.out.println(msgInfo + "\n" + "      " + msg);
    	}
    Creating XML is a bit of an additional challenge. One tutorial is at:
    Chapter*3.*Writing XML with Java

Similar Threads

  1. log4j - multiple logs with diffrent contents
    By ld09 in forum Java Applets
    Replies: 0
    Last Post: 10-08-2009, 12:36 PM
  2. Replies: 1
    Last Post: 04-22-2009, 12:20 AM
  3. Log4j problem - Logs are rolling into the previous date file
    By vaibhavborole in forum Advanced Java
    Replies: 0
    Last Post: 04-16-2009, 04:33 PM
  4. Replies: 2
    Last Post: 12-23-2008, 07:35 PM
  5. Replies: 0
    Last Post: 11-24-2008, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •