1.首先引入jar包依赖
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2.添加文件
=.
- 1
3.添加文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorinterval="10">
<!--全异步需要配置一个环境变量,可在properties文件中配置,也可启动时使用JVM参数配置,也可设置系统环境变量-->
<!--=.-->
<Properties>
<!--这里设置全局日志生成的地址-->
<Property name="">C:/logs/logOut</Property>
<!--这里设置日志输出的前缀格式-->
<Property name="">%d %-5p [%t] %C{2} %M (%F:%L) - %m%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/>
</Console>
<File name="TEMP" fileName="temp">
<PatternLayout pattern="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/>
</File>
<Async name="ASYNC">
<AppenderRef ref="TEMP"/>
<AppenderRef ref="CONSOLE"/>
</Async>
<!--这里设置日志输出的具体位置,文件大小,最大生成个数等 -->
<!--<RollingRandomAccessFile bufferSize="1024" name="LogOutINFO" fileName="${}/" filePattern="${}/log_INFO_storage/output-%d{yyyy-MM-dd HH-mm}.%">-->
<RollingRandomAccessFile bufferSize="1024" name="LogOutINFO" fileName="${}/" filePattern="${}/log_INFO_storage/output-%d{yyyy-MM-dd}.%">
<PatternLayout>
<Pattern>${}</Pattern>
</PatternLayout>
<Policies>
<!--按时间分割日志 modulate为true代表是否偏移时间,如当日志的分割的名称最小单位为yyyy-MM-dd时,则到当日00:00才进行分割-->
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
<!--按大小进行划分,当达到20Mb之后会分割成gz压缩包 到日志分割的指定地址-->
<SizeBasedTriggeringPolicy size="20MB"/>
<!--<SizeBasedTriggeringPolicy size="20MB"/>-->
</Policies>
<!--设置最大分割个数-->
<DefaultRolloverStrategy max="365"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile bufferSize="1024" name="LogOutERROR" fileName="${}/" filePattern="${}/log_ERROR_storage/output-%d{yyyy-MM-dd}.%">
<PatternLayout>
<Pattern>${}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="365"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile bufferSize="1024" name="LogOutWARN" fileName="${}/" filePattern="${}/log_WARN_storage/output-%d{yyyy-MM-dd}.%">
<PatternLayout>
<Pattern>${}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="365"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<!--设置日志输出的等级-->
<Root level="INFO" includeLocation="true">
<!--设置各个等级日志输出的 指向参数-->
<AppenderRef level="INFO" ref="LogOutINFO"/>
<AppenderRef level="WARN" ref="LogOutWARN"/>
<AppenderRef level="ERROR" ref="LogOutERROR"/>
</Root>
</Loggers>
</Configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
4.编写启动类,,指定log4j2的配置文件路径
public class CertWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
("getRootConfigClasses=============================");
return new Class<?>[]{ };
}
@Override
protected Class<?>[] getServletConfigClasses() {
("getServletConfigClasses============================");
return new Class<?>[]{ };
}
@Override
protected String[] getServletMappings() {
("getServletMappings=================================");
return new String[] {"/"};
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
(servletContext);
("onStartup========================================");
("log4jConfigLocation","classpath:/");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
5.在两个配置类中,其中一个添加注解,扫描properties文件进入容器当中
@PropertySource("classpath:/")
- 1
到此就可以在项目里配置使用日志输出啦
可以使用lombok依赖的@Log4j2,直接加在需要使用log输出的类头上,但要注意编译器需要安装lombok插件
<dependency>
<groupId></groupId>
<artifactId>lombok</artifactId>
</dependency>
- 1
- 2
- 3
- 4
或者使用
private static final Logger LOGGER = (类名.class);
- 1