Logging
CS 240 – Advanced Programming Concepts
Java Logging
Java Logging: Log Messages
3
Java Logging: Loggers
4
Java Logging: Handlers
5
Java Logging: Formatters
6
BLANK SLIDE
Configuration
Programmatic Configuration
public class ProgrammaticConfigurationExample {
� private static Logger logger;�� static {� try {� initLog();� } catch (IOException e) {� System.out.println("Could not initialize log: " + e.getMessage());� }� }�� private static void initLog() throws IOException {�� Level logLevel = Level.FINEST;�� logger = Logger.getLogger("ProgrammaticConfigurationExample");� logger.setLevel(logLevel);� logger.setUseParentHandlers(false);�� Handler consoleHandler = new ConsoleHandler();� consoleHandler.setLevel(logLevel);�// consoleHandler.setFormatter(new SimpleFormatter());� logger.addHandler(consoleHandler);�� FileHandler fileHandler = new FileHandler("log.txt", false);� fileHandler.setLevel(logLevel);�// fileHandler.setFormatter(new SimpleFormatter());� logger.addHandler(fileHandler);� }
}
9
Configuration File Example
import java.util.logging.Level;�import java.util.logging.Logger;��/**� * Specify the configuration file by adding the following VM option
* in the run configuration or you will get the default logging
* configuration:� *� * -Djava.util.logging.config.file=logging.properties� *� */�public class FileConfigurationExample {�� private static Logger logger =
Logger.getLogger("FileConfigurationExample");��}
10
Logging Configuration File
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler�java.util.logging.ConsoleHandler.level=FINE�java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter��java.util.logging.FileHandler.level=FINE�java.util.logging.FileHandler.pattern=log.txt��# Write 10MB before rotating this file�java.util.logging.FileHandler.limit=10000000��# Number of rotating files to be used�java.util.logging.FileHandler.count=4�java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter��# Log format to use�java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n��.level=FINEST
11
Log Formatting
12
BLANK SLIDE
Logging Messages
Logging Methods
15
Logging Example 1
import java.util.logging.Level;�import java.util.logging.Logger;��/**� * Specify the configuration file by adding the following VM option in the run configuration� * or you will get the default logging configuration:� *� * -Djava.util.logging.config.file=logging.properties� *� */�public class FileConfigurationExample {�� private static Logger logger = Logger.getLogger("FileConfigurationExample");�� public static void main(String [] args) {� logger.info("This is my info log message.");� logger.fine("This is my fine log message");�� try {� throw new Exception("An exception occurred");� } catch (Exception ex) {� logger.severe("This is my exception message");� logger.log(Level.SEVERE, "This is my message logged with the exception", ex);� }� }�}
16
Logging Example 2
import java.util.logging.Level;�import java.util.logging.Logger;
public class Server {
private static Logger logger = Logger.getLogger(”Server");�
private void run() {
logger.info("Initializing HTTP Server");
try {
server = HttpServer.create(new InetSocketAddress(SERVER_PORT_NUMBER),
MAX_WAITING_CONNECTIONS);
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
return;
}
logger.info("Creating contexts");
server.createContext("/games/list", new ListGamesHandler());
server.createContext("/routes/claim", new ClaimRouteHandler());
logger.info("Starting HTTP Server");
server.start();
}
}
17
Logging Example 3
class ClaimRouteHandler implements HttpHandler {
private static Logger = Logger.getLogger("ClaimRouteHandler");
@Override
public void handle(HttpExchange exchange) throws IOException {
logger.entering("ClaimRouteHandler", "handle");
try {
...
logger.fine(reqData);
...
}
catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
...
}
logger.exiting("ClaimRouteHandler", "handle");
}
...
}
18
Log4J
19
BLANK SLIDE