org.springframework.boot
spring-boot-starter-web
${LOG_FILE}
${CONSOLE_LOG_PATTERN}
utf8
${LOG_FILE}.%d{yyyy-MM-dd}.%i.log
7
50MB
500MB
${WARN_LOG_FILE}
${CONSOLE_LOG_PATTERN}
utf8
${LOG_PATH}/warn.%d{yyyy-MM-dd}.%i.log
7
50MB
500MB
WARN
${ERROR_LOG_FILE}
${CONSOLE_LOG_PATTERN}
utf8
${LOG_PATH}/error.%d{yyyy-MM-dd}.%i.log
7
50MB
500MB
ERROR
${CONSOLE_LOG_PATTERN}
utf8
@Controller
@RequestMapping("/modeler")
public class ModelerController {
private final Logger log = LoggerFactory.getLogger(ModelerController.class);
@Autowired
RepositoryService repositoryService;
@ResponseBody
@RequestMapping("/my")
public String index(){
log.error("error");
log.warn("warn");
log.info("xinxxxxxxx");
log.debug("debugger");
log.trace("trace");
return "my.html";
}
}
本地日志:
1、添加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
2、log4j.properties文件配置
#日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL
log4j.rootLogger=info,error,info,warn,stdout
##############打印到平台的日志##################
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#输出的信息格式
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p:%X{traceID}]-(%C:%L) %m%n
##############WARN打印的文件的日志##################
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.tracking.Encoding=utf-8
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) %m%n
#输出文件位置
log4j.appender.warn.File=E:/log4j/warn/warn
log4j.appender.warn.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.warn.Threshold=ALL
##############ERROR打印的文件的日志##################
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) %m%n
log4j.appender.error.File=E:/log4j/error/error
log4j.appender.error.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.error.Threshold=ERROR
##############INFO打印的文件的日志##################cn.com.bluemoon.bd.service.its.deg.common.Log4jMyAppender
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p:%X{traceID}]-(%C:%L) %m%n
#这个相对路径是从当前配置文件的路径开始的
log4j.appender.info.File=E:/log4j/info/info
#DataPattern规定每小时产生一个日志文档,当前小时的文日志会放在名字为info的文档里,以前的日志放在lofo.yyyy-MM-dd-HH的文档里。
log4j.appender.info.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.info.ImmediateFlush=true
3、配置文件中设置日志
application.yml:
#log4j 日志输出
logging:
config:classpath: classpath:log4j.properties #log4j.properties配置文件(log4j后缀为*.properties)
# path 和 name 同时启用,起作用的时name
file:
path: E:/LOG4J #设置存储在本地日志路径
name: log4j #设置存储在本地日志名称(日志文件默认存储在项目根目录)
4、使用:
@Controller
@RequestMapping("/modeler")
public class ModelerController {
private final Logger log = LoggerFactory.getLogger(ModelerController.class);
@Autowired
RepositoryService repositoryService;
@ResponseBody
@RequestMapping("/my")
public String index(){
log.error("error");
log.warn("warn");
log.info("xinxxxxxxx");
log.debug("debugger");
log.trace("trace");
return "my.html";
}
}
1、添加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
2、log4j2.xml配置
%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level %l} -
%m%n
%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %C.%M -
%m%n
20MB
DEBUG
${basePath}/all.log
${basePath}/time-based-logs/%d{yyyy-MM-dd}-all.log.zip
50
${basePath}/info.log
${basePath}/time-based-logs/%d{yyyy-MM-dd}-info.log.zip
10
${basePath}/warn.log
${basePath}/time-based-logs/%d{yyyy-MM-dd}-warn.log.zip
10
${basePath}/error.log
${basePath}/time-based-logs/%d{yyyy-MM-dd}-error.log.zip
10
DEBUG
${basePath}/druid.log
${basePath}/time-based-logs/%d{yyyy-MM-dd}-druid.log.zip
10
3、配置文件中设置日志
application.yml
#log4j2 日志输出
logging:
config:classpath: classpath:log4j2.xml #log4j2配置文件(log4j2后缀为*.xml/*.json)
# path 和 name 同时启用,起作用的时name
file:
path: E:/LOG4J2 #设置存储在本地日志路径
name: log4j2 #设置存储在本地日志名称(日志文件默认存储在项目根目录)
4、使用:
@Controller
@RequestMapping("/modeler")
public class ModelerController {
private final Logger log = LoggerFactory.getLogger(ModelerController.class);
@Autowired
RepositoryService repositoryService;
@ResponseBody
@RequestMapping("/my")
public String index(){
log.error("error");
log.warn("warn");
log.info("xinxxxxxxx");
log.debug("debugger");
log.trace("trace");
return "my.html";
}
}
注意:如果pom.xml中添加有 spring-boot-starter-test 依赖
必须写成如下,否则爆提示包冲突错误