03、模拟日志的产生

首先,必须得有一份日志文件,日志文件可以是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观察日志的产生

13274599-050748ebeb2e3589.png

001、日志的产生.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值