java.util.logging 例子

Logging

java.util.logging

import java.util.logging.*;
public class Main {    public static void main(String[] args) {
        Logger log = Logger.getLogger("test"); 
        log.setLevel(Level.INFO); 
        log.info("--------------------------");
        log.info("Test");
        log.info("--------------------------");

    }
}

 XML

import java.io.IOException;
import java.util.logging.*;
public class Main {    public static void main(String[] args) {        try {
            Logger log = Logger.getLogger("test");
            FileHandler fileHandler = new FileHandler("test.%g.log");
            fileHandler.setLevel(Level.INFO);
            log.addHandler(fileHandler);

            log.setLevel(Level.INFO);
            log.info("One");
            log.info("Two");
            log.info("Three");

        } catch (SecurityException e) {            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

输出结果

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE log SYSTEM "logger.dtd"><log><record>
  <date>2016-04-19T15:57:19</date>
  <millis>1461052639360</millis>
  <sequence>0</sequence>
  <logger>test</logger>
  <level>INFO</level>
  <class>Main</class>
  <method>main</method>
  <thread>1</thread>
  <message>One</message></record><record>
  <date>2016-04-19T15:57:19</date>
  <millis>1461052639394</millis>
  <sequence>1</sequence>
  <logger>test</logger>
  <level>INFO</level>
  <class>Main</class>
  <method>main</method>
  <thread>1</thread>
  <message>Two</message></record><record>
  <date>2016-04-19T15:57:19</date>
  <millis>1461052639395</millis>
  <sequence>2</sequence>
  <logger>test</logger>
  <level>INFO</level>
  <class>Main</class>
  <method>main</method>
  <thread>1</thread>
  <message>Three</message></record></log>

Formatter

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
class LogFormatter extends Formatter {    @Override
    public String format(LogRecord record) {        return String.format("%s %s\t%s\n", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) , record.getLevel(), record.getMessage());
    }
}public class Main {    public static void main(String[] args) {        try {
            Logger log = Logger.getLogger("test");
            FileHandler fileHandler = new FileHandler("test.%g.log");
            fileHandler.setLevel(Level.INFO);
            log.addHandler(fileHandler);
            fileHandler.setFormatter(new LogFormatter());
            log.setLevel(Level.INFO);
            log.info("One");
            log.info("Two");
            log.info("Three");

        } catch (SecurityException e) {            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

输出结果

2016-04-19 16:05:53.324 INFO    One
2016-04-19 16:05:53.352 INFO    Two
2016-04-19 16:05:53.353 INFO    Three

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2016-04-20

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏算法修养

POJ-1887 Testing the CATCHER(dp,最长下降子序列)

Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submi...

3585
来自专栏ml

uva-----11292 The Dragon of Loowater

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, ...

3105
来自专栏ml

HDUOJ--1865 1string

1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

2176
来自专栏前端儿

1000. A-B

Time Limit: 1 secs, Memory Limit: 32 MB                                         ...

1131
来自专栏ml

HDUOJ---The number of divisors(约数) about Humble Numbers

The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/O...

3375
来自专栏函数式编程语言及工具

Akka(40): Http:Marshalling reviewed - 传输数据序列化重温

   上篇我们讨论了Akka-http的文件交换。由于文件内容编码和传输线上数据表达型式皆为bytes,所以可以直接把文件内容存进HttpEntity中进行传递...

2278
来自专栏算法修养

ZOJ 3607 Lazier Salesgirl (枚举)

Lazier Salesgirl Time Limit: 2 Seconds Memory Limit: 65536 KB Kochiya S...

3607
来自专栏Java学习123

maven settings.xml 包含多个镜像库

2957
来自专栏ml

hdu 1805Expressions(二叉树构造的后缀表达式)

Expressions Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

3366
来自专栏算法修养

POJ--3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K ...

3344

扫码关注云+社区