log4j2跟之前版本的区别就不发了,那玩意上网上搜有的是介绍的
这个文章主要是为了那些初学者上网上不好配搞得,直接扒就能用,网上其他文章不是给你讲概念,就是给你配置没发全发一半,有严谨性但是不通俗易懂。
先看下效果图
我这里有两个log,一个是info级别,一个是error级别
在我的磁盘里会生成两种级别的日志文件,这个文件里就是单独一种级别的,方便以后为了看有哪些error,还得去log里搜。
maven引入
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-log4j2artifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-loggingartifactId>
- <exclusions>
- <exclusion>
- <groupId>*groupId>
- <artifactId>*artifactId>
- exclusion>
- exclusions>
- dependency>
在resource下建立文件 ,直接粘贴
- <configuration monitorInterval="5">
-
- <Properties>
- <Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L -%m%nProperty>
- <Property name="displayName">CeipageServerProperty>
-
- <property name="FILE_PATH">E://Ceinetproperty>
-
- <property name="FILE_NAME">CeipageServerproperty>
- Properties>
- <Appenders>
- <Console name="console" target="SYSTEM_OUT" follow="true">
-
- <PatternLayout pattern="${pattern}"/>
-
- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
- Console>
-
- <File name="error" fileName="${FILE_NAME}_ERROR.log" append="true">
-
- <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
- <PatternLayout pattern="${pattern}"/>
- File>
-
-
- <RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/${FILE_NAME}_INFO.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz">
-
- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
- <PatternLayout pattern="${pattern}"/>
- <Policies>
-
- <TimeBasedTriggeringPolicy interval="1"/>
-
- <SizeBasedTriggeringPolicy size="10MB"/>
- Policies>
-
- <DefaultRolloverStrategy max="30"/>
- RollingFile>
-
- <RollingFile name="RollingFileError" fileName="${FILE_PATH}/${FILE_NAME}_ERROR.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz">
-
- <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
- <PatternLayout pattern="${pattern}"/>
- <Policies>
-
- <TimeBasedTriggeringPolicy interval="1"/>
-
- <SizeBasedTriggeringPolicy size="10MB"/>
- Policies>
-
- <DefaultRolloverStrategy max="30"/>
- RollingFile>
- Appenders>
- <Loggers>
- <Root level="INFO">
- <AppenderRef ref="console">AppenderRef>
- <AppenderRef ref="error">AppenderRef>
- <AppenderRef ref="RollingFileInfo">AppenderRef>
- <AppenderRef ref="RollingFileError">AppenderRef>
- Root>
- Loggers>
- configuration>
这里面的配置我拷了好几个文章然后改吧改吧整合的
这里注意几个地方,根据自己情况修改
这个是我们自己写的log工具类,发现2代里调用log改成LogManager了。 这里面我们做了调用类的处理,可以不用像以前似的每个类里都得getLogger(XXXX.class)了
这个不一定非得用我们的,自己有封装的更好
- package com.cei.utils;
-
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import sun.reflect.Reflection;
-
- import java.util.Date;
-
- public class LoggerUtil {
- private static boolean isLog = true;
- private static Logger logger = null;
-
- public static void setLogger(boolean isLog) {
- LoggerUtil.isLog = isLog;
- }
-
- public static void setLog(Logger logger) {
- LoggerUtil.logger = logger;
- }
-
- public static void setLogger(Logger logger) {
- LoggerUtil.logger = logger;
- }
-
- public static void debug(Object msg) {
- if (isLog) {
- if (logger == null) {
- StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
- if (stackTraceElement.length > 3) {
- logger = LogManager.getLogger(stackTraceElement[2].getClassName());
- logger.debug("调用者类名" + stackTraceElement[2].getClassName());
- } else {
- logger = LogManager.getLogger(Reflection.getCallerClass(2).getName());
- logger.debug("调用者类名" + Reflection.getCallerClass(2).getName());
- }
- }
- logger.debug(new Date() + " " + msg);
- }
-
- }
-
- public static void info(Object msg) {
- if (isLog) {
- if (logger == null) {
- StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
- if (stackTraceElement.length > 3) {
- logger = LogManager.getLogger(stackTraceElement[2].getClassName());
- logger.debug("调用者类名" + stackTraceElement[2].getClassName());
- } else {
- logger = LogManager.getLogger(Reflection.getCallerClass(2).getName());
- logger.debug("调用者类名" + Reflection.getCallerClass(2).getName());
- }
- }
- logger.info(new Date() + " " + msg);
- }
- }
-
- public static void warn(Object msg) {
- if (isLog) {
- if (logger == null) {
- StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
- if (stackTraceElement.length > 3) {
- logger = LogManager.getLogger(stackTraceElement[2].getClassName());
- logger.debug("调用者类名" + stackTraceElement[2].getClassName());
- } else {
- logger = LogManager.getLogger(Reflection.getCallerClass(2).getName());
- logger.debug("调用者类名" + Reflection.getCallerClass(2).getName());
- }
- }
- logger.warn(new Date() + " " + msg);
- }
- }
-
- public static void error(Object msg) {
- if (isLog) {
- if (logger == null) {
- StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
- if (stackTraceElement.length > 3) {
- logger = LogManager.getLogger(stackTraceElement[2].getClassName());
- logger.debug("调用者类名" + stackTraceElement[2].getClassName());
- } else {
- logger = LogManager.getLogger(Reflection.getCallerClass(2).getName());
- logger.debug("调用者类名" + Reflection.getCallerClass(2).getName());
- }
- }
- logger.error(new Date() + " " + msg);
- }
- }
-
- public static void fatal(Object msg) {
- if (isLog) {
- if (logger == null) {
- StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
- if (stackTraceElement.length > 3) {
- logger = LogManager.getLogger(stackTraceElement[2].getClassName());
- logger.debug("调用者类名" + stackTraceElement[2].getClassName());
- } else {
- logger = LogManager.getLogger(Reflection.getCallerClass(2).getName());
- logger.debug("调用者类名" + Reflection.getCallerClass(2).getName());
- }
- }
- logger.fatal(new Date() + " " + msg);
- }
- }
-
- }
还有就是我们是类似抓异常那种线程抓类名,而不是用的这个
这个有过时的注解了,不是不能用,但是怕有问题就没用
调用就简单了,上面都配好后 ,随便调