zoukankan      html  css  js  c++  java
  • Java Logging: Formatters

     

    The Handler's in the Java Logging API use a java.util.logging.Formatter to format theLogRecord's before writing it to an external system.

    Java comes with two built-in Formatter's (subclasses of Formatter):

    1. SimpleFormatter
    2. XMLFormatter

    The various Handler's in the Java Logging API use either of these two Formatter's by default, but you can also set your own custom Formatter subclass on a Handler.

    You can create your own Formatter by subclassing the java.util.logging.Formatter class. Here is a simple example:

    public class MyFormatter extends Formatter {
    
        @Override
        public String format(LogRecord record) {
            return record.getLevel() + ":" + record.getMessage();
        }
    }
    

    The subclass must override the abstract format() method in the Formatter class. The String returned by theformat() is what is forwarded to the external system by the Handler. Exactly how the string should be formatted is up to you.

    The Formatter class also contains the convenience method formatMessage() which can be used to format the message using the ResourceBundle of the LogRecord. Here is an example:

    public class MyFormatter extends Formatter {
    
        @Override
        public String format(LogRecord record) {
            return formatMessage(record);
        }
    }
    

    You will not often need to implement your own Formatter, but once in a while, if a specific log format is required, a custom Formatter may be useful.

  • 相关阅读:
    poj2954
    bzoj1863
    bzoj2002
    bzoj1389
    [POJ3041] Asteroids(最小点覆盖-匈牙利算法)
    [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
    [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
    [luoguP1266] 速度限制(spfa)
    [luoguP1186] 玛丽卡(spfa)
    [luoguP1027] Car的旅行路线(Floyd)
  • 原文地址:https://www.cnblogs.com/hephec/p/4579625.html
Copyright © 2011-2022 走看看