1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17
18 package org.apache.logging.log4j.core.config;
19
20 import java.util.Objects;
21
22 import org.apache.logging.log4j.Level;
23 import org.apache.logging.log4j.Marker;
24 import org.apache.logging.log4j.core.LogEvent;
25 import org.apache.logging.log4j.message.Message;
26 import org.apache.logging.log4j.status.StatusLogger;
27 import org.apache.logging.log4j.util.PropertiesUtil;
28 import org.apache.logging.log4j.util.Supplier;
29
30 /**
31 * Reliability strategy that sleeps unconditionally for some time before allowing a Configuration to be stopped.
32 */
33 public class AwaitUnconditionallyReliabilityStrategy implements ReliabilityStrategy {
34
35 private static final long DEFAULT_SLEEP_MILLIS = 5000; // 5 seconds
36 private static final long SLEEP_MILLIS = sleepMillis();
37 private final LoggerConfig loggerConfig;
38
39 public AwaitUnconditionallyReliabilityStrategy(final LoggerConfig loggerConfig) {
40 this.loggerConfig = Objects.requireNonNull(loggerConfig, "loggerConfig is null");
41 }
42
43 private static long sleepMillis() {
44 return PropertiesUtil.getProperties().getLongProperty("log4j.waitMillisBeforeStopOldConfig",
45 DEFAULT_SLEEP_MILLIS);
46 }
47
48 /*
49 * (non-Javadoc)
50 *
51 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#log(org.apache.logging.log4j.util.Supplier,
52 * java.lang.String, java.lang.String, org.apache.logging.log4j.Marker, org.apache.logging.log4j.Level,
53 * org.apache.logging.log4j.message.Message, java.lang.Throwable)
54 */
55 @Override
56 public void log(final Supplier<LoggerConfig> reconfigured, final String loggerName, final String fqcn, final Marker marker, final Level level,
57 final Message data, final Throwable t) {
58 loggerConfig.log(loggerName, fqcn, marker, level, data, t);
59 }
60
61 /*
62 * (non-Javadoc)
63 *
64 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#log(org.apache.logging.log4j.util.Supplier,
65 * org.apache.logging.log4j.core.LogEvent)
66 */
67 @Override
68 public void log(final Supplier<LoggerConfig> reconfigured, final LogEvent event) {
69 loggerConfig.log(event);
70 }
71
72 /*
73 * (non-Javadoc)
74 *
75 * @see
76 * org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeLogEvent(org.apache.logging.log4j.core.config.
77 * LoggerConfig, org.apache.logging.log4j.util.Supplier)
78 */
79 @Override
80 public LoggerConfig getActiveLoggerConfig(final Supplier<LoggerConfig> next) {
81 return this.loggerConfig;
82 }
83
84 /*
85 * (non-Javadoc)
86 *
87 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#afterLogEvent()
88 */
89 @Override
90 public void afterLogEvent() {
91 // no action
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeStopAppenders()
98 */
99 @Override
100 public void beforeStopAppenders() {
101 // no action
102 }
103
104 /*
105 * (non-Javadoc)
106 *
107 * @see
108 * org.apache.logging.log4j.core.config.ReliabilityStrategy#beforeStopConfiguration(org.apache.logging.log4j.core
109 * .config.Configuration)
110 */
111 @Override
112 public void beforeStopConfiguration(final Configuration configuration) {
113 // only sleep once per configuration stop
114 if (loggerConfig == configuration.getRootLogger()) {
115 try {
116 Thread.sleep(SLEEP_MILLIS);
117 } catch (final InterruptedException e) {
118 StatusLogger.getLogger().warn("Sleep before stop configuration was interrupted.");
119 }
120 }
121 }
122
123 }