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 */ 017package org.apache.logging.log4j.core.util; 018 019import org.apache.logging.log4j.core.time.PreciseClock; 020import org.apache.logging.log4j.status.StatusLogger; 021import org.apache.logging.log4j.util.PropertiesUtil; 022import org.apache.logging.log4j.util.Supplier; 023 024import java.util.HashMap; 025import java.util.Map; 026 027/** 028 * Factory for {@code Clock} objects. 029 */ 030public final class ClockFactory { 031 032 /** 033 * Name of the system property that can be used to specify a {@code Clock} 034 * implementation class. The value of this property is {@value}. 035 */ 036 public static final String PROPERTY_NAME = "log4j.Clock"; 037 private static final StatusLogger LOGGER = StatusLogger.getLogger(); 038 039 // private static final Clock clock = createClock(); 040 041 private ClockFactory() { 042 } 043 044 /** 045 * Returns a {@code Clock} instance depending on the value of system 046 * property {@link #PROPERTY_NAME}. 047 * <p> 048 * If system property {@code log4j.Clock=CachedClock} is specified, 049 * this method returns an instance of {@link CachedClock}. If system 050 * property {@code log4j.Clock=CoarseCachedClock} is specified, this 051 * method returns an instance of {@link CoarseCachedClock}. 052 * </p> 053 * <p> 054 * If another value is specified, this value is taken as the fully qualified 055 * class name of a class that implements the {@code Clock} interface. An 056 * object of this class is instantiated and returned. 057 * </p> 058 * <p> 059 * If no value is specified, or if the specified value could not correctly 060 * be instantiated or did not implement the {@code Clock} interface, then an 061 * instance of {@link SystemClock} is returned. 062 * </p> 063 * 064 * @return a {@code Clock} instance 065 */ 066 public static Clock getClock() { 067 return createClock(); 068 } 069 070 private static Map<String, Supplier<Clock>> aliases() { 071 final Map<String, Supplier<Clock>> result = new HashMap<>(); 072 result.put("SystemClock", SystemClock::new); 073 result.put("SystemMillisClock", SystemMillisClock::new); 074 result.put("CachedClock", CachedClock::instance); 075 result.put("CoarseCachedClock", CoarseCachedClock::instance); 076 result.put("org.apache.logging.log4j.core.util.CachedClock", CachedClock::instance); 077 result.put("org.apache.logging.log4j.core.util.CoarseCachedClock", CoarseCachedClock::instance); 078 return result; 079 } 080 081 private static Clock createClock() { 082 final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME); 083 if (userRequest == null) { 084 LOGGER.trace("Using default SystemClock for timestamps."); 085 return logSupportedPrecision(new SystemClock()); 086 } 087 final Supplier<Clock> specified = aliases().get(userRequest); 088 if (specified != null) { 089 LOGGER.trace("Using specified {} for timestamps.", userRequest); 090 return logSupportedPrecision(specified.get()); 091 } 092 try { 093 final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class); 094 LOGGER.trace("Using {} for timestamps.", result.getClass().getName()); 095 return logSupportedPrecision(result); 096 } catch (final Exception e) { 097 final String fmt = "Could not create {}: {}, using default SystemClock for timestamps."; 098 LOGGER.error(fmt, userRequest, e); 099 return logSupportedPrecision(new SystemClock()); 100 } 101 } 102 103 private static Clock logSupportedPrecision(final Clock clock) { 104 final String support = clock instanceof PreciseClock ? "supports" : "does not support"; 105 LOGGER.debug("{} {} precise timestamps.", clock.getClass().getName(), support); 106 return clock; 107 } 108}