Results 1 to 2 of 2
- 10-27-2009, 08:00 AM #1
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.
- 10-27-2009, 03:18 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
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.
Also in Utils.java is static code to create an instance of TagLoggerJava 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); } }
and assign it to a global variable.
The main class of the application creates and installs a new TagLogger.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()); }
It writes the message to both the console and a message area in the application:
The writeMessageToConsole method is declared in Util.java thus: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); } });
Creating XML is a bit of an additional challenge. One tutorial is at: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); }
Chapter*3.*Writing XML with Java
Similar Threads
-
log4j - multiple logs with diffrent contents
By ld09 in forum Java AppletsReplies: 0Last Post: 10-08-2009, 11:36 AM -
How to Kill session at application context level by using session Id
By Kishore.Kumar in forum Java ServletReplies: 1Last Post: 04-21-2009, 11:20 PM -
Log4j problem - Logs are rolling into the previous date file
By vaibhavborole in forum Advanced JavaReplies: 0Last Post: 04-16-2009, 03:33 PM -
How do I create session for a user when S/he login and expire the session.
By dpk_vash in forum Web FrameworksReplies: 2Last Post: 12-23-2008, 06:35 PM -
Track Java web application logs in Firefox using LogDigger
By martinw in forum Java SoftwareReplies: 0Last Post: 11-24-2008, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks