1、首先看pom.xml文件,需要以下配置
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.7</slf4j.version> <kudu-client.version>1.7.0</kudu-client.version> <httpclient.version>4.5.1</httpclient.version> <org.json.version>20180813</org.json.version> </properties>
2、在src/main/resources文件夹下建立log4j.properties文件,内容如下:
log4j.rootLogger=INFO, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%c] (%t:%x) %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/open_monitor.log
#log4j.appender.file.File=E://logs/open_monitor.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d|%p|%c|%t\:%x|%m%n
log4j.logger.org.apache=WARN
log4j.logger.org.springframework=ERROR
log4j.logger.org.mybatis.spring=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.net.sf.ehcache=ERROR
log4j.logger.net.sf.jxls=ERROR
log4j.logger.cn.ctrchina=DEBUG
log4j.logger.java.sql.Connection=ERROR
log4j.logger.java.sql.Statement=ERROR
log4j.logger.java.sql.PreparedStatement=ERROR
log4j.logger.java.sql.ResultSet=ERROR
3、代码
public class CommonMethod{ private static Logger logger = LoggerFactory.getLogger(CommonMethod.class); public static <billDetail> List<String> getWebRestInfo(String monitorDate){ logger.info("--------------------------------------"); logger.info("开始迁移"+monitorDate+"号数据..."); 。。。 } }
4、日志位置
由于在上面进行了设置log4j.appender.file.File=${catalina.home}/logs/open_monitor.log
由于运行程序在d盘的某个位置,因此,需要在d盘根目录有一个log文件夹,里面生成open_monitor.log文件。
内容如下:
5、把程序打成jar包,执行
比如把jar包放到e盘,并用批处理文件执行
run.bat内容如下:
@echo off
start cmd /k java -jar ott-monitor.jar cn.ctrchina.dataMigrate
这样执行后,日志就生成在(这个位置和jar包在同一个盘,并且在在e的盘根目录logs文件夹)