001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.core.config; 019 020import java.util.Objects; 021 022import org.apache.logging.log4j.Level; 023import org.apache.logging.log4j.Marker; 024import org.apache.logging.log4j.core.LogEvent; 025import org.apache.logging.log4j.message.Message; 026import org.apache.logging.log4j.status.StatusLogger; 027import org.apache.logging.log4j.util.PropertiesUtil; 028import org.apache.logging.log4j.util.Supplier; 029 030/** 031 * Reliability strategy that sleeps unconditionally for some time before allowing a Configuration to be stopped. 032 */ 033public class AwaitUnconditionallyReliabilityStrategy implements ReliabilityStrategy { 034 035 private static final long DEFAULT_SLEEP_MILLIS = 5000; // 5 seconds 036 private static final long SLEEP_MILLIS = sleepMillis(); 037 private final LoggerConfig loggerConfig; 038 039 public AwaitUnconditionallyReliabilityStrategy(final LoggerConfig loggerConfig) { 040 this.loggerConfig = Objects.requireNonNull(loggerConfig, "loggerConfig is null"); 041 } 042 043 private static long sleepMillis() { 044 return PropertiesUtil.getProperties().getLongProperty("log4j.waitMillisBeforeStopOldConfig", 045 DEFAULT_SLEEP_MILLIS); 046 } 047 048 /* 049 * (non-Javadoc) 050 * 051 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#log(org.apache.logging.log4j.util.Supplier, 052 * java.lang.String, java.lang.String, org.apache.logging.log4j.Marker, org.apache.logging.log4j.Level, 053 * org.apache.logging.log4j.message.Message, java.lang.Throwable) 054 */ 055 @Override 056 public void log(Supplier<LoggerConfig> reconfigured, String loggerName, String fqcn, Marker marker, Level level, 057 Message data, Throwable t) { 058 loggerConfig.log(loggerName, fqcn, marker, level, data, t); 059 } 060 061 /* 062 * (non-Javadoc) 063 * 064 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#log(org.apache.logging.log4j.util.Supplier, 065 * org.apache.logging.log4j.core.LogEvent) 066 */ 067 @Override 068 public void log(Supplier<LoggerConfig> reconfigured, LogEvent event) { 069 loggerConfig.log(event); 070 } 071 072 /* 073 * (non-Javadoc) 074 * 075 * @see 076 * org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeLogEvent(org.apache.logging.log4j.core.config. 077 * LoggerConfig, org.apache.logging.log4j.util.Supplier) 078 */ 079 @Override 080 public LoggerConfig getActiveLoggerConfig(Supplier<LoggerConfig> next) { 081 return this.loggerConfig; 082 } 083 084 /* 085 * (non-Javadoc) 086 * 087 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#afterLogEvent() 088 */ 089 @Override 090 public void afterLogEvent() { 091 // no action 092 } 093 094 /* 095 * (non-Javadoc) 096 * 097 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeStopAppenders() 098 */ 099 @Override 100 public void beforeStopAppenders() { 101 // no action 102 } 103 104 /* (non-Javadoc) 105 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeStopConfiguration(org.apache.logging.log4j.core.config.Configuration) 106 */ 107 @Override 108 public void beforeStopConfiguration(Configuration configuration) { 109 // only sleep once per configuration stop 110 if (loggerConfig == configuration.getRootLogger()) { 111 try { 112 Thread.sleep(SLEEP_MILLIS); 113 } catch (InterruptedException e) { 114 StatusLogger.getLogger().warn("Sleep before stop configuration was interrupted."); 115 } 116 } 117 } 118 119}