Java Reference
In-Depth Information
Level.INFO
Level.CONFIG
Level.FINE
Level.FINER
Level.FINEST (lowest value)
I use info for audit logs and warning or severe for error logs. Lower levels are for de‐
bugging only and should not be used in production systems. Info, severe, and warning
all have convenience helper methods that log at that level. For example, this statement
logs a hit including the date and the remote address:
logger . info ( new Date () + " " + connection . getRemoteSocketAddress ());
You can use any format that's convenient for the individual log records. Generally, each
record should contain a timestamp, the client address, and any information specific to
the request that was being processed. If the log message represents an error, include the
specific exception that was thrown. Java fills in the location in the code where the mes‐
sage was logged automatically, so you don't need to worry about that.
Example 9-6 demonstrates by adding logging to the daytime server.
Example 9-6. A daytime server that logs requests and errors
import java.io.* ;
import java.net.* ;
import java.util.Date ;
import java.util.concurrent.* ;
import java.util.logging.* ;
public class LoggingDaytimeServer {
public final static int PORT = 13 ;
private final static Logger auditLogger = Logger . getLogger ( "requests" );
private final static Logger errorLogger = Logger . getLogger ( "errors" );
public static void main ( String [] args ) {
ExecutorService pool = Executors . newFixedThreadPool ( 50 );
try ( ServerSocket server = new ServerSocket ( PORT )) {
while ( true ) {
try {
Socket connection = server . accept ();
Callable < Void > task = new DaytimeTask ( connection );
pool . submit ( task );
} catch ( IOException ex ) {
errorLogger . log ( Level . SEVERE , "accept error" , ex );
} catch ( RuntimeException ex ) {
errorLogger . log ( Level . SEVERE , "unexpected error " + ex . getMessage (), ex );
Search WWH ::




Custom Search