org.springframework.boot:spring-boot:1.3.0.M1

spring-boot-1.3.0.M1.jar

  1. package org.springframework.boot.logging;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6.  
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.springframework.boot.ApplicationPid;
  10. import org.springframework.boot.SpringApplication;
  11. import org.springframework.boot.bind.RelaxedPropertyResolver;
  12. import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
  13. import org.springframework.boot.context.event.ApplicationStartedEvent;
  14. import org.springframework.context.ApplicationContext;
  15. import org.springframework.context.ApplicationEvent;
  16. import org.springframework.context.ApplicationListener;
  17. import org.springframework.context.event.ContextClosedEvent;
  18. import org.springframework.context.event.GenericApplicationListener;
  19. import org.springframework.core.Ordered;
  20. import org.springframework.core.ResolvableType;
  21. import org.springframework.core.env.ConfigurableEnvironment;
  22. import org.springframework.core.env.Environment;
  23. import org.springframework.util.LinkedMultiValueMap;
  24. import org.springframework.util.MultiValueMap;
  25. import org.springframework.util.ResourceUtils;
  26. import org.springframework.util.StringUtils;
  27.  
  28. /**
  29. * An {@link ApplicationListener} that configures the {@link LoggingSystem}. If the
  30. * environment contains a {@code logging.config} property a then that will be used to
  31. * initialize the logging system, otherwise a default configuration is used.
  32. * <p>
  33. * By default, log output is only written to the console. If a log file is required the
  34. * {@code logging.path} and {@code logging.file} properties can be used.
  35. * <p>
  36. * Some system properties may be set as side effects, and these can be useful if the
  37. * logging configuration supports placeholders (i.e. log4j or logback):
  38. * <ul>
  39. * <li>{@code LOG_FILE} is set to the value of path of the log file that should be written
  40. * (if any).</li>
  41. * <li>{@code PID} is set to the value of the current process ID if it can be determined.</li>
  42. * </ul>
  43. *
  44. * @author Dave Syer
  45. * @author Phillip Webb
  46. * @author Andy Wilkinson
  47. * @see LoggingSystem#get(ClassLoader)
  48. */
  49. public class LoggingApplicationListener implements GenericApplicationListener {
  50.  
  51. /**
  52. * The name of the Spring property that contains a reference to the logging
  53. * configuration to load.
  54. */
  55. public static final String CONFIG_PROPERTY = "logging.config";
  56.  
  57. /**
  58. * The name of the Spring property that contains the path where the logging
  59. * configuration can be found.
  60. */
  61. public static final String PATH_PROPERTY = LogFile.PATH_PROPERTY;
  62.  
  63. /**
  64. * The name of the Spring property that contains the name of the logging configuration
  65. * file.
  66. */
  67. public static final String FILE_PROPERTY = LogFile.FILE_PROPERTY;
  68.  
  69. /**
  70. * The name of the System property that contains the process ID.
  71. */
  72. public static final String PID_KEY = "PID";
  73.  
  74. private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
  75. static {
  76. LOG_LEVEL_LOGGERS = new LinkedMultiValueMap<LogLevel, String>();
  77. LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.springframework.boot");
  78. LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.springframework");
  79. LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.tomcat");
  80. LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.catalina");
  81. LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.eclipse.jetty");
  82. LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl");
  83. LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.hibernate.SQL");
  84. }
  85.  
  86. private static Class<?>[] EVENT_TYPES = { ApplicationStartedEvent.class,
  87. ApplicationEnvironmentPreparedEvent.class, ContextClosedEvent.class };
  88.  
  89. private static Class<?>[] SOURCE_TYPES = { SpringApplication.class,
  90. ApplicationContext.class };
  91.  
  92. private final Log logger = LogFactory.getLog(getClass());
  93.  
  94. private LoggingSystem loggingSystem;
  95.  
  96. private int order = Ordered.HIGHEST_PRECEDENCE + 11;
  97.  
  98. private boolean parseArgs = true;
  99.  
  100. private LogLevel springBootLogging = null;
  101.  
  102. @Override
  103. public boolean supportsEventType(ResolvableType resolvableType) {
  104. return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES);
  105. }
  106.  
  107. @Override
  108. public boolean supportsSourceType(Class<?> sourceType) {
  109. return isAssignableFrom(sourceType, SOURCE_TYPES);
  110. }
  111.  
  112. private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) {
  113. if (type != null) {
  114. for (Class<?> supportedType : supportedTypes) {
  115. if (supportedType.isAssignableFrom(type)) {
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122.  
  123. @Override
  124. public void onApplicationEvent(ApplicationEvent event) {
  125. if (event instanceof ApplicationStartedEvent) {
  126. onApplicationStartedEvent((ApplicationStartedEvent) event);
  127. }
  128. else if (event instanceof ApplicationEnvironmentPreparedEvent) {
  129. onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
  130. }
  131. else if (event instanceof ContextClosedEvent) {
  132. onContextClosedEvent();
  133. }
  134. }
  135.  
  136. private void onApplicationStartedEvent(ApplicationStartedEvent event) {
  137. this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
  138. .getClassLoader());
  139. this.loggingSystem.beforeInitialize();
  140. }
  141.  
  142. private void onApplicationEnvironmentPreparedEvent(
  143. ApplicationEnvironmentPreparedEvent event) {
  144. if (this.loggingSystem == null) {
  145. this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
  146. .getClassLoader());
  147. }
  148. initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
  149. }
  150.  
  151. private void onContextClosedEvent() {
  152. if (this.loggingSystem != null) {
  153. this.loggingSystem.cleanUp();
  154. }
  155. }
  156.  
  157. /**
  158. * Initialize the logging system according to preferences expressed through the
  159. * {@link Environment} and the classpath.
  160. * @param environment the environment
  161. * @param classLoader the classloader
  162. */
  163. protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
  164. if (System.getProperty(PID_KEY) == null) {
  165. System.setProperty(PID_KEY, new ApplicationPid().toString());
  166. }
  167. initializeEarlyLoggingLevel(environment);
  168. initializeSystem(environment, this.loggingSystem);
  169. initializeFinalLoggingLevels(environment, this.loggingSystem);
  170. }
  171.  
  172. private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
  173. if (this.parseArgs && this.springBootLogging == null) {
  174. if (environment.containsProperty("debug")) {
  175. this.springBootLogging = LogLevel.DEBUG;
  176. }
  177. if (environment.containsProperty("trace")) {
  178. this.springBootLogging = LogLevel.TRACE;
  179. }
  180. }
  181. }
  182.  
  183. private void initializeSystem(ConfigurableEnvironment environment,
  184. LoggingSystem system) {
  185. LogFile logFile = LogFile.get(environment);
  186. String logConfig = environment.getProperty(CONFIG_PROPERTY);
  187. if (StringUtils.hasLength(logConfig)) {
  188. try {
  189. ResourceUtils.getURL(logConfig).openStream().close();
  190. system.initialize(logConfig, logFile);
  191. }
  192. catch (Exception ex) {
  193. this.logger.warn("Logging environment value '" + logConfig
  194. + "' cannot be opened and will be ignored "
  195. + "(using default location instead)");
  196. system.initialize(null, logFile);
  197. }
  198. }
  199. else {
  200. system.initialize(null, logFile);
  201. }
  202. }
  203.  
  204. private void initializeFinalLoggingLevels(ConfigurableEnvironment environment,
  205. LoggingSystem system) {
  206. if (this.springBootLogging != null) {
  207. initializeLogLevel(system, this.springBootLogging);
  208. }
  209. setLogLevels(system, environment);
  210. }
  211.  
  212. protected void initializeLogLevel(LoggingSystem system, LogLevel level) {
  213. List<String> loggers = LOG_LEVEL_LOGGERS.get(level);
  214. if (loggers != null) {
  215. for (String logger : loggers) {
  216. system.setLogLevel(logger, level);
  217. }
  218. }
  219. }
  220.  
  221. protected void setLogLevels(LoggingSystem system, Environment environment) {
  222. Map<String, Object> levels = new RelaxedPropertyResolver(environment)
  223. .getSubProperties("logging.level.");
  224. for (Entry<String, Object> entry : levels.entrySet()) {
  225. setLogLevel(system, environment, entry.getKey(), entry.getValue().toString());
  226. }
  227. }
  228.  
  229. private void setLogLevel(LoggingSystem system, Environment environment, String name,
  230. String level) {
  231. try {
  232. if (name.equalsIgnoreCase("root")) {
  233. name = null;
  234. }
  235. level = environment.resolvePlaceholders(level);
  236. system.setLogLevel(name, LogLevel.valueOf(level));
  237. }
  238. catch (RuntimeException ex) {
  239. this.logger.error("Cannot set level: " + level + " for '" + name + "'");
  240. }
  241. }
  242.  
  243. public void setOrder(int order) {
  244. this.order = order;
  245. }
  246.  
  247. @Override
  248. public int getOrder() {
  249. return this.order;
  250. }
  251.  
  252. /**
  253. * Sets a custom logging level to be used for Spring Boot and related libraries.
  254. * @param springBootLogging the logging level
  255. */
  256. public void setSpringBootLogging(LogLevel springBootLogging) {
  257. this.springBootLogging = springBootLogging;
  258. }
  259.  
  260. /**
  261. * Sets if initialization arguments should be parsed for {@literal --debug} and
  262. * {@literal --trace} options. Defaults to {@code true}.
  263. * @param parseArgs if arguments should be parsed
  264. */
  265. public void setParseArgs(boolean parseArgs) {
  266. this.parseArgs = parseArgs;
  267. }
  268.  
  269. }

