LoggingApplicationListener
org.springframework.boot:spring-boot:1.3.0.M1
spring-boot-1.3.0.M1.jar
- package org.springframework.boot.logging;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.boot.ApplicationPid;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.bind.RelaxedPropertyResolver;
- import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
- import org.springframework.boot.context.event.ApplicationStartedEvent;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationEvent;
- import org.springframework.context.ApplicationListener;
- import org.springframework.context.event.ContextClosedEvent;
- import org.springframework.context.event.GenericApplicationListener;
- import org.springframework.core.Ordered;
- import org.springframework.core.ResolvableType;
- import org.springframework.core.env.ConfigurableEnvironment;
- import org.springframework.core.env.Environment;
- import org.springframework.util.LinkedMultiValueMap;
- import org.springframework.util.MultiValueMap;
- import org.springframework.util.ResourceUtils;
- import org.springframework.util.StringUtils;
- /**
- * An {@link ApplicationListener} that configures the {@link LoggingSystem}. If the
- * environment contains a {@code logging.config} property a then that will be used to
- * initialize the logging system, otherwise a default configuration is used.
- * <p>
- * By default, log output is only written to the console. If a log file is required the
- * {@code logging.path} and {@code logging.file} properties can be used.
- * <p>
- * Some system properties may be set as side effects, and these can be useful if the
- * logging configuration supports placeholders (i.e. log4j or logback):
- * <ul>
- * <li>{@code LOG_FILE} is set to the value of path of the log file that should be written
- * (if any).</li>
- * <li>{@code PID} is set to the value of the current process ID if it can be determined.</li>
- * </ul>
- *
- * @author Dave Syer
- * @author Phillip Webb
- * @author Andy Wilkinson
- * @see LoggingSystem#get(ClassLoader)
- */
- public class LoggingApplicationListener implements GenericApplicationListener {
- /**
- * The name of the Spring property that contains a reference to the logging
- * configuration to load.
- */
- public static final String CONFIG_PROPERTY = "logging.config";
- /**
- * The name of the Spring property that contains the path where the logging
- * configuration can be found.
- */
- public static final String PATH_PROPERTY = LogFile.PATH_PROPERTY;
- /**
- * The name of the Spring property that contains the name of the logging configuration
- * file.
- */
- public static final String FILE_PROPERTY = LogFile.FILE_PROPERTY;
- /**
- * The name of the System property that contains the process ID.
- */
- public static final String PID_KEY = "PID";
- private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
- static {
- LOG_LEVEL_LOGGERS = new LinkedMultiValueMap<LogLevel, String>();
- LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.springframework.boot");
- LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.springframework");
- LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.tomcat");
- LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.apache.catalina");
- LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.eclipse.jetty");
- LOG_LEVEL_LOGGERS.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl");
- LOG_LEVEL_LOGGERS.add(LogLevel.DEBUG, "org.hibernate.SQL");
- }
- private static Class<?>[] EVENT_TYPES = { ApplicationStartedEvent.class,
- ApplicationEnvironmentPreparedEvent.class, ContextClosedEvent.class };
- private static Class<?>[] SOURCE_TYPES = { SpringApplication.class,
- ApplicationContext.class };
- private final Log logger = LogFactory.getLog(getClass());
- private LoggingSystem loggingSystem;
- private int order = Ordered.HIGHEST_PRECEDENCE + 11;
- private boolean parseArgs = true;
- private LogLevel springBootLogging = null;
- @Override
- public boolean supportsEventType(ResolvableType resolvableType) {
- return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES);
- }
- @Override
- public boolean supportsSourceType(Class<?> sourceType) {
- return isAssignableFrom(sourceType, SOURCE_TYPES);
- }
- private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) {
- if (type != null) {
- for (Class<?> supportedType : supportedTypes) {
- if (supportedType.isAssignableFrom(type)) {
- return true;
- }
- }
- }
- return false;
- }
- @Override
- public void onApplicationEvent(ApplicationEvent event) {
- if (event instanceof ApplicationStartedEvent) {
- onApplicationStartedEvent((ApplicationStartedEvent) event);
- }
- else if (event instanceof ApplicationEnvironmentPreparedEvent) {
- onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
- }
- else if (event instanceof ContextClosedEvent) {
- onContextClosedEvent();
- }
- }
- private void onApplicationStartedEvent(ApplicationStartedEvent event) {
- this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
- .getClassLoader());
- this.loggingSystem.beforeInitialize();
- }
- private void onApplicationEnvironmentPreparedEvent(
- ApplicationEnvironmentPreparedEvent event) {
- if (this.loggingSystem == null) {
- this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
- .getClassLoader());
- }
- initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
- }
- private void onContextClosedEvent() {
- if (this.loggingSystem != null) {
- this.loggingSystem.cleanUp();
- }
- }
- /**
- * Initialize the logging system according to preferences expressed through the
- * {@link Environment} and the classpath.
- * @param environment the environment
- * @param classLoader the classloader
- */
- protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
- if (System.getProperty(PID_KEY) == null) {
- System.setProperty(PID_KEY, new ApplicationPid().toString());
- }
- initializeEarlyLoggingLevel(environment);
- initializeSystem(environment, this.loggingSystem);
- initializeFinalLoggingLevels(environment, this.loggingSystem);
- }
- private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
- if (this.parseArgs && this.springBootLogging == null) {
- if (environment.containsProperty("debug")) {
- this.springBootLogging = LogLevel.DEBUG;
- }
- if (environment.containsProperty("trace")) {
- this.springBootLogging = LogLevel.TRACE;
- }
- }
- }
- private void initializeSystem(ConfigurableEnvironment environment,
- LoggingSystem system) {
- LogFile logFile = LogFile.get(environment);
- String logConfig = environment.getProperty(CONFIG_PROPERTY);
- if (StringUtils.hasLength(logConfig)) {
- try {
- ResourceUtils.getURL(logConfig).openStream().close();
- system.initialize(logConfig, logFile);
- }
- catch (Exception ex) {
- this.logger.warn("Logging environment value '" + logConfig
- + "' cannot be opened and will be ignored "
- + "(using default location instead)");
- system.initialize(null, logFile);
- }
- }
- else {
- system.initialize(null, logFile);
- }
- }
- private void initializeFinalLoggingLevels(ConfigurableEnvironment environment,
- LoggingSystem system) {
- if (this.springBootLogging != null) {
- initializeLogLevel(system, this.springBootLogging);
- }
- setLogLevels(system, environment);
- }
- protected void initializeLogLevel(LoggingSystem system, LogLevel level) {
- List<String> loggers = LOG_LEVEL_LOGGERS.get(level);
- if (loggers != null) {
- for (String logger : loggers) {
- system.setLogLevel(logger, level);
- }
- }
- }
- protected void setLogLevels(LoggingSystem system, Environment environment) {
- Map<String, Object> levels = new RelaxedPropertyResolver(environment)
- .getSubProperties("logging.level.");
- for (Entry<String, Object> entry : levels.entrySet()) {
- setLogLevel(system, environment, entry.getKey(), entry.getValue().toString());
- }
- }
- private void setLogLevel(LoggingSystem system, Environment environment, String name,
- String level) {
- try {
- if (name.equalsIgnoreCase("root")) {
- name = null;
- }
- level = environment.resolvePlaceholders(level);
- system.setLogLevel(name, LogLevel.valueOf(level));
- }
- catch (RuntimeException ex) {
- this.logger.error("Cannot set level: " + level + " for '" + name + "'");
- }
- }
- public void setOrder(int order) {
- this.order = order;
- }
- @Override
- public int getOrder() {
- return this.order;
- }
- /**
- * Sets a custom logging level to be used for Spring Boot and related libraries.
- * @param springBootLogging the logging level
- */
- public void setSpringBootLogging(LogLevel springBootLogging) {
- this.springBootLogging = springBootLogging;
- }
- /**
- * Sets if initialization arguments should be parsed for {@literal --debug} and
- * {@literal --trace} options. Defaults to {@code true}.
- * @param parseArgs if arguments should be parsed
- */
- public void setParseArgs(boolean parseArgs) {
- this.parseArgs = parseArgs;
- }
- }
LoggingApplicationListener的更多相关文章
- 使用canal分析binlog(一) 入门
canal介绍 canal是应阿里巴巴存在杭州和美国的双机房部署,存在跨机房同步的业务需求而提出的.早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数据库同步 ...
- spring boot源码分析之SpringApplication
spring boot提供了sample程序,学习spring boot之前先跑一个最简单的示例: /* * Copyright 2012-2016 the original author or au ...
- Spring Boot启动流程详解(一)
环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...
- springboot源码解析 - 构建SpringApplication
1 package com.microservice.framework; 2 3 import org.springframework.boot.SpringApplication; 4 impor ...
- Logback相关知识汇总
例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4格式修饰符,与转换符共同使用:可选的格式修饰符位于“%”和转换符之间.第一个可选修饰符是左对齐 标志 ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- 深入理解SpringCloud之引导程序应用上下文
tips:我希望通过这篇文章来给对于bootstrap还不理解的朋友带来帮助.当然这篇文章不仅仅是讲解知识,我更希望给广大朋友带来学习与理解官方文档的一种思路.阅读本文前,建议大家对SpringBoo ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
随机推荐
- Java 学习 第三篇;面向对象
1:Java的常用包: 核心类在java 包中:扩展类在javax包中 java.lang 系统默认自动导入 包含String Math System Thread等类 java.util 包含了工具 ...
- HTML常用字符
显示结果 描述 实体名称 实体编号 空格 < 小于号 < < > 大于号 > > & 和号 & & " 引号 &quo ...
- \s+(?<request_time>\d+(?:\.\d+)?)\s+ 解释
<pre name="code" class="html"><pre name="code" class="ht ...
- 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 ...
- LeeCode-Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...
- [转]Traceroute网络排障实用指南(2)
五.优先级与限速 5.1 Traceroute延时判断影响因素 Traceroute延时包括三点: 探测包到达一个特定路由器的时间 路由器生成IPMI TTL Exceed的时间 ICMP TTL E ...
- 武道释义 · 零散
若是你防御你的左边.你的右边必定是弱点: 若是你防御你的前面,你的后面必定是弱点. 若是你处处小心防御,则必定处处都是弱点." 有些武术尽管先声夺人.但却如喝渗水之酒,令人越瞧越觉无味: 但 ...
- Android系统Surface机制的SurfaceFlinger服务的线程模型分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8062945 在前面两篇文章中,我们分析了Sur ...
- HTML中IE条件注释判断语句(<!--[if XX IE X]><![endif]-->)
<!--[if XX IE X]><![endif]-->是IE专门提供的一种语法,其他浏览器会将其作为注释而忽略这些语句. 作用: 根据不同的IE版本加载对应的CSS或者 ...
- 影响布局的inline-block的空白符的问题
昨天切页面时,进行布局时,想改变以下方法换换口味,所以就抛弃了float方法,采用了display:inline-block方法,没想到却随之而来的带来了一个想不通的问题,那就是空白.废话不多说,上代 ...