Example usage for java.util.logging FileHandler FileHandler

List of usage examples for java.util.logging FileHandler FileHandler

Introduction

In this page you can find the example usage for java.util.logging FileHandler FileHandler.

Prototype

public FileHandler(String pattern) throws IOException, SecurityException 

Source Link

Document

Initialize a FileHandler to write to the given filename.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    logger.addHandler(new FileHandler("LogToFile.xml"));
    logger.info("A message logged to the file");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileHandler logFile = new FileHandler("LogToFile2.txt");
    logFile.setFormatter(new SimpleFormatter());
    logger.addHandler(logFile);/* w  w w.  ja  v a 2  s  . c  om*/
    logger.info("A message logged to the file");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Logger logger = Logger.getLogger("com.mycompany");
    FileHandler fh = new FileHandler("mylog.txt");
    fh.setFormatter(new SimpleFormatter());
    logger.addHandler(fh);//from w w  w  .j  a v  a2  s.  c  o  m

    // fh = new FileHandler("mylog.xml");
    // fh.setFormatter(new XMLFormatter());
    // logger.addHandler(fh);

    // Log a few messages
    logger.severe("my severe message");
    logger.warning("my warning message");
    logger.info("my info message");
    logger.config("my config message");
    logger.fine("my fine message");
    logger.finer("my finer message");
    logger.finest("my finest message");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileHandler logFile = new FileHandler("MultipleHandlers.xml");
    logger.addHandler(logFile);// w w  w .  ja va 2 s .  c o m
    logger.addHandler(new ConsoleHandler());
    logger.warning("Output to multiple handlers");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileHandler logFile = new FileHandler("MultipleHandlers2.xml");
    logger.addHandler(logFile);/*from w w  w  .j  a  va 2  s . c o m*/
    logger.addHandler(new ConsoleHandler());
    logger.setUseParentHandlers(false);
    logger.warning("Output to multiple handlers");
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    LogManager lm = LogManager.getLogManager();
    Logger logger;//  ww  w. j a v a  2s. c  om
    FileHandler fh = new FileHandler("log_test.txt");

    logger = Logger.getLogger("LoggingExample1");

    lm.addLogger(logger);
    logger.setLevel(Level.INFO);
    fh.setFormatter(new XMLFormatter());

    logger.addHandler(fh);
    //logger.setUseParentHandlers(false);
    logger.log(Level.INFO, "test 1");
    logger.log(Level.INFO, "test 2");
    logger.log(Level.INFO, "test 3");
    fh.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLFormatter formatter = new XMLFormatter();
    LogRecord record = new LogRecord(Level.INFO, "XML message..");

    FileHandler handler = new FileHandler("newxml.xml");
    handler.setFormatter(formatter);//from   w  ww  .  j  av  a 2s  .  co  m

    handler.publish(record);
    handler.flush();
}

From source file:LoggingExample1.java

public static void main(String args[]) {
    try {/* w w  w . j  a  v  a2s . c  om*/
        LogManager lm = LogManager.getLogManager();
        Logger logger;
        FileHandler fh = new FileHandler("log_test.txt");

        logger = Logger.getLogger("LoggingExample1");

        lm.addLogger(logger);
        logger.setLevel(Level.INFO);
        fh.setFormatter(new XMLFormatter());

        logger.addHandler(fh);
        //logger.setUseParentHandlers(false);
        logger.log(Level.INFO, "test 1");
        logger.log(Level.INFO, "test 2");
        logger.log(Level.INFO, "test 3");
        fh.close();
    } catch (Exception e) {
        System.out.println("Exception thrown: " + e);
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String args[]) {

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

    Handler handler = null;/* w  w w  . ja  v a  2s  . com*/

    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");
}

From source file:HTMLFormatter.java

public static void main(String args[]) throws Exception {
    LogManager lm = LogManager.getLogManager();
    Logger parentLogger, childLogger;
    FileHandler xml_handler = new FileHandler("log_output.xml");
    FileHandler html_handler = new FileHandler("log_output.html");
    parentLogger = Logger.getLogger("ParentLogger");
    childLogger = Logger.getLogger("ParentLogger.ChildLogger");

    lm.addLogger(parentLogger);/*from   w w w  .  j av  a  2  s  . c o m*/

    lm.addLogger(childLogger);

    parentLogger.setLevel(Level.WARNING);
    childLogger.setLevel(Level.ALL);
    xml_handler.setFormatter(new XMLFormatter());
    html_handler.setFormatter(new HTMLFormatter());

    parentLogger.addHandler(xml_handler);
    childLogger.addHandler(html_handler);

    childLogger.log(Level.FINE, "This is a fine log message");
    childLogger.log(Level.SEVERE, "This is a severe log message");
    xml_handler.close();
    html_handler.close();
}