LoggingApplicationListener的更多相关文章

  1. 使用canal分析binlog(一) 入门

    canal介绍 canal是应阿里巴巴存在杭州和美国的双机房部署,存在跨机房同步的业务需求而提出的.早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数据库同步 ...

  2. spring boot源码分析之SpringApplication

    spring boot提供了sample程序,学习spring boot之前先跑一个最简单的示例: /* * Copyright 2012-2016 the original author or au ...

  3. Spring Boot启动流程详解(一)

    环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...

  4. springboot源码解析 - 构建SpringApplication

    1 package com.microservice.framework; 2 3 import org.springframework.boot.SpringApplication; 4 impor ...

  5. Logback相关知识汇总

    例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4格式修饰符,与转换符共同使用:可选的格式修饰符位于“%”和转换符之间.第一个可选修饰符是左对齐 标志 ...

  6. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  7. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  8. 深入理解SpringCloud之引导程序应用上下文

    tips:我希望通过这篇文章来给对于bootstrap还不理解的朋友带来帮助.当然这篇文章不仅仅是讲解知识,我更希望给广大朋友带来学习与理解官方文档的一种思路.阅读本文前,建议大家对SpringBoo ...

  9. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

随机推荐

  1. Java 学习 第三篇;面向对象

    1:Java的常用包: 核心类在java 包中:扩展类在javax包中 java.lang 系统默认自动导入 包含String Math System Thread等类 java.util 包含了工具 ...

  2. HTML常用字符

    显示结果 描述 实体名称 实体编号   空格     < 小于号 < < > 大于号 > > & 和号 & & " 引号 &quo ...

  3. \s+(?<request_time>\d+(?:\.\d+)?)\s+ 解释

    <pre name="code" class="html"><pre name="code" class="ht ...

  4. bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    Description The cows are building a roller coaster! They want your help to design as fun a roller co ...

  5. LeeCode-Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...

  6. [转]Traceroute网络排障实用指南(2)

    五.优先级与限速 5.1 Traceroute延时判断影响因素 Traceroute延时包括三点: 探测包到达一个特定路由器的时间 路由器生成IPMI TTL Exceed的时间 ICMP TTL E ...

  7. 武道释义 &#183; 零散

    若是你防御你的左边.你的右边必定是弱点: 若是你防御你的前面,你的后面必定是弱点. 若是你处处小心防御,则必定处处都是弱点." 有些武术尽管先声夺人.但却如喝渗水之酒,令人越瞧越觉无味: 但 ...

  8. Android系统Surface机制的SurfaceFlinger服务的线程模型分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8062945 在前面两篇文章中,我们分析了Sur ...

  9. HTML中IE条件注释判断语句(<!--[if XX IE X]><![endif]-->)

    <!--[if XX IE X]><![endif]-->是IE专门提供的一种语法,其他浏览器会将其作为注释而忽略这些语句.   作用: 根据不同的IE版本加载对应的CSS或者 ...

  10. 影响布局的inline-block的空白符的问题

    昨天切页面时,进行布局时,想改变以下方法换换口味,所以就抛弃了float方法,采用了display:inline-block方法,没想到却随之而来的带来了一个想不通的问题,那就是空白.废话不多说,上代 ...