1. 程式人生 > >tomcat 日誌 java.util.logging.Logger使用 (一)

tomcat 日誌 java.util.logging.Logger使用 (一)


1:java sdk 自帶的logger配置檔案位於 sdk home目錄下:

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

2:啟用該日誌配置有兩種方式

1)用java -Djava.util.logging.config.file=myfile屬性

java -Djava.util.logging.config.file=myfile  JavaClass
2)在程式中初始化LogManager ,該方式也適用於基於Tomcat等web專案
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();
		}

logger的主要配置資訊如下:

handlers 使用者配置log的處理方式,主要有java.util.logging.ConsoleHandler 用於往控制檯列印資訊

還有一個java.util.logging.FileHandler用於往檔案中寫資料

handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.FileHandler.pattern = %h/java%u.log  為生成的輸出檔名稱指定一個模式。
模式由包括以下特殊元件的字串組成,則執行時要替換這些元件:
  • "/" 本地路徑名分隔符
  • "%t" 系統臨時目錄
  • "%h" "user.home" 系統屬性的值  (win系統會在c:/users/username目錄下)
  • "%g" 區分迴圈日誌的生成號
  • "%u" 解決衝突的惟一號碼
  • "%%" 轉換為單個百分數符號"%"  

1):該屬性可以寫絕對路徑 如c:/xxxxx/java%u.log 但是不能寫c:/java%u.log即四個盤的根目錄不能寫

2):絕對路徑如果不存在的話會報錯  

Can't load log handler "java.util.logging.FileHandler"
java.io.IOException: Couldn't get lock for c:/java%u.log
java.io.IOException: Couldn't get lock for c:/java%u.log
	at java.util.logging.FileHandler.openFiles(FileHandler.java:372)
	at java.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)

解決辦法:

/**
	 * 初始化Logger相關的配置 主要包括,讀取指定指定目錄下的logger.properties檔案
	 */
	public static void init() {
		logManager = LogManager.getLogManager();
		// 重新讀取配置檔案
		// 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");
			// 如果不是以%開頭,則手動建立file檔案
			// 否則會報如下:java.io.IOException: Couldn't get lock for c:/xx/java%u.log類似的錯誤
			if (logPath.startsWith("%") == false) {
				File logFile = new File(logPath);
				// 注意不能指定c:/log.file 即win下四個硬碟的根目錄下不能存放
				if (logFile.getParentFile().exists() == false) {
					logFile.getParentFile().mkdirs();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inStream != null) {
					inStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

附上logging.properties的全文:

############################################################
#  	Default Logging Configuration File
#
# You can use a different 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 will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler

# To also add the FileHandler, 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 this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also 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 that 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