LOG4J日志使用记录

    log4j在程序调试过程中非常方便,配置简单,但长时间不用又容易忘记,为了忘却的记忆,写上这个示例。过程如下:

1 新建一个MAVEN简单项目,项目路径:(D:\java_project\sts\testjava\)POM.XML内容配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cuigh.test</groupId>
  <artifactId>testjava</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <JAVA.VERSION>1.8</JAVA.VERSION>
  </properties>
 
  <dependencies>
    <dependency>
  			<groupId>log4j</groupId>
  			<artifactId>log4j</artifactId>
  			<version>1.2.16</version>
  		</dependency>
  </dependencies>
  
  <build>
    <plugins>
      	<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>${JAVA.VERSION}</source>
				<target>${JAVA.VERSION}</target>
			</configuration>
		</plugin>
      	<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                
                  <mainClass>cuigh.test.testjava.MainLogTest</mainClass>
                  
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      	</plugin>
    </plugins>
  </build>

</project>
2 在项目下建立一个资源文件夹 D:\java_project\sts\testjava\src\main\resources ,放置日志配置文件 log4j.properties ,内容如下:
log4j.rootLogger=INFO, CONSOLE, FILE

# out to console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c %x - %m%n

# out to file
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=d:\\java_project\\log\\stock.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=%d [%t] [%F  %L] %-5p - %m%n

3 新建一个JAVA测试类 MainLogTest.java ,内容如下:
package cuigh.test.testjava;

import org.apache.log4j.Logger;

/**
 * 功能:测试日志与MAVEN打包主程序的配置
 *
 */
public class MainLogTest {

	private static Logger log = Logger.getLogger( MainLogTest.class );
	
	public static void main(String[] args){
                System.out.println("MainLogTest run ...");
		log.info("MainLogTest run...");
		
	}

}
4 编译 mvn package

5 运行程序

    cd target

    D:\java_project\sts\testjava\target>java -jar testjava-0.0.1-SNAPSHOT.jar

运行结果如下:

MainLogTest run ...
2017-12-22 17:45:48 INFO  [main] cuigh.test.testjava.MainLogTest  - MainLogTest run...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值