【IntelliJ IDEA 2019.2】java log4j 最简用法及进阶用法
java log4j 最简用法
1.官网下载log4j jar 包。
2.src同级创建并设置log4j.properties
#设置级别
log4j.rootLogger=debug,appender1
#输出到文件(这里默认为追加方式)
log4j.appender.appender1=org.apache.log4j.FileAppender
#设置文件输出路径
log4j.appender.appender1.File=myproject.log
#设置文件输出样式
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
测试代码:
package com.myproject.test;
import org.apache.log4j.Logger;
public class MyTestClass {
private static Logger logger = Logger.getLogger(MyTestClass.class);
public static void main(String[] args){
System.out.println("Hello world");
logger.info("----交易日志,日志级别info----->"+"hello world"+"<--------");
}
}
运行后,就会在目录下生成一个后缀为 *.log 的日志文件。
进阶用法:java log4j 打日志到控制台同时打印到不同文件
参考自:https://www.cnblogs.com/lfxiao/p/11418157.html
log4j.properrties
#
# 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 = 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
测试代码:
import org.apache.log4j.Logger;
public class Test327Class extends AT_Cmd_Interface{
private static final Logger logger = Logger.getLogger(Test327Class.class); //默认log4j对象
private static final Logger logger1=Logger.getLogger("batch1");//batch1 log4j对象
private static final Logger logger2=Logger.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
}
}
效果:所有日志均会打印到控制台和test.txt。logger1对象打印的日志还会记录到test1.txt,logger2对象打印的日志还会记录到test2.txt
运行结果,生成3个文件,如下: