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

來源:互聯網
上載者:User


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.logjava.io.IOException: Couldn't get lock for c:/java%u.logat 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.logjava.util.logging.FileHandler.limit = 50000java.util.logging.FileHandler.count = 1java.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.