|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + * |
| 14 | + * Copyright 2014 Edgar Espina |
| 15 | + */ |
| 16 | +package io.jooby; |
| 17 | + |
| 18 | +import javax.annotation.Nonnull; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +import static java.util.Arrays.asList; |
| 29 | + |
| 30 | +public final class LogConfigurer { |
| 31 | + private LogConfigurer() { |
| 32 | + } |
| 33 | + |
| 34 | + public static void configure(@Nonnull String... names) { |
| 35 | + String[] keys = {"logback.configurationFile", "log4j.configurationFile"}; |
| 36 | + for (String key : keys) { |
| 37 | + String file = property(key); |
| 38 | + if (file != null) { |
| 39 | + // found, reset and exit |
| 40 | + System.setProperty(key, file); |
| 41 | + return; |
| 42 | + } |
| 43 | + } |
| 44 | + Path userdir = Paths.get(System.getProperty("user.dir")); |
| 45 | + Map<String, List<Path>> options = new LinkedHashMap<>(); |
| 46 | + options.put("logback.configurationFile", logbackFiles(userdir, names)); |
| 47 | + options.put("log4j.configurationFile", log4jFiles(userdir, names)); |
| 48 | + for (Map.Entry<String, List<Path>> entry : options.entrySet()) { |
| 49 | + Optional<Path> logfile = entry.getValue().stream().filter(Files::exists) |
| 50 | + .findFirst() |
| 51 | + .map(Path::toAbsolutePath); |
| 52 | + if (logfile.isPresent()) { |
| 53 | + System.setProperty(entry.getKey(), logfile.get().toString()); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static List<Path> logbackFiles(Path basedir, String[] env) { |
| 60 | + return logFile(basedir, env, "logback", ".xml"); |
| 61 | + } |
| 62 | + |
| 63 | + private static List<Path> logFile(Path basedir, String[] names, String name, String ext) { |
| 64 | + Path confdir = basedir.resolve("conf"); |
| 65 | + List<Path> result = new ArrayList<>(); |
| 66 | + for (String env : names) { |
| 67 | + String envlogfile = name + "." + env + ext; |
| 68 | + result.add(confdir.resolve(envlogfile)); |
| 69 | + result.add(basedir.resolve(envlogfile)); |
| 70 | + } |
| 71 | + String logfile = name + ext; |
| 72 | + result.add(confdir.resolve(logfile)); |
| 73 | + result.add(basedir.resolve(logfile)); |
| 74 | + return result; |
| 75 | + } |
| 76 | + |
| 77 | + private static List<Path> log4jFiles(Path basedir, String[] names) { |
| 78 | + List<Path> result = new ArrayList<>(); |
| 79 | + String[] extensions = {".properties", ".xml", ".yaml", ".yml", ".json"}; |
| 80 | + for (String extension : extensions) { |
| 81 | + result.addAll(logFile(basedir, names, "log4j", extension)); |
| 82 | + result.addAll(logFile(basedir, names, "log4j2", extension)); |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + private static String property(String name) { |
| 88 | + return System.getProperty(name, System.getenv(name)); |
| 89 | + } |
| 90 | +} |
0 commit comments