17 Javalogging
17 Javalogging
Sang Shin
Java Technology Architect
Sun Microsystems, Inc.
[email protected]
www.javapassion.com
Topics
• What is and Why Java logging?
• Architecture of Java logging framework
• Logging example
• Logging levels
• Loggers
• Handlers
• Formatters
• Configuration
• Logging and performance
2
What is & Why
Java Logging API?
What is a Java Logging API?
4
Why Use Java Logging API?
• Facilitate software servicing and maintenance at
customer sites by producing log reports suitable for
analysis by end users, system administrators, field
service engineers, and software development teams
> Capture information such as security failures, configuration
errors, performance bottlenecks, and/or bugs in the
application or platform
5
Architecture of Java
Logging Framework
Logger and Handler
• Applications make logging calls on Logger objects.
• The Logger objects allocate LogRecord objects which are
passed to Handler objects for publication.
7
Filter and Formatter
8
Logging Example First
Example
package com.wombat;
public class Nose{
// Obtain a suitable logger.
private static Logger logger = Logger.getLogger("com.wombat.nose");
public static void main(String argv[]){
// Log a FINE tracing message
logger.fine("doing stuff");
try{
Wombat.sneeze();
} catch (Error ex){
// Log the error
logger.log(Level.WARNING,"trouble sneezing",ex);
}
logger.fine("done");
}
}
10
Logging Levels
Logging Levels
12
Loggers
Logger
14
Handlers
Handlers
• StreamHandler
> A simple handler for writing formatted records to an OutputStream.
• ConsoleHandler
> A simple handler for writing formatted records to System.err
• FileHandler
> A handler that writes formatted log records either to a single file, or
to a set of rotating log files.
• SocketHandler
> A handler that writes formatted log records to remote TCP ports.
• MemoryHandler
> A handler that buffers log records in memory.
16
Set up its own Logging Handler
package com.wombat;
import java.util.logging.*;
public class Nose {
private static Logger logger = Logger.getLogger("com.wombat.nose");
private static FileHandler fh = new FileHandler("mylog.txt");
public static void main(String argv[]) {
// Send logger output to our FileHandler.
logger.addHandler(fh);
// Request that every detail gets logged.
logger.setLevel(Level.ALL);
// Log a simple INFO message.
logger.info("doing stuff");
try {
Wombat.sneeze();
} catch (Error ex) {
logger.log(Level.WARNING, "trouble sneezing", ex);
}
logger.fine("done");
}
}
17
Logging Methods
Logging Methods
19
Formatters
Two Standard Formaters
• SimpleFormatter
> Writes brief "human-readable" summaries of log records.
• XMLFormatter
> Writes detailed XML-structured information.
21
Sample XML Output
22
LogManager
LogManager
• There is a global LogManager object that keeps track of
global logging information
> A hierarchical namespace of named Loggers
> A set of logging control properties read from the configuration
file
• A LogManager object can be retrieved using the static
LogManager.getLogManager() method
• LogManager object is created during LogManager
initialization, based on a system property
> This property allows container applications (such as EJB
containers) to substitute their own subclass of
LogManager in place of the default class.
24
Configuration File
Configuration File
26
Changing Configuration
// Dynamically adjust the logging configuration to send output
// to a specific file and to get lots of information on wombats
public static void main(String[] args){
Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);
Logger.getLogger("com.wombat").setLevel("com.wombat",Level.FINEST);
...
}
27
Logging &
Performance Implication
Logging can be disabled
29
Java Logging
Sang Shin
Java Technology Architect
Sun Microsystems, Inc.
[email protected]
www.javapassion.com