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 org.apache.logging.log4j.core.util.Loader; 021import org.apache.logging.log4j.status.StatusLogger; 022import org.apache.logging.log4j.util.PropertiesUtil; 023 024/** 025 * Factory for ReliabilityStrategies. 026 */ 027public final class ReliabilityStrategyFactory { 028 private ReliabilityStrategyFactory() { 029 } 030 031 /** 032 * Returns a new {@code ReliabilityStrategy} instance based on the value of system property 033 * {@code log4j.ReliabilityStrategy}. If not value was specified this method returns a new 034 * {@code AwaitUnconditionallyReliabilityStrategy}. 035 * <p> 036 * Valid values for this system property are {@code "AwaitUnconditionally"} (use 037 * {@code AwaitUnconditionallyReliabilityStrategy}), {@code "Locking"} (use {@code LockingReliabilityStrategy}) and 038 * {@code "AwaitCompletion"} (use the default {@code AwaitCompletionReliabilityStrategy}). 039 * <p> 040 * Users may also use this system property to specify the fully qualified class name of a class that implements the 041 * {@code ReliabilityStrategy} and has a constructor that accepts a single {@code LoggerConfig} argument. 042 * 043 * @param loggerConfig the LoggerConfig the resulting {@code ReliabilityStrategy} is associated with 044 * @return a ReliabilityStrategy that helps the specified LoggerConfig to log events reliably during or after a 045 * configuration change 046 */ 047 public static ReliabilityStrategy getReliabilityStrategy(final LoggerConfig loggerConfig) { 048 049 final String strategy = PropertiesUtil.getProperties().getStringProperty("log4j.ReliabilityStrategy", 050 "AwaitCompletion"); 051 if ("AwaitCompletion".equals(strategy)) { 052 return new AwaitCompletionReliabilityStrategy(loggerConfig); 053 } 054 if ("AwaitUnconditionally".equals(strategy)) { 055 return new AwaitUnconditionallyReliabilityStrategy(loggerConfig); 056 } 057 if ("Locking".equals(strategy)) { 058 return new LockingReliabilityStrategy(loggerConfig); 059 } 060 try { 061 final Class<? extends ReliabilityStrategy> cls = Loader.loadClass(strategy).asSubclass( 062 ReliabilityStrategy.class); 063 return cls.getConstructor(LoggerConfig.class).newInstance(loggerConfig); 064 } catch (final Exception dynamicFailed) { 065 StatusLogger.getLogger().warn( 066 "Could not create ReliabilityStrategy for '{}', using default AwaitCompletionReliabilityStrategy: {}", strategy, dynamicFailed); 067 return new AwaitCompletionReliabilityStrategy(loggerConfig); 068 } 069 } 070}