zoukankan      html  css  js  c++  java
  • An Entry Example of Log4j

    The log4j can be configured both programmatically and externally using special configuration files. External configuration is most preferred, because to take effect it doesn’t require change in application code, recompilation, or redeployment. Configuration files can be XML files or Java property files that can be created and edited using any text editor or XML editor, respectively.

    1. Programmatically configure log4j

    This example is from this site, the code is messy, so why not make it clean.

    package Test;
     
    import org.apache.log4j.*;
     
    public class TestLog {
     
    	/*
    	 * get a static logger instance with name TestLog
    	 */
    	static Logger myLogger = Logger.getLogger(TestLog.class.getName());
    	Appender myAppender;
    	SimpleLayout myLayout;
     
    	/* Constructor */
    	public TestLog() {
     
    		/*
    		 * Set logger priority level programmatically. Though this is better done externally
    		 */
    		myLogger.setLevel(Level.ALL);
     
    		/*
    		 * Instantiate a layout and an appender, assign layout to appender
    		 * programmatically
    		 */
    		myLayout = new SimpleLayout();
    		myAppender = new ConsoleAppender(myLayout); // Appender is Interface
     
    		/* Assign appender to the logger programmatically */
    		myLogger.addAppender(myAppender);
     
    	} // end constructor
     
    	public void do_something(int a, float b) {
     
    		/*
    		 * This log request enabled and log statement logged, since INFO = INFO
    		 */
    		myLogger.info("The values of parameters passed to method  do_something are: "+ a + ", " + b);
     
    		/* this log request is not enabled, since DEBUG < INFO */ 		myLogger.debug("Operation performed successfully"); 		Object x=null; 		if (x == null) { 			/* 			 * this log request is enabled and log statement logged, since ERROR 			 * > INFO
    			 */
    			myLogger.error("Value of X is null");
     
    		}
    	} // end do_something()
    	public static void main(String []args){
    		new TestLog().do_something(1, 3);
    	}
    } // end class MyClass

    Here is the snapshot of output:
    log4j

    2. Configure Log4j externally

    Create a file named “log4j.properties” in the “src” directory of your project.

    # Define the root logger with appender file
    log = /home/ryan/Desktop/log4j
    log4j.rootLogger = DEBUG, FILE
    
    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=${log}/log.out
    
    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.conversionPattern=%m%n
    

    Here is the java code.

    package Test;
    import org.apache.log4j.Logger;
    public class TestLogger {
    	static Logger myLogger = Logger.getLogger(TestLog.class.getName());
     
    	public static void main(String[] args) {
    		myLogger.info("testing");
    		myLogger.warn("warning testing");
    		myLogger.error("this is an error");
    	}
     
    }

    The log will be printed on the log.out file in /home/ryan/Desktop/log4j directory.

  • 相关阅读:
    jq幻灯片2013-8-31
    jq设置样式
    KindEditor编辑器常用操作
    深入理解JavaScript模拟私有成员
    后台常见报错处理和注意问题(二)
    从项目上一个子查询扩展学习开来:mysql的查询、子查询及连接查询
    在MySQL中使用子查询和标量子查询的基本用法
    HQL语句中数据类型转换,及hibernate中createQuery执行hql报错
    SQL中的cast()函数
    模糊搜索:concat各种函数详解、like操作符、通配符
  • 原文地址:https://www.cnblogs.com/daichangya/p/12958996.html
Copyright © 2011-2022 走看看