首页 > 编程语言 > 详细

java log4j 打日志到控制台同时打印到不同文件

时间:2019-08-27 14:42:30      阅读:120      评论:0      收藏:0      [点我收藏+]

1.pom配置

<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>


</project>

2.  log4j.properties

logs 文件夹需要手动创建

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Set everything to be logged to the console
log4j.rootCategory=DEBUG, console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n


log4j.appender.file = org.apache.log4j.FileAppender
log4j.appender.file.File = ..\\test.txt
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n

log4j.logger.batch1=INFO,file1
log4j.appender.file1 = org.apache.log4j.FileAppender
log4j.appender.file1.File = \\logs\\test1.txt
log4j.appender.file1.layout = org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n

log4j.logger.batch2=INFO,file2
log4j.appender.file2 = org.apache.log4j.FileAppender
log4j.appender.file2.File = test2.txt
log4j.appender.file2.layout = org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n

3.  测试代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Test {
    private static final Logger logger = LoggerFactory.getLogger(Test.class); //默认log4j对象
    private static final Logger logger1=LoggerFactory.getLogger("batch1");//batch1 log4j对象
    private static final Logger logger2=LoggerFactory.getLogger("batch2"); //batch2 log4j对象
    public static void main(String [] args){
        logger.info("cs");//只写到test.txt
        logger1.info("cs1");//写到test.txt和test1.txt
        logger2.info("cs2");//写到test.txt和test2.txt

    }
}

4.效果:所有日志均会打印到控制台和test.txt。logger1对象打印的日志还会记录到test1.txt,logger2对象打印的日志还会记录到test2.txt

java log4j 打日志到控制台同时打印到不同文件

原文:https://www.cnblogs.com/lfxiao/p/11418157.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!