首先,必须得有一份日志文件,日志文件可以是web项目,每天产生的日志,但是,这里使用java程序模拟产生日志,为了测试,每分钟产生日志
pom文件
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<classpathScope>compile</classpathScope>
<mainClass>com.zj.spark.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
log4j.properties
### set log levels ###
log4j.rootLogger = INFO , C
### console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
# 输出liveness日志
log4j.logger.liveness = INFO, liveness
# 是否在父log中输出日志
log4j.additivity.liveness = false
# 每天生产日志文件
log4j.appender.liveness = org.apache.log4j.DailyRollingFileAppender
# 生成日志到单独的文件
log4j.appender.liveness.File =/opt/logs/liveness.log
log4j.appender.liveness.DatePattern = '_'yyyy-MM-dd-HH-mm'.log'
# 追加日志
log4j.appender.liveness.Append = true
# 日志级别
log4j.appender.liveness.Threshold = INFO
log4j.appender.liveness.layout = org.apache.log4j.PatternLayout
# 日志格式
log4j.appender.liveness.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
代码编写
public class Hello {
private static final Logger log2 = LoggerFactory.getLogger("liveness");
public static void main(String[] args) throws InterruptedException {
while(true) {
log2.info("test liveness");
Thread.sleep(1000);
}
}
}
打包 上传到虚拟机,并启动程序
mvn clean package
java -cp printlog.jar com.zj.mysparkproject.Hello start
到/opt/logs观察日志的产生
001、日志的产生.png