001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.logging; 018 019import org.springframework.boot.system.ApplicationPid; 020import org.springframework.core.env.ConfigurableEnvironment; 021import org.springframework.core.env.Environment; 022import org.springframework.core.env.PropertyResolver; 023import org.springframework.core.env.PropertySourcesPropertyResolver; 024import org.springframework.util.Assert; 025 026/** 027 * Utility to set system properties that can later be used by log configuration files. 028 * 029 * @author Andy Wilkinson 030 * @author Phillip Webb 031 * @author Madhura Bhave 032 * @author Vedran Pavic 033 * @since 2.0.0 034 */ 035public class LoggingSystemProperties { 036 037 /** 038 * The name of the System property that contains the process ID. 039 */ 040 public static final String PID_KEY = "PID"; 041 042 /** 043 * The name of the System property that contains the exception conversion word. 044 */ 045 public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD"; 046 047 /** 048 * The name of the System property that contains the log file. 049 */ 050 public static final String LOG_FILE = "LOG_FILE"; 051 052 /** 053 * The name of the System property that contains the log path. 054 */ 055 public static final String LOG_PATH = "LOG_PATH"; 056 057 /** 058 * The name of the System property that contains the console log pattern. 059 */ 060 public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN"; 061 062 /** 063 * The name of the System property that contains the file log pattern. 064 */ 065 public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN"; 066 067 /** 068 * The name of the System property that contains the file log max history. 069 */ 070 public static final String FILE_MAX_HISTORY = "LOG_FILE_MAX_HISTORY"; 071 072 /** 073 * The name of the System property that contains the file log max size. 074 */ 075 public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE"; 076 077 /** 078 * The name of the System property that contains the log level pattern. 079 */ 080 public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN"; 081 082 /** 083 * The name of the System property that contains the log date-format pattern. 084 */ 085 public static final String LOG_DATEFORMAT_PATTERN = "LOG_DATEFORMAT_PATTERN"; 086 087 private final Environment environment; 088 089 /** 090 * Create a new {@link LoggingSystemProperties} instance. 091 * @param environment the source environment 092 */ 093 public LoggingSystemProperties(Environment environment) { 094 Assert.notNull(environment, "Environment must not be null"); 095 this.environment = environment; 096 } 097 098 public void apply() { 099 apply(null); 100 } 101 102 public void apply(LogFile logFile) { 103 PropertyResolver resolver = getPropertyResolver(); 104 setSystemProperty(resolver, EXCEPTION_CONVERSION_WORD, 105 "exception-conversion-word"); 106 setSystemProperty(PID_KEY, new ApplicationPid().toString()); 107 setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "pattern.console"); 108 setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file"); 109 setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history"); 110 setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size"); 111 setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level"); 112 setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat"); 113 if (logFile != null) { 114 logFile.applyToSystemProperties(); 115 } 116 } 117 118 private PropertyResolver getPropertyResolver() { 119 if (this.environment instanceof ConfigurableEnvironment) { 120 PropertyResolver resolver = new PropertySourcesPropertyResolver( 121 ((ConfigurableEnvironment) this.environment).getPropertySources()); 122 ((PropertySourcesPropertyResolver) resolver) 123 .setIgnoreUnresolvableNestedPlaceholders(true); 124 return resolver; 125 } 126 return this.environment; 127 } 128 129 private void setSystemProperty(PropertyResolver resolver, String systemPropertyName, 130 String propertyName) { 131 setSystemProperty(systemPropertyName, 132 resolver.getProperty("logging." + propertyName)); 133 } 134 135 private void setSystemProperty(String name, String value) { 136 if (System.getProperty(name) == null && value != null) { 137 System.setProperty(name, value); 138 } 139 } 140 141}