Config log4j

 reference:http://www.vipan.com/htdocs/log4jhelp.html

 

package  log4j;

import  java.util.Enumeration;
import  java.util.Properties;

import  org.apache.log4j.Category;
import  org.apache.log4j.Priority;

public   class  TestLogging  {
    
// Initialize a logging category. Here, we get THE ROOT CATEGORY
    
// static Category cat = Category.getRoot();
    
// Or, get a custom category
    static Category cat = Category.getInstance(TestLogging.class.getName());

    
public void init() {
        Properties prop 
= System.getProperties();

        Enumeration enum1 
= prop.propertyNames();

        cat.info(
"***System Environment As Seen By Java***");
        cat.debug(
"***Format: PROPERTY = VALUE***");

        
while (enum1.hasMoreElements()) {
            String key 
= (String) enum1.nextElement();
            cat.info(key 
+ " = " + System.getProperty(key));
        }

    }


    
public static void main(String args[]) {
        
// Try a few logging methods

        
// Use debug to write debugging messages which should not be printed
        
// when the application is in production
        cat.debug("Start of main()");
        
        
// Use info for messages similar to the "verbose" mode of many
        
// applications.
        cat.info("Just testing a log message with priority set to INFO");
        
        
// Use warn for warning messages which are logged to some log but the
        
// application is able to carry on without a problem.
        cat.warn("Just testing a log message with priority set to WARN");
        
        
// Use error for application error messages which are also logged to
        
// some log but, still, the application can hobble along. Such as when
        
// some administrator-supplied configuration parameter is incorrect and
        
// you fall back to using some hard-coded default value.
        cat.error("Just testing a log message with priority set to ERROR");
        
        
// Use fatal for critical messages, after logging of which the
        
// application quits abnormally.
        cat.fatal("Just testing a log message with priority set to FATAL");

        
// Alternate but INCONVENIENT form
        cat.log(Priority.DEBUG, "Calling init()");
        
new TestLogging().init();
    }

}

 

log4j.properties is below:

log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout

Note:

log4j.properties must be under the src dir

src
    -log4j
           -TestLogging.java
    -log4j.properties

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值