简明log4j配置教程

时间:2021-06-22 21:55:55

先准备好log4j需要对应的开发包:

apache-log4j-extras-1.2.17.jar

slf4j
-api-1.6.1.jar

slf4j
-log4j12-1.6.1.jar

然后就是在项目中准备一个log4j.xml开发文件:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- 设置按天滚动方式生成日志文件 -->
<!-- 将日志写到文件中 -->
<appender name="biFormat" class="org.apache.log4j.rolling.RollingFileAppender">
<!-- 设置日志编码 -->
<param name="Encoding" value="UTF-8" />
<!-- 设置按天滚动方式生成日志文件 -->
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/opt/test/log/bi_%d{yyyyMMdd}.log" />
</rollingPolicy>
<!-- 设置日志内容格式 -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %p (%F.%L): %m%n" />
</layout>
</appender>

<!-- 设置按天滚动方式生成日志文件 -->
<!-- 将日志写到Linux系统日志中 -->
<appender name="syslogger" class="org.apache.log4j.net.SyslogAppender">
<!-- 设置阈值为ERROR级别,低于ERROR级别的日志将不会写入到系统中 -->
<param name="Threshold" value="ERROR" />
<param name="SyslogHost" value="192.168.172.140" />
<param name="Facility" value="USER" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p (%F.%L): %m%n" />
</layout>
</appender>

<!-- 设置logger日志名称,在程序中引用 -->
<logger name="logfile">
<!-- 设置日志级别为DEBUG级别 -->
<level value="debug" />
<appender-ref ref="biFormat" />
<appender-ref ref="syslogger" />
</logger>
</log4j:configuration>

 

简明log4j配置教程

 

关于上面日志格式常用占位符解释:

%d: 当前时间,如果要指定格式,则可以yyyy MM dd HH mm ss SSS 代表 年、月、日、时、分、秒、毫秒

%F: 日志所在代码所在文件

%L: 日志所在的行数

%m: 消息主体

%t: 程序所在线程

%n: 换行

%p: 当前日志级别

 

示例程序代码:

package org.lyk.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(
"/test/*")
public class Test
{
private Logger logger = LoggerFactory.getLogger("logfile");
@RequestMapping(
"testDemo")
public ModelAndView testDemo(String msg)
{
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
logger.info(
"This is for log file " + sdf.format(new Date()));
logger.error(
"This is for sys log file " + sdf.format(new Date()));
return new ModelAndView("/index.jsp");
}
}

 

输出效果

日志文件:

简明log4j配置教程

Linux syslog:

简明log4j配置教程

程序代码:

package org.lyk.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(
"/test/*")
public class Test
{
private Logger logger = LoggerFactory.getLogger("logfile");
@RequestMapping(
"testDemo")
public ModelAndView testDemo(String msg)
{
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
logger.info(
"This is for log file " + sdf.format(new Date()));
logger.error(
"This is for sys log file " + sdf.format(new Date()));
return new ModelAndView("/index.jsp");
}
}

 

https://pan.baidu.com/s/1kVghs83

欢迎加入QQ群参与讨论. 422683877

简明log4j配置教程