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.util;
018
019import java.util.Map;
020
021/**
022 * PropertySource implementation that uses environment variables as a source.
023 * All environment variables must begin with {@code LOG4J_} so as not to
024 * conflict with other variables. Normalized environment variables follow a
025 * scheme like this: {@code log4j2.fooBarProperty} would normalize to
026 * {@code LOG4J_FOO_BAR_PROPERTY}.
027 *
028 * @since 2.10.0
029 */
030public class EnvironmentPropertySource implements PropertySource {
031
032        private static final String PREFIX = "LOG4J_";
033        private static final int DEFAULT_PRIORITY = -100;
034
035        @Override
036        public int getPriority() {
037                return DEFAULT_PRIORITY;
038        }
039
040        @Override
041        public void forEach(final BiConsumer<String, String> action) {
042                final Map<String, String> getenv;
043                try {
044                        getenv = System.getenv();
045                } catch (final SecurityException e) {
046                        // There is no status logger yet.
047                        LowLevelLogUtil.logException(
048                                        "The system environment variables are not available to Log4j due to security restrictions: " + e,
049                                        e);
050                        return;
051                }
052                for (final Map.Entry<String, String> entry : getenv.entrySet()) {
053                        final String key = entry.getKey();
054                        if (key.startsWith(PREFIX)) {
055                                action.accept(key.substring(PREFIX.length()), entry.getValue());
056                        }
057                }
058        }
059
060        @Override
061        public CharSequence getNormalForm(final Iterable<? extends CharSequence> tokens) {
062                final StringBuilder sb = new StringBuilder("LOG4J");
063                for (final CharSequence token : tokens) {
064                        sb.append('_');
065                        for (int i = 0; i < token.length(); i++) {
066                                sb.append(Character.toUpperCase(token.charAt(i)));
067                        }
068                }
069                return sb.toString();
070        }
071}