目前流行的日誌記錄工具配置大全(log4j,Jakarta Commons Logging,jdk1.4 logging)

來源:互聯網
上載者:User

How to do the initial setup of my logging framework
如何設定我的日誌架構。

原文來自於http://log4e.jayefem.de/index.php/Log4E:FAQ

If you are already familiar with your logging framework you might want to skip this section.

This chapter gives you a slight idea of how to make the initial setup of your logger. This is NOT supported by Log4E at the moment and was not the intended use at the beginning of this project.

Log4E does not ship any logging framework which means that you have to download and install it for yourself!

Examples are available for Log4j, Commons Logging, JDK 1.4 Logging (again: you have to do this for yourself).

Log4j
1. Download Log4j at http://logging.apache.org/ and copy the log4j.jar to your lib directory.

2. Create a new file 'log4j.properties' or 'log4j.xml' (case sensitive) and put it in your classpath. To be more concrete: Put in your source directory, the file will be copied by Eclipse automatically to your build directory.

For example:
.../MyEclipseProject/
.../MyEclipseProject/src/log4j.properties
.../MyEclipseProject/src/com/mycompany/myapp/...

2a. Alternatively, you can use the method org.apache.log4j.PropertyConfigurator.configure("..../log4j.properties");


3. Edit the 'log4j.properties' to declare your own categories, log levels and appenders (which means the output like standard out or a log file).

log4j.properties example:
#######################################################################
# Categories and levels
#######################################################################
log4j.rootCategory=ERROR, FileApp, ConApp
log4j.category.de.jayefem=DEBUG
#######################################################################
# Appenders
#######################################################################
# ConApp is set to be a ConsoleAppender.
log4j.appender.ConApp=org.apache.log4j.ConsoleAppender
# ConApp uses PatternLayout.
log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout
# Define Pattern
log4j.appender.ConApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FileApp
log4j.appender.FileApp=org.apache.log4j.RollingFileAppender
log4j.appender.FileApp.File=D:/proj/Devel/Java/de.jayefem.log4e/log/log4e.log
log4j.appender.FileApp.MaxFileSize=500KB
# Keep one backup file
log4j.appender.FileApp.MaxBackupIndex=1
log4j.appender.FileApp.layout=org.apache.log4j.PatternLayout
log4j.appender.FileApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

"de.jayefem", "ConApp", "FileApp" and the path to the logfile are selfdefined. All other words are keywords of Log4j.

4. That's all. Have fun.

Note that there are much more possibilities to configure Log4j. Check http://logging.apache.org/ for more.



Jakarta Commons Logging



Jakarta Commons Logging Framework is a wrapper for all common logging frameworks. If you want to use it, you have to install it AND the underlying logging framework. To install the Commons Logging download it from http://jakarta.apache.org/commons/logging/ and put the commons-logging.jar in your lib directory.
The Commons Logging Frameworks uses Log4j by default. When Log4j isn't found in classpath and JDK 1.4 or higher is being used, the JDK 1.4 logger will be used. If none of the above applies, Commons Logging will fall back to the internal SimpleLog.

It is also possible to specify the logging framework directly:

1. Create a new file 'commons-logging.properties' and put it in your classpath. To be more concrete: Put it in your source directory, the file will be copied by Eclipse automatically to your build directory.

For example:
.../MyEclipseProject/
.../MyEclipseProject/src/commons-logging.properties
.../MyEclipseProject/src/com/mycompany/myapp/...
2. Edit the 'commons-logging.properties'.

commons-logging.properties example:
#
#org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
# SimpleLog
#org.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog
# JDK 1.4 logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# Avalon Toolkit
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger
# Log4j
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger


As though it is not recommended to use Simplelog because it is not threadsafe, here's an example how to set it up:


1. Create a new file 'simplelog.properties' and put it in your classpath. To be more concrete: Put it in your source directory, the file will be copied by Eclipse automatically to your build directory.

For example:
.../MyEclipseProject/
.../MyEclipseProject/src/simplelog.properties
.../MyEclipseProject/src/com/mycompany/myapp/...
2. Edit the 'simplelog.properties' to declare your own categories and log levels.

simplelog.properties example:
# Default logging detail level for all instances of SimpleLog. Must be one of
# ("trace", "debug", "info", "warn", "error", or "fatal"). If not specified,
# defaults to "info".
org.apache.commons.logging.simplelog.defaultlog=warn
# Logging detail level for a SimpleLog instance named "xxxxx". Must be one of
# ("trace", "debug", "info", "warn", "error", or "fatal"). If not specified, the
# default logging detail level is used.
org.apache.commons.logging.simplelog.log.de.jayefem.log4e=debug
# Set to true if you want the Log instance name to be included in output
# messages. Defaults to false.
org.apache.commons.logging.simplelog.showlogname=false
# Set to true if you want the last componet of the name to be included in
# output messages. Defaults to true.
org.apache.commons.logging.simplelog.showShortLogname=true
# Set to true if you want the current date and time to be included in output
# messages. Default is false.
org.apache.commons.logging.simplelog.showdatetime=true


See http://jakarta.apache.org/commons/logging/ for more.



JDK 1.4 Logging


1. Use JDK 1.4 or higher :-)

2. Create a new file 'logging.properties'.

3. Invoke your application with:
java -Djava.util.logging.config.file=D:/your/path/to/logging.properties com.mycompany.myproject.MyClass


logging.properties example:
# handlers
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# general level
.level=INFO
# file handler
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
# console handler
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
test.de.jayefem.log4e.logkits.JDK1_4_Logging.level = FINEST

Since I am not an expert of JDK 1.4 logging there might be better ways to configure the logging framework. Suggestions are welcome.
See http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/package-summary.html for more information.

據說apache Jakarta小組曾經強烈建議sun把log4j作為jdk1.4的日誌工具,但這條建議沒有被採納。於是他們就開發了common logging 來相容log4j和jdk的logging,呵呵。簡單一點,還是使用log4j吧。
note:在eclipse中運行程式時似乎並不會搜尋原生classpath,因此即使把log4j.properties檔案放到classpath中也沒用。

最近發現Weblogic也是用的log4j,呵呵。


Trackback:http://www.blogjava.net/tedeyang/articles/18935.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.