下面列出了怎么用org.springframework.boot.logging.LogFile的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) {
super.loadConfiguration(initializationContext, location, logFile);
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
try {
configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location));
} catch (Exception ex) {
throw new IllegalStateException("Could not initialize Logback logging from " + location, ex);
}
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
StringBuilder errors = new StringBuilder();
for (Status status : statuses) {
if (status.getLevel() == Status.ERROR) {
errors.append(errors.length() > 0 ? String.format("%n") : "");
errors.append(status.toString());
}
}
if (errors.length() > 0) {
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors));
}
}
private void reinitializeLoggingSystem(ConfigurableEnvironment environment,
String oldLogConfig, LogFile oldLogFile) {
Map<String, Object> props = Binder.get(environment)
.bind("logging", Bindable.mapOf(String.class, Object.class))
.orElseGet(Collections::emptyMap);
if (!props.isEmpty()) {
String logConfig = environment.resolvePlaceholders("${logging.config:}");
LogFile logFile = LogFile.get(environment);
LoggingSystem system = LoggingSystem
.get(LoggingSystem.class.getClassLoader());
try {
ResourceUtils.getURL(logConfig).openStream().close();
// Three step initialization that accounts for the clean up of the logging
// context before initialization. Spring Boot doesn't initialize a logging
// system that hasn't had this sequence applied (since 1.4.1).
system.cleanUp();
system.beforeInitialize();
system.initialize(new LoggingInitializationContext(environment),
logConfig, logFile);
}
catch (Exception ex) {
PropertySourceBootstrapConfiguration.logger
.warn("Error opening logging config file " + logConfig, ex);
}
}
}
protected Appender<ILoggingEvent> fileAppender(Space space) {
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
String logPattern = this.patterns.getProperty("logging.pattern.file", FILE_LOG_PATTERN);
encoder.setPattern(OptionHelper.substVars(logPattern, context));
encoder.setCharset(DEFAULT_CHARSET);
appender.setEncoder(encoder);
start(encoder);
// parse path and file
// first consider spec.path, second, default_spec.path, third logging.path
LogFile logFile = LogFile.get(patterns);
Properties defaultProperties = new Properties();
if (logFile != null) {
logFile.applyTo(defaultProperties);
}
String path = space.getSpec().getPath() != null ? space.getSpec().getPath() :
space.getDefaultSpec().getPath() != null ? space.getDefaultSpec().getPath() :
defaultProperties.contains(LoggingSystemProperties.LOG_PATH)
? defaultProperties.getProperty(LoggingSystemProperties.LOG_PATH) :
DEFAULT_PATH;
path = patterns.resolvePlaceholders(path);
String file = space.getSpec().getFile() != null
? fileName(space.getSpec().getFile()) : fileName(space.getName());
file = patterns.resolvePlaceholders(file);
appender.setFile(path + "/" + file);
setRollingPolicy(appender, space, path, file);
// threshold config
ThresholdFilter thresholdFilter = new ThresholdFilter();
if (space.getSpec().getThreshold() != null) {
thresholdFilter.setLevel(space.getSpec().getThreshold());
start(thresholdFilter);
appender.addFilter(thresholdFilter);
}
appender("SPACE-" + space.getName(), appender);
return appender;
}
private Resource getLogFileResource() {
if (this.externalFile != null) {
return new FileSystemResource(this.externalFile.toFile());
}
LogFile logFile = LogFile.get(this.environment);
if (logFile == null) {
log.debug("Missing 'logging.file' or 'logging.path' properties");
return null;
}
return new FileSystemResource(logFile.toString());
}
@Override
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
LoggerContext loggerContext = getLoggerContext();
if (isAlreadyInitialized(loggerContext)) {
return;
}
loggerContext.getTurboFilterList().remove(FILTER);
super.initialize(initializationContext, configLocation, logFile);
markAsInitialized(loggerContext);
if (StringUtils.hasText(System.getProperty(CONFIGURATION_FILE_PROPERTY))) {
getLogger(LogbackLoggingSystem.class.getName()).warn(
"Ignoring '" + CONFIGURATION_FILE_PROPERTY + "' system property. " + "Please use 'logging.config' instead.");
}
}
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
LoggerContext context = getLoggerContext();
stopAndReset(context);
LogbackConfigurator configurator = new LogbackConfigurator(context);
context.putProperty("LOG_LEVEL_PATTERN",
initializationContext.getEnvironment().resolvePlaceholders("${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}"));
new EnhancedLogbackConfiguration(initializationContext, logFile).apply(configurator);
context.setPackagingDataEnabled(true);
}
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
if (logFile != null) {
this.loadConfiguration(this.getBootPackagedConfigFile("log4j2-file.xml"), logFile);
} else {
this.loadConfiguration(this.getBootPackagedConfigFile("log4j2.xml"), logFile);
}
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
PropertySource ps = new MapPropertySource("LogFileLocationPS",
Collections.singletonMap(LogFile.FILE_PROPERTY, SettingsService.getLogFile().toAbsolutePath().toString()));
event.getEnvironment().getPropertySources().addLast(ps);
}
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
beforeLoadSpaces(initializationContext);
super.loadDefaults(initializationContext, logFile);
loadSpaces(initializationContext);
}
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) {
beforeLoadSpaces(initializationContext);
super.loadConfiguration(initializationContext, location, logFile);
loadSpaces(initializationContext);
}
EnhancedLogbackConfiguration(LoggingInitializationContext initializationContext, LogFile logFile) {
this.logging = getPatternsResolver(initializationContext.getEnvironment());
this.logFile = logFile;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
PropertySource ps = new MapPropertySource("LogFileLocationPS",
Collections.singletonMap(LogFile.FILE_PROPERTY, SettingsService.getLogFile().getAbsolutePath()));
event.getEnvironment().getPropertySources().addLast(ps);
}
/**
* Set the environment into the ExternalContext field so that it can be obtained by SpringLookup when it
* is constructed. Spring will replace the ExternalContext field with a String once initialization is
* complete.
* @param initializationContext The initialization context.
* @param configLocation The configuration location.
* @param logFile the log file.
*/
@Override
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, initializationContext.getEnvironment());
super.initialize(initializationContext, configLocation, logFile);
}