参考页面

​https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-logging​

springProperty

// source来源于配置文件 比如applicaton.yml
<springProperty scope="context" name="LOG_FILE_NAME" source="logging.file.name"/>


// defaultValue
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>

<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
...
</appender>

使用

比如文件路径: ${FILE_DIR}/${LOG_FILE_NAME}_%d{yyyy-MM-dd}_%i.log


springProfile

spring:
application:
name: springboot-logback
profiles:
active: test
<springProfile name="default">
<property name="FILE_DIR" value="c:/logs/springboot-logback-default/" />
</springProfile>

<springProfile name="test">
<property name="FILE_DIR" value="c:/logs/springboot-logback-test/" />
</springProfile>

<springProfile name="dev">
<property name="FILE_DIR" value="c:/logs/springboot-logback-dev/" />
</springProfile>
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

Log Groups

logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat

logging.level.tomcat=TRACE
# 在application文件中配置

logging:
file:
name: springboot-log-group
path: c:/logs/springboot-log-group/
group:
task: com.kq.log.task
sql1: com.kq.log.dao,com.kq.log.sql
logic: com.kq.log.controller,com.kq.log.service
level:
task: debug
sql1: debug
logic: debug

Color-coded Output

# 右对齐
%clr(%5p) %5p

# 左对齐
%clr(%-5level) %-5level

Level

Color


​FATAL​



Red



​ERROR​



Red



​WARN​



Yellow



​INFO​



Green



​DEBUG​



Green



​TRACE​



Green



%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}

The following colors and styles are supported:


  • ​blue​
  • ​cyan​
  • ​faint​
  • ​green​
  • ​magenta​
  • ​red​
  • ​yellow​

属性

Spring Environment

System Property

Comments


​logging.exception-conversion-word​



​LOG_EXCEPTION_CONVERSION_WORD​



The conversion word used when logging exceptions.



​logging.file.clean-history-on-start​



​LOG_FILE_CLEAN_HISTORY_ON_START​



Whether to clean the archive log files on startup (if LOG_FILE enabled). (Only supported with the default Logback setup.)



​logging.file.name​



​LOG_FILE​



If defined, it is used in the default log configuration.



​logging.file.max-size​



​LOG_FILE_MAX_SIZE​



Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)



​logging.file.max-history​



​LOG_FILE_MAX_HISTORY​



Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)



​logging.file.path​



​LOG_PATH​



If defined, it is used in the default log configuration.



​logging.file.total-size-cap​



​LOG_FILE_TOTAL_SIZE_CAP​



Total size of log backups to be kept (if LOG_FILE enabled). (Only supported with the default Logback setup.)



​logging.pattern.console​



​CONSOLE_LOG_PATTERN​



The log pattern to use on the console (stdout). (Only supported with the default Logback setup.)



​logging.pattern.dateformat​



​LOG_DATEFORMAT_PATTERN​



Appender pattern for log date format. (Only supported with the default Logback setup.)



​logging.pattern.file​



​FILE_LOG_PATTERN​



The log pattern to use in a file (if ​​LOG_FILE​​ is enabled). (Only supported with the default Logback setup.)



​logging.pattern.level​



​LOG_LEVEL_PATTERN​



The format to use when rendering the log level (default ​​%5p​​). (Only supported with the default Logback setup.)



​logging.pattern.rolling-file-name​



​ROLLING_FILE_NAME_PATTERN​



Pattern for rolled-over log file names (default ​​${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz​​). (Only supported with the default Logback setup.)



​PID​



​PID​



The current process ID (discovered if possible and when not already defined as an OS environment variable).