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