MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;

public class MainClass {
    public static void main(String args[]) {

        Logger logger = Logger.getLogger("my.log");

        Handler handler = null;

        try {
            handler = new FileHandler("messages.log");
        } catch (IOException e) {
            System.out.println("Could not create file. Using the console handler");
            handler = new ConsoleHandler();
        }

        logger.addHandler(handler);

        handler.setFormatter(new XMLFormatter());

        logger.info("Our first logging message");
        logger.severe("Something terrible happened");
    }
}