HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Building the Ultimate Logging Solution"
Vol. 9, Issue 5, p. 32

	



Listing 1

1 import java.text.*;
2 import java.util.*;
3 import java.util.logging.*;
4 
5 
6 public class  SingleLineFormatter extends Formatter
7 {
8     private SimpleDateFormat dateformat = new
	  SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
9     private Date date = new Date();
10 
11     public String format(LogRecord record)
12     {
13         date.setTime(System.currentTimeMillis());
14 
15         return "["+dateformat.format(date)+"] "+record.getLevel()+":
"+record.getMessage()+"\r\n";
16     }
17 }

Listing 2

try
{
    Logger.getLogger("com.example.MyServer").log(new LogRecord(Level.FINEST, "This
	is a debug statement."));
    Logger.getLogger("com.example.MyServer").log(new LogRecord(Level.SEVERE, "This
	is an error statement."));
}
catch(Exception e)
{
    Logger.getLogger("com.example.MyServer").log(new LogRecord(Level.SEVERE, "An
	error occurred!", e);
}

Listing 3

1 import java.io.*;
2 import java.util.logging.*;
3 
4 public class LoggerOutputStream extends OutputStream
5 {
6     private Logger logger;
7     private StringBuffer buffer = new StringBuffer();
8     private Level level;
9 
10     public LoggerOutputStream(Logger logger)
11     {
12         this(logger, Level.INFO); 
13     }
14     
15     public LoggerOutputStream(Logger logger, Level level)
16     {
17         this.logger = logger;
18         this.level = level;
19     }
20 
21     public void write(int c) throws IOException
22     {
23         if(c == '\n')
24         {
25             logger.log(new LogRecord(level, buffer.toString()));
26             buffer = new StringBuffer();
27         }
28         else
29         {
30             buffer.append((char)c);
31         }
32     }
33 }

Listing 4

1 import java.io.*;
2 import java.util.*;
3 
4 public class MultiOutputStream extends OutputStream
5 {
6     private ThreadLocal lookup = new InheritableThreadLocal();
7     
8     public void write(int data) throws IOException
9     {
10         OutputStream out = (OutputStream)lookup.get();
11         
12         out.write(data);
13     }
14     
15     public void setOutputStream(OutputStream out)
16     {
17         lookup.set(out);
18     }
19 }

Listing 5

1 public class MyThread implements Runnable
2 {
3     private static MultiOutputStream sysout = new MultiOutputStream();
4     private static MultiOutputStream syserr = new MultiOutputStream();
5 
6     static {
7         System.setOut(sysout);
8         System.setErr(syserr);
9     }
10 
11     private Logger logger;
12 
13     public MyThread(Logger logger)
14     {
15         this.logger = logger;
16     }
17 
18     public void run()
19     {
20         sysout.setOutputStream(new LoggerOutputStream(logger));
21         syserr.setOutputStream(new LoggerOutputStream(logger, Level.SEVERE);
22 
23         //Do stuff here
24     }
25 }

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.