001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.logging;
018
019import java.util.Properties;
020
021import org.springframework.core.env.Environment;
022import org.springframework.core.env.PropertyResolver;
023import org.springframework.util.Assert;
024import org.springframework.util.StringUtils;
025
026/**
027 * A reference to a log output file. Log output files are specified using
028 * {@code logging.file} or {@code logging.path} {@link Environment} properties. If the
029 * {@code logging.file} property is not specified {@code "spring.log"} will be written in
030 * the {@code logging.path} directory.
031 *
032 * @author Phillip Webb
033 * @since 1.2.1
034 * @see #get(PropertyResolver)
035 */
036public class LogFile {
037
038        /**
039         * The name of the Spring property that contains the name of the log file. Names can
040         * be an exact location or relative to the current directory.
041         */
042        public static final String FILE_PROPERTY = "logging.file";
043
044        /**
045         * The name of the Spring property that contains the directory where log files are
046         * written.
047         */
048        public static final String PATH_PROPERTY = "logging.path";
049
050        private final String file;
051
052        private final String path;
053
054        /**
055         * Create a new {@link LogFile} instance.
056         * @param file a reference to the file to write
057         */
058        LogFile(String file) {
059                this(file, null);
060        }
061
062        /**
063         * Create a new {@link LogFile} instance.
064         * @param file a reference to the file to write
065         * @param path a reference to the logging path to use if {@code file} is not specified
066         */
067        LogFile(String file, String path) {
068                Assert.isTrue(StringUtils.hasLength(file) || StringUtils.hasLength(path),
069                                "File or Path must not be empty");
070                this.file = file;
071                this.path = path;
072        }
073
074        /**
075         * Apply log file details to {@code LOG_PATH} and {@code LOG_FILE} system properties.
076         */
077        public void applyToSystemProperties() {
078                applyTo(System.getProperties());
079        }
080
081        /**
082         * Apply log file details to {@code LOG_PATH} and {@code LOG_FILE} map entries.
083         * @param properties the properties to apply to
084         */
085        public void applyTo(Properties properties) {
086                put(properties, LoggingSystemProperties.LOG_PATH, this.path);
087                put(properties, LoggingSystemProperties.LOG_FILE, toString());
088        }
089
090        private void put(Properties properties, String key, String value) {
091                if (StringUtils.hasLength(value)) {
092                        properties.put(key, value);
093                }
094        }
095
096        @Override
097        public String toString() {
098                if (StringUtils.hasLength(this.file)) {
099                        return this.file;
100                }
101                String path = this.path;
102                if (!path.endsWith("/")) {
103                        path = path + "/";
104                }
105                return StringUtils.applyRelativePath(path, "spring.log");
106        }
107
108        /**
109         * Get a {@link LogFile} from the given Spring {@link Environment}.
110         * @param propertyResolver the {@link PropertyResolver} used to obtain the logging
111         * properties
112         * @return a {@link LogFile} or {@code null} if the environment didn't contain any
113         * suitable properties
114         */
115        public static LogFile get(PropertyResolver propertyResolver) {
116                String file = propertyResolver.getProperty(FILE_PROPERTY);
117                String path = propertyResolver.getProperty(PATH_PROPERTY);
118                if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) {
119                        return new LogFile(file, path);
120                }
121                return null;
122        }
123
124}