冷面阎罗

低调做人&&高调做事
随笔 - 208, 文章 - 3, 评论 - 593, 引用 - 0
数据加载中……

Log4j 简单使用

         程序开发环境中的日志记录是由嵌入在程序中以输出一些对开发人员有用信息的语句所组成。例如,跟踪语句( trace ),结构转储和常见的 System.out.println printf 调试语句。 log4j 提供分级方法在程序中嵌入日志记录语句。日志信息具有多种输出格式和多个输出级别。
使用一个专门的日志记录包,可以减轻对成千上万的 System.out.println 语句的维护成本,因为日志记录可以通过配置脚本在运行时得以控制。 log4j 维护嵌入在程序代码中的日志记录语句。通过规范日志记录的处理过程,一些人认为应该鼓励更多的使用日志记录并且获得更高程度的效率。
       在java程序中,使用的方法,我总结一下有两种:
       一是:
private static org.apache.log4j.Category log =
org.apache.log4j.Category.getInstance(classname);
//classname java文件名\r

log.debug(
"");
log.error(
"");
..
    一是:
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class TestLog4j 
{
    
//代码(1)
    static Logger logger = Logger.getLogger(TestLog4j.class.getName());
    
public TestLog4j(){}
    
public static void main(String[] args)
{
     
//代码(2)
        BasicConfigurator.configure();
         
//代码(3)
        logger.debug("Start of the main() in TestLog4j");
        logger.info(
"Just testing a log message with priority set to INFO");
        logger.warn(
"Just testing a log message with priority set to WARN");
        logger.error(
"Just testing a log message with priority set to ERROR");
        logger.fatal(
"Just testing a log message with priority set to FATAL");
        logger.log(Priority.DEBUG, 
"Testing a log message use a alternate form");
        logger.debug(
"End of the main() in TestLog4j");
    }

}

posted on 2006-12-27 19:39 冷面阎罗 阅读(492) 评论(1)  编辑  收藏 所属分类: java

评论

# re: Log4j 简单使用  回复  更多评论   

嗯,正想学下LOG4J,很实用的东东
2007-01-03 21:06 | 小贺

只有注册用户登录后才能发表评论。


网站导航: