forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogConfigurer.java
More file actions
109 lines (102 loc) · 3.9 KB
/
LogConfigurer.java
File metadata and controls
109 lines (102 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Utility class that initializes logback or log4j2 logging implementation.
*
* Initializes the <code>logback.configurationFile</code> system property when a
* <code>logback[.env].xml</code> file is found at <code>user.dir/conf</code> directory or
* <code>user.dir</code>.
*
* Initializes the <code>log4j.configurationFile</code> system property when a
* <code>log4j[.env].[ext]</code> file is found at <code>user.dir/conf</code> directory or
* <code>user.dir</code>. Extension can be one of: <code>.xml</code>, <code>.properties</code>,
* <code>.yaml</code> or <code>.json</code>.
*
* NOTE: This class must be call it before instantiating a logger instance. Otherwise, this setup
* is completely ignored.
*
* @since 2.0.0
* @author edgar
*/
public final class LogConfigurer {
private LogConfigurer() {
}
/**
* Initializes the <code>logback.configurationFile</code> system property when a
* <code>logback[.env].xml</code> file is found at <code>user.dir/conf</code> directory or
* <code>user.dir</code>.
*
* Initializes the <code>log4j.configurationFile</code> system property when a
* <code>log4j[.env].[ext]</code> file is found at <code>user.dir/conf</code> directory or
* <code>user.dir</code>. Extension can be one of: <code>.xml</code>, <code>.properties</code>,
* <code>.yaml</code> or <code>.json</code>.
*
* @param names Actives environment names. Useful for choosing an environment specific logging
* configuration file.
*/
public static void configure(@Nonnull List<String> names) {
String[] keys = {"logback.configurationFile", "log4j.configurationFile"};
for (String key : keys) {
String file = property(key);
if (file != null) {
// found, reset and exit
System.setProperty(key, file);
return;
}
}
Path userdir = Paths.get(System.getProperty("user.dir"));
Map<String, List<Path>> options = new LinkedHashMap<>();
options.put("logback.configurationFile", logbackFiles(userdir, names));
options.put("log4j.configurationFile", log4jFiles(userdir, names));
for (Map.Entry<String, List<Path>> entry : options.entrySet()) {
Optional<Path> logfile = entry.getValue().stream().filter(Files::exists)
.findFirst()
.map(Path::toAbsolutePath);
if (logfile.isPresent()) {
System.setProperty(entry.getKey(), logfile.get().toString());
break;
}
}
}
private static List<Path> logbackFiles(Path basedir, List<String> env) {
return logFile(basedir, env, "logback", ".xml");
}
private static List<Path> logFile(Path basedir, List<String> names, String name, String ext) {
Path confdir = basedir.resolve("conf");
List<Path> result = new ArrayList<>();
for (String env : names) {
String envlogfile = name + "." + env + ext;
result.add(confdir.resolve(envlogfile));
result.add(basedir.resolve(envlogfile));
}
String logfile = name + ext;
result.add(confdir.resolve(logfile));
result.add(basedir.resolve(logfile));
return result;
}
private static List<Path> log4jFiles(Path basedir, List<String> names) {
List<Path> result = new ArrayList<>();
String[] extensions = {".properties", ".xml", ".yaml", ".yml", ".json"};
for (String extension : extensions) {
result.addAll(logFile(basedir, names, "log4j", extension));
result.addAll(logFile(basedir, names, "log4j2", extension));
}
return result;
}
private static String property(String name) {
return System.getProperty(name, System.getenv(name));
}
}