Java.util.logging.Logger log output by means of a pass profile

Source: Internet
Author: User
Tags log4j

Transferred from: http://zochen.iteye.com/blog/616151

Simple implementation of the use of the JDK class Java.util.logging.Logger to record the log. The main reason is to use configuration files to configure the output of the log in the log4j manner. There are many articles on the web about how to use Java.util.logging.Logger, but there is no complete way to configure the configuration file to achieve control log output data. The purpose of this article is to welcome the shooting of bricks.
On the yards ...

1. First, a logmanager is encapsulated. The main function of this class is the code in the static block, which is intended to read the properties file and initialize the log attributes.

Java code
  1. Import java.io.IOException;
  2. Import Java.io.InputStream;
  3. Import Java.util.logging.Logger;
  4. Public class Logmanager {
  5. //Initialize Logmanager
  6. Static {
  7. //Read config file (file stream can also be used here)
  8. ClassLoader cl = Logmanager.  Class.getclassloader ();
  9. InputStream InputStream = null;
  10. if (cl! = null) {
  11. InputStream = Cl.getresourceasstream ("log.properties");
  12. } Else {
  13. InputStream = ClassLoader
  14. . Getsystemresourceasstream ("log.properties");
  15. }
  16. Java.util.logging.LogManager Logmanager = Java.util.logging.LogManager
  17. . Getlogmanager ();
  18. try {
  19. //re-initialize the log properties and reread the log configuration.
  20. Logmanager.readconfiguration (InputStream);
  21. } catch (SecurityException e) {
  22. System.err.println (e);
  23. } catch (IOException e) {
  24. System.err.println (e);
  25. }
  26. }
  27. /** 
  28. * Get Log objects
  29. * @param clazz
  30. * @return
  31. */
  32. public static Logger GetLogger (Class clazz) {
  33. Logger Logger = Logger
  34. . GetLogger (Clazz.getname ());
  35. return logger;
  36. }
  37. }



2. In the place where logging is required, the Logmanager.getlogger () method is used to get the logger object, and then the logging method is called. Here, I log the error stack information using the log (level, String msg, throwable thrown).


Post the contents of my properties profile, where the handlers attribute must not be forgotten, and I spend a lot of time looking for reasons for missing this attribute.

Java code
  1. #Level的五个等级SEVERE (highest value), WARNING, INFO, CONFIG, FINE, Finer, FINEST (lowest). This is different from log4j.
  2. #为 Handler Specifies the default level (default is Level.info).
  3. Java.util.logging.consolehandler.level=info
  4. # Specifies the name of the Formatter class to use (default is Java.util.logging.SimpleFormatter).
  5. Java.util.logging.consolehandler.formatter=java.util.logging.simpleformatter
  6. # Specifies the default level for Handler (default is Level.all).
  7. Java.util.logging.filehandler.level=info
  8. # Specifies the name of the Formatter class to use (default is Java.util.logging.XMLFormatter).
  9. Java.util.logging.filehandler.formatter=java.util.logging.simpleformatter
  10. # Specifies the approximate maximum amount, in bytes, to write to any file.   If the number is 0, there is no limit (the default is unrestricted).
  11. java.util.logging.filehandler.limit=1024000
  12. # Specifies how many output files participate in the loop (default is 1).
  13. java.util.logging.filehandler.count=1
  14. # Specify a pattern for the generated output file name.   For details, see the following (the default is "%h/java%u.log").
  15. Java.util.logging.filehandler.pattern=c:/sslog%u.log
  16. # Specifies whether Filehandler should be appended to any existing file (false by default ).
  17. Java.util.logging.filehandler.append=True
  18. handlers= Java.util.logging.consolehandler,java.util.logging.filehandler




This configuration also has a flaw, when the log file accumulates to the specified size, it will be recreated, that is, the previous log information will be lost, but the size of the message will cause a single file too large. The following blog post should be able to solve the problem, which has not taken time to scrutiny.
Custom file processor and log output format for java.util.logging

Java.util.logging.Logger log output by means of a pass profile

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.