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 org.apache.logging.log4j.core.util.Loader;
21 import org.apache.logging.log4j.status.StatusLogger;
22 import org.apache.logging.log4j.util.PropertiesUtil;
23
24 /**
25 * Factory for ReliabilityStrategies.
26 */
27 public final class ReliabilityStrategyFactory {
28 private ReliabilityStrategyFactory() {
29 }
30
31 /**
32 * Returns a new {@code ReliabilityStrategy} instance based on the value of system property
33 * {@code log4j.ReliabilityStrategy}. If not value was specified this method returns a new
34 * {@code AwaitUnconditionallyReliabilityStrategy}.
35 * <p>
36 * Valid values for this system property are {@code "AwaitUnconditionally"} (use
37 * {@code AwaitUnconditionallyReliabilityStrategy}), {@code "Locking"} (use {@code LockingReliabilityStrategy}) and
38 * {@code "AwaitCompletion"} (use the default {@code AwaitCompletionReliabilityStrategy}).
39 * <p>
40 * Users may also use this system property to specify the fully qualified class name of a class that implements the
41 * {@code ReliabilityStrategy} and has a constructor that accepts a single {@code LoggerConfig} argument.
42 *
43 * @param loggerConfig the LoggerConfig the resulting {@code ReliabilityStrategy} is associated with
44 * @return a ReliabilityStrategy that helps the specified LoggerConfig to log events reliably during or after a
45 * configuration change
46 */
47 public static ReliabilityStrategy getReliabilityStrategy(final LoggerConfig loggerConfig) {
48
49 final String strategy = PropertiesUtil.getProperties().getStringProperty("log4j.ReliabilityStrategy",
50 "AwaitCompletion");
51 if ("AwaitCompletion".equals(strategy)) {
52 return new AwaitCompletionReliabilityStrategy(loggerConfig);
53 }
54 if ("AwaitUnconditionally".equals(strategy)) {
55 return new AwaitUnconditionallyReliabilityStrategy(loggerConfig);
56 }
57 if ("Locking".equals(strategy)) {
58 return new LockingReliabilityStrategy(loggerConfig);
59 }
60 try {
61 final Class<? extends ReliabilityStrategy> cls = Loader.loadClass(strategy).asSubclass(
62 ReliabilityStrategy.class);
63 return cls.getConstructor(LoggerConfig.class).newInstance(loggerConfig);
64 } catch (final Exception dynamicFailed) {
65 StatusLogger.getLogger().warn(
66 "Could not create ReliabilityStrategy for '{}', using default AwaitCompletionReliabilityStrategy: {}", strategy, dynamicFailed);
67 return new AwaitCompletionReliabilityStrategy(loggerConfig);
68 }
69 }
70 }