Tomcat Log Java.util.logging.Logger use (i)

Source: Internet
Author: User


The 1:java SDK has its own logger profile located in the SDK home directory:

Jdk1.6.0_25\jre\lib\logging.properties

2: There are two ways to enable this log configuration

1) with Java-djava.util.logging.config.file=myfile property

Java-djava.util.logging.config.file=myfile  Javaclass
2 Initialize Logmanager in the program, which also applies to Web projects based on Tomcat

Logmanager Logmanager = Logmanager.getlogmanager ();
		InputStream in;
		try {in
			= new FileInputStream (LoggerUtils.class.getResource ("/"). GetPath ()
					+ "conf/logging.properties");
			Logmanager.readconfiguration (in);
		} catch (Exception e) {
			e.printstacktrace ();
		}

The main configuration information for logger is as follows:

Handlers User Configuration log processing, mainly Java.util.logging.ConsoleHandler used to print information to the console

There is also a java.util.logging.FileHandler used to write data to the file

handlers= Java.util.logging.ConsoleHandler
# to also add the Filehandler and use the following line instead.
#handlers = Java.util.logging.FileHandler, Java.util.logging.ConsoleHandler
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 temp Directory "%h" "User.home" system property value (Win system will be in C:/users/username directory) "%g" distinguish circular log generation number "%u" The only one number "%" to resolve the conflict is converted to a single hundred Fractional symbol "%"

1): This property can write absolute path such as C:/xxxxx/java%u.log but cannot write c:/java%u.log the root directory of four disks cannot be written

2): Absolute path if not exist will be an error

Can ' t load log handler ' Java.util.logging.FileHandler ' java.io.IOException:Couldn ' t get lock for C:/java%u.log java.io.i Oexception:couldn ' t get lock for C:/java%u.log at Java.util.logging.FileHandler.openFiles (filehandler.java:372) at JAV A.util.logging.filehandler.<init> (filehandler.java:208) at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (nativeconstructoraccessorimpl.java:39) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (delegatingconstructoraccessorimpl.java:27) at
	Java.lang.reflect.Constructor.newInstance (constructor.java:513) at Java.lang.Class.newInstance0 (class.java:355) At Java.lang.Class.newInstance (class.java:308) at Java.util.logging.logmanager$3.run (logmanager.java:358) at Java.security.AccessController.doPrivileged (Native method) at Java.util.logging.LogManager.loadLoggerHandlers ( logmanager.java:344) at Java.util.logging.LogManager.initializeGlobalHandlers (Logmanager.java:909) at java.util.logging.logmanager.access$900 (logmanager.java:128) 

Solution:

/** * Initialization Logger related configuration consists mainly of reading the logger.properties file specified in the specified directory/public static void Init () {Logmanager = Logmanager.getl
		Ogmanager ();
		Re-read configuration file//Classloaderlogmanager Logmanager = new Classloaderlogmanager ();
		InputStream instream = null;
			try {instream = new FileInputStream (Log.class.getResource ("/"). GetPath () + "conf/logging.properties");

			Logmanager.readconfiguration (instream);
			String LogPath = Logmanager. GetProperty ("Java.util.logging.FileHandler.pattern"); If you do not start with%, manually create the file files//Otherwise it will be reported as follows: Java.io.IOException:Couldn ' t get lock for C:/xx/java%u.log similar error if (Logpath.start
				Swith ("%") = = False) {File LogFile = new file (LogPath); Note You cannot specify C:/log.file (Logfile.getparentfile (). Exists () = False) {Logfile.getparentfile () in the root directory of the four hard drives under win.
				Mkdirs ();
		A catch (Exception e) {e.printstacktrace ());
				Finally {try {if (instream!= null) {instream.close (); } catch (IOException e) {E.prinTstacktrace ();
 }
		}
	}

Attach the full text of logging.properties:

############################################################ # Default Logging Configuration File # can use a diff  
Erent file by specifying a filename # with the Java.util.logging.config.file system property. # For example Java-djava.util.logging.config.file=myfile ######################################################### ### ############################################################ # Global Properties ##############################  ############################## # "Handlers" specifies a comma separated list of log Handler # classes.
These handlers would be installed during VM startup.
# These classes must are on the system classpath.
# By default We only configure a consolehandler, which'll only # show messages at the INFO and above levels.
Handlers= Java.util.logging.ConsoleHandler # To also add the Filehandler and use the following line instead.
#handlers = Java.util.logging.FileHandler, Java.util.logging.ConsoleHandler # Default global logging level. # this Specifies which kinds of events are logged across # all loggers. For any given facility the ' global level # can is overriden by a facility specific level # of that Consolehandler al
So has a separate level # setting to limit messages printed to the console.
. level= WARNING ############################################################ # Handler specific properties.
# describes specific configuration info for handlers.
############################################################ # Default file output is in user ' s home directory.
Java.util.logging.FileHandler.pattern =%h/java%u.log Java.util.logging.FileHandler.limit = 50000 Java.util.logging.FileHandler.count = 1 Java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter #
Limit The message this are printed on the console to INFO and above. Java.util.logging.ConsoleHandler.level = WARNING Java.util.logging.ConsoleHandler.formatter = Java.util.logging.SimpleFormatter ############################################################ # Facility specific properties.
# provides extra control for each logger. 
############################################################ # For example, set the Com.xyz.foo logger to only log SEVERE
 # messages:com.xyz.foo.level = SEVERE

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.