Use and configuration of Java Logger

Source: Internet
Author: User
Tags stringbuffer
Logger the corresponding property file in the installation JDK directory Jre/lib/logging.properties (for example, in my current machine is C:\Program files\java\jre6\lib\ Logging.properties)
Logging.properties file (logging.properties file contains logger configuration information, including where the log is exported, what level, etc.)
handlers= Java.util.logging.ConsoleHandler output Log content to the console
handlers= Java.util.logging.FileHandler The log file to the file
handlers= Java.util.logging.consolehandler,java.util.logging.filehandler Output The log contents to the console and files at the same time

# LIMIT The message this are printed on the console to INFO and above.
Java.util.logging.ConsoleHandler.level = INFO//log Output level

Java.util.logging.FileHandler.pattern =%h/java%u.log Specifies a pattern for the generated output file name.
The pattern consists of strings that include the following special components, and the runtime replaces these components:
"/" Local path name separator "%t" System temporary Directory "%h" "User.home" System Properties Value "%g" distinguishes circular log generation number "%u" the only number "%" to resolve conflicts is converted to a single percent symbol "%"
If the%g field is not specified and the file count is greater than 1, the build number is added after the decimal point at the end of the generated file name.

Java.util.logging.FileHandler.limit = 50000 limits the size of the file, in bytes
Java.util.logging.FileHandler.count = 1 Specifies how many output files participate in the loop (default is 1).
Java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter Specifies the name of the formatter class to use (default is Java.util.logging . Xmlformatter). The other one is: java.util.logging simpleformatter. Xmlformatter is output in XML style, Simpleformatter is output in Normal style.
JAVA.UTIL.LOGGING.FILEHANDLER.APPEND Specifies whether the Filehandler should be appended to any existing file (the default is False).

Here's an example of how I implement log output in a class

Import Java.io.File;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.logging.ConsoleHandler;
Import Java.util.logging.FileHandler;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;

Import Java.util.logging.SimpleFormatter; /** * Log output to the login system users in the root directory of the Postal Savings log folder, * And according to the system date named log file * @author Wang * */public class Loggerutil {/** stored in the text
    
    Clip **/private static String file_name = "Postal Savings journal";  /** * Get the path and file name of the log to be logged * @return/private static String Getlogname () {StringBuffer LogPath =
        New StringBuffer ();
        Logpath.append (System.getproperty ("User.home"));
        Logpath.append ("\" +file_name);
        File File = new file (logpath.tostring ());
        
        if (!file.exists ()) File.mkdir ();
        SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
        
        Logpath.append ("\" +sdf.format (new Date ()) + ". Log"); Return logpath.tostring(); 
     /** * Configure Logger Object output log file path * @param logger * @throws SecurityException * @throws IOException */public static void Setlogingproperties (Logger Logger) throws SecurityException, IOException {Setlogi
    Ngproperties (Logger,level.all); /** * Configure Logger Object output log file path * @param logger * @param level the information in the log file that is above levels is exported * @throws SECU
        Rityexception * @throws IOException */public static void Setlogingproperties (Logger logger,level level) {
        Filehandler FH;
            try {fh = new Filehandler (Getlogname (), true);
            Logger.addhandler (FH);//Log output file//logger.setlevel (level); Fh.setformatter (New Simpleformatter ());//output Format//logger.addhandler (new Consolehandler ());//output to console} cat
        CH (SecurityException e) {logger.log (Level.severe, "Security error", E);
catch (IOException e) {logger.log (level.severe, "read file log Error", E);        The public static void main (String [] args) {Logger Logger = Logger.getlogger ("Sgg");
            try {loggerutil.setlogingproperties (logger);
            Logger.log (Level.info, "ddddd");
            Logger.log (Level.info, "eeeeee");
            Logger.log (Level.info, "ffffff");
            Logger.log (Level.info, "gggggg");
        Logger.log (Level.info, "hhhhhh");
        catch (SecurityException e) {//TODO auto-generated catch block E.printstacktrace ();
        catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
        
    }
}
Note: Logger Logger = Logger.getlogger ("Sgg");  SGG can only occur once, otherwise repeat output values
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.