/** * Gets the named property as a String. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the String value of the property or {@code defaultValue} if undefined. */ public String getStringProperty(final String name, final String defaultValue) { final String prop = getStringProperty(name); return (prop == null) ? defaultValue : prop; }
/** * Gets the named property as a boolean value. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the boolean value of the property or {@code defaultValue} if undefined. */ public boolean getBooleanProperty(final String name, final boolean defaultValue) { final String prop = getStringProperty(name); return prop == null ? defaultValue : "true".equalsIgnoreCase(prop); }
/** * Returns true if system properties tell us we are running on Windows. * * @return true if system properties tell us we are running on Windows. */ public boolean isOsWindows() { return getStringProperty("os.name", "").startsWith("Windows"); }
/** * Gets the named property as a double. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the parsed double value of the property or {@code defaultValue} if it was undefined or could not be parsed. */ public double getDoubleProperty(final String name, final double defaultValue) { final String prop = getStringProperty(name); if (prop != null) { try { return Double.parseDouble(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; }
/** * Gets the named property as a long. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the parsed long value of the property or {@code defaultValue} if it was undefined or could not be parsed. */ public long getLongProperty(final String name, final long defaultValue) { final String prop = getStringProperty(name); if (prop != null) { try { return Long.parseLong(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; }
/** * Gets the named property as a boolean value. * * @param name the name of the property to look up * @param defaultValueIfAbsent the default value to use if the property is undefined * @param defaultValueIfPresent the default value to use if the property is defined but not assigned * @return the boolean value of the property or {@code defaultValue} if undefined. */ public boolean getBooleanProperty(final String name, final boolean defaultValueIfAbsent, final boolean defaultValueIfPresent) { final String prop = getStringProperty(name); return prop == null ? defaultValueIfAbsent : prop.isEmpty() ? defaultValueIfPresent : "true".equalsIgnoreCase(prop); }
/** * Gets the named property as an integer. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the parsed integer value of the property or {@code defaultValue} if it was undefined or could not be * parsed. */ public int getIntegerProperty(final String name, final int defaultValue) { final String prop = getStringProperty(name); if (prop != null) { try { return Integer.parseInt(prop); } catch (final Exception ignored) { return defaultValue; } } return defaultValue; }
private static boolean isIgnoreTccl() { // we need to lazily initialize this, but concurrent access is not an issue if (ignoreTCCL == null) { final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null); ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim()); } return ignoreTCCL; }
private static Class<? extends MessageFactory> createClassForProperty(final String property, final Class<ReusableMessageFactory> reusableParameterizedMessageFactoryClass, final Class<ParameterizedMessageFactory> parameterizedMessageFactoryClass) { try { final String fallback = Constants.ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName() : parameterizedMessageFactoryClass.getName(); final String clsName = PropertiesUtil.getProperties().getStringProperty(property, fallback); return LoaderUtil.loadClass(clsName).asSubclass(MessageFactory.class); } catch (final Throwable t) { return parameterizedMessageFactoryClass; } }
private static Class<? extends FlowMessageFactory> createFlowClassForProperty(final String property, final Class<DefaultFlowMessageFactory> defaultFlowMessageFactoryClass) { try { final String clsName = PropertiesUtil.getProperties().getStringProperty(property, defaultFlowMessageFactoryClass.getName()); return LoaderUtil.loadClass(clsName).asSubclass(FlowMessageFactory.class); } catch (final Throwable t) { return defaultFlowMessageFactoryClass; } }
private static int calculateRingBufferSize(final String propertyName) { final String userPreferredRBSize = PropertiesUtil.getProperties().getStringProperty(propertyName, String.valueOf(RINGBUFFER_MIN_SIZE)); try { int size = Integer.parseInt(userPreferredRBSize); if (size < RINGBUFFER_MIN_SIZE) { size = RINGBUFFER_MIN_SIZE; LOGGER.warn("Invalid {} {}, using minimum size {}.", propertyName, userPreferredRBSize, RINGBUFFER_MIN_SIZE); } return ceilingNextPowerOfTwo(size); } catch (final Exception ex) { LOGGER.warn("Invalid {} {}, using default size {}.", propertyName, userPreferredRBSize, RINGBUFFER_MIN_SIZE); return RINGBUFFER_MIN_SIZE; } }
/** * Initializes static variables based on system properties. Normally called when this class is initialized by the VM * and when Log4j is reconfigured. */ private static void initPrivate() { final PropertiesUtil properties = PropertiesUtil.getProperties(); ThreadContextMapName = properties.getStringProperty(THREAD_CONTEXT_KEY); GcFreeThreadContextKey = properties.getBooleanProperty(GC_FREE_THREAD_CONTEXT_KEY); }
final PrintStream stream) { super(name, messageFactory); final String lvl = props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + name + ".level"); this.level = Level.toLevel(lvl, defaultLevel); if (showShortLogName) {
/** * Gets the named property as a Charset value. If we cannot find the named Charset, see if it is mapped in * file {@code Log4j-charsets.properties} on the class path. * * @param name the name of the property to look up * @param defaultValue the default value to use if the property is undefined * @return the Charset value of the property or {@code defaultValue} if undefined. */ public Charset getCharsetProperty(final String name, final Charset defaultValue) { final String charsetName = getStringProperty(name); if (charsetName == null) { return defaultValue; } if (Charset.isSupported(charsetName)) { return Charset.forName(charsetName); } ResourceBundle bundle = getCharsetsResourceBundle(); if (bundle.containsKey(name)) { String mapped = bundle.getString(name); if (Charset.isSupported(mapped)) { return Charset.forName(mapped); } } LowLevelLogUtil.log("Unable to get Charset '" + charsetName + "' for property '" + name + "', using default " + defaultValue + " and continuing."); return defaultValue; }
public SimpleLoggerContext() { props = new PropertiesUtil("log4j2.simplelog.properties"); showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false); showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false); showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true); showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false); final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level"); defaultLevel = Level.toLevel(lvl, Level.ERROR); dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat", DEFAULT_DATE_TIME_FORMAT) : null; final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", SYSTEM_ERR); PrintStream ps; if (SYSTEM_ERR.equalsIgnoreCase(fileName)) { ps = System.err; } else if (SYSTEM_OUT.equalsIgnoreCase(fileName)) { ps = System.out; } else { try { final FileOutputStream os = new FileOutputStream(fileName); ps = new PrintStream(os); } catch (final FileNotFoundException fnfe) { ps = System.err; } } this.stream = ps; }
/** * Loads and instantiates a class given by a property name. * * @param propertyName The property name to look up a class name for. * @param clazz The class to cast it to. * @param <T> The type to cast it to. * @return new instance of the class given in the property or {@code null} if the property was unset. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.5 */ public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { final String className = PropertiesUtil.getProperties().getStringProperty(propertyName); if (className == null) { return null; } return newCheckedInstanceOf(className, clazz); }
@Test public void testNonStringSystemProperties() { Object key1 = "1"; Object key2 = new Object(); System.getProperties().put(key1, new Object()); System.getProperties().put(key2, "value-2"); try { final PropertiesUtil util = new PropertiesUtil(new Properties()); assertNull(util.getStringProperty("1")); } finally { System.getProperties().remove(key1); System.getProperties().remove(key2); } } }
private StatusLogger(final String name, final MessageFactory messageFactory) { super(name, messageFactory); final String dateFormat = PROPS.getStringProperty(STATUS_DATE_FORMAT, Strings.EMPTY); final boolean showDateTime = !Strings.isEmpty(dateFormat); this.logger = new SimpleLogger("StatusLogger", Level.ERROR, false, true, showDateTime, false, dateFormat, messageFactory, PROPS, System.err); this.listenersLevel = Level.toLevel(DEFAULT_STATUS_LEVEL, Level.WARN).intLevel(); // LOG4J2-1813 if system property "log4j2.debug" is defined, print all status logging if (isDebugPropertyEnabled()) { logger.setLevel(Level.TRACE); } }
/** * Makes sure we do not blow up with exception below due to a security manager * rejecting environment variable access in * {@link SystemPropertiesPropertySource}. * * <pre> * </pre> */ @Test public void test() { final PropertiesUtil propertiesUtil = new PropertiesUtil(TEST_FIXTURE_PATH); Assert.assertEquals(null, propertiesUtil.getStringProperty("a.1")); } }
/** * Makes sure we do not blow up with exception below due to a security manager * rejecting environment variable access in * {@link SystemPropertiesPropertySource}. * * <pre> * java.lang.ExceptionInInitializerError * at org.apache.logging.log4j.util.SystemPropertiesPropertySourceSecurityManagerTest.test(SystemPropertiesPropertySourceSecurityManagerTest.java:64) * ... * Caused by: java.lang.SecurityException * at org.apache.logging.log4j.util.SystemPropertiesPropertySourceSecurityManagerTest$TestSecurityManager.checkPermission(SystemPropertiesPropertySourceSecurityManagerTest.java:49) * at java.lang.SecurityManager.checkPropertiesAccess(SecurityManager.java:1265) * at java.lang.System.getProperties(System.java:624) * at org.apache.logging.log4j.util.SystemPropertiesPropertySource.forEach(SystemPropertiesPropertySource.java:40) * at org.apache.logging.log4j.util.PropertiesUtil$Environment.reload(PropertiesUtil.java:330) * at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:322) * at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:310) * at org.apache.logging.log4j.util.PropertiesUtil.<init>(PropertiesUtil.java:69) * at org.apache.logging.log4j.util.PropertiesUtil.<clinit>(PropertiesUtil.java:49) * ... 26 more * </pre> */ @Test public void test() { final PropertiesUtil propertiesUtil = new PropertiesUtil("src/test/resources/PropertiesUtilTest.properties"); Assert.assertEquals(null, propertiesUtil.getStringProperty("a.1")); } }