The current popular Logging tool configuration encyclopedia (Log4j,jakarta Commons logging,jdk1.4 Logging)

Source: Internet
Author: User
Tags log4j

The initial setup of my logging framework
How to set up my log frame.

The original text comes from Http://log4e.jayefem.de/index.php/Log4E:FAQ

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

This is chapter gives you a slight idea of how to make the initial setup of your logger. This isn't supported by log4e at the moment and the ' not ' intended use in the beginning of this project.

LOG4E does not ship the any logging framework which means, have to download and install it for yourself!

Examples are available for log4j, Commons Logging, JDK 1.4 Logging (Again:you have to does this for yourself).

Log4j
1. Download log4j at Http://logging.apache.org/and copy of 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 is more concrete:put in your source directory, the file would be copied by Eclipse automatically to your build Director Y.

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

2a. Alternatively, 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 stand ARD 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 this 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 to all common Logging. If you are want to use it, your 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 are being used, the JDK 1.4 logger would be used. If None of the above applies, Commons Logging'll 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 is more concrete:put it in your source directory, the file would be copied by Eclipse automatically to your build Direc Tory.

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 isn't recommended to use simplelog because it isn't threadsafe, here's a example how to set it up:


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

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 to 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 your want the Log instance name to is included in output
# messages. Defaults to False.
Org.apache.commons.logging.simplelog.showlogname=false
# Set to True if you are want the last componet of the ' name to ' is included in
# output messages. Defaults to True.
Org.apache.commons.logging.simplelog.showshortlogname=true
# Set to True if you want the ' current date and ' to ' is included in output
# messages. The Default is false.
Org.apache.commons.logging.simplelog.showdatetime=true


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 a expert of JDK 1.4 logging there might to better the ways framework. Suggestions are welcome.
Http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/package-summary.html for more information.

It is said that the Apache Jakarta team had strongly advised Sun to use log4j as a jdk1.4 log tool, but this suggestion was not adopted. So they developed a common logging to compatible log4j and JDK logging, hehe. To be simple, use log4j.
Note: Running programs in Eclipse does not seem to search for native classpath, so even putting log4j.properties files into classpath is useless.

Recently found WebLogic is also used log4j, hehe.


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

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.