Browse Source

update readme

张泳健 5 years ago
parent
commit
29ff413406

+ 10 - 0
configuration-loader/README.md View File

@@ -98,6 +98,16 @@ redis:
98 98
 
99 99
 默认的配置文件为`application.yml/application.yaml/application.properties`
100 100
 
101
+基本用法
101 102
 ```java
102 103
 ConnectorProperties connectorProperties = ConfigLoader.parseConfig(ConnectorProperties.class);
104
+```
105
+
106
+用法:
107
+```
108
+LogSetter.resetLogger();
109
+ConnectorProperties connectorProperties = ConfigLoader.parseConfig(args, ConnectorProperties.class, profile -> {
110
+    LogSetter.setLogback("logback", profile);
111
+});
112
+
103 113
 ```

+ 17 - 0
configuration-loader/pom.xml View File

@@ -13,6 +13,23 @@
13 13
 
14 14
 
15 15
     <dependencies>
16
+        <dependency>
17
+            <groupId>org.slf4j</groupId>
18
+            <artifactId>slf4j-api</artifactId>
19
+        </dependency>
20
+        <dependency>
21
+            <groupId>org.slf4j</groupId>
22
+            <artifactId>jul-to-slf4j</artifactId>
23
+        </dependency>
24
+        <dependency>
25
+            <groupId>org.slf4j</groupId>
26
+            <artifactId>log4j-over-slf4j</artifactId>
27
+        </dependency>
28
+        <dependency>
29
+            <groupId>ch.qos.logback</groupId>
30
+            <artifactId>logback-classic</artifactId>
31
+            <scope>provided</scope>
32
+        </dependency>
16 33
 
17 34
         <!-- json -->
18 35
         <dependency>

+ 1 - 1
configuration-loader/src/main/java/me/yuxiaoyao/config/loader/ConfigLoader.java View File

@@ -23,7 +23,7 @@ public class ConfigLoader {
23 23
     /**
24 24
      * 如果想修改,可以通过反射修改
25 25
      */
26
-    private static String DEFAULT_FILE_CONFIG_FOLDER = "config";
26
+    public static String DEFAULT_FILE_CONFIG_FOLDER = "config";
27 27
     private static String DEFAULT_APPLICATION = "application";
28 28
 
29 29
     private static String START_PLACEHOLDER = "${";

+ 36 - 0
configuration-loader/src/main/java/me/yuxiaoyao/config/log/LogSetter.java View File

@@ -0,0 +1,36 @@
1
+package me.yuxiaoyao.config.log;
2
+
3
+import ch.qos.logback.classic.util.ContextInitializer;
4
+import me.yuxiaoyao.config.loader.ConfigLoader;
5
+import org.slf4j.bridge.SLF4JBridgeHandler;
6
+
7
+import java.io.File;
8
+
9
+/**
10
+ * @author Kerry on 19/10/10
11
+ */
12
+
13
+public class LogSetter {
14
+
15
+    public static void resetLogger() {
16
+        // 第三方JAR包引用了 其他日志LIB,这里统一用 slf4j 统一接管
17
+        // @see https://stackoverflow.com/questions/6020545/send-redirect-route-java-util-logging-logger-jul-to-logback-using-slf4j/20407321
18
+        SLF4JBridgeHandler.removeHandlersForRootLogger();
19
+        SLF4JBridgeHandler.install();
20
+    }
21
+
22
+    public static void setLogback(String basename, String profile) {
23
+        if (profile == null) {
24
+            return;
25
+        }
26
+        File file = new File(ConfigLoader.DEFAULT_FILE_CONFIG_FOLDER + File.separator + basename + "-" + profile + "xml");
27
+        if (file.exists()) {
28
+            System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, file.getAbsolutePath());
29
+            return;
30
+        }
31
+        file = new File(ConfigLoader.DEFAULT_FILE_CONFIG_FOLDER + File.separator + basename + "xml");
32
+        if (file.exists()) {
33
+            System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, file.getAbsolutePath());
34
+        }
35
+    }
36
+}

+ 10 - 0
pom.xml View File

@@ -37,6 +37,16 @@
37 37
                 <version>${slf4j.version}</version>
38 38
             </dependency>
39 39
             <dependency>
40
+                <groupId>org.slf4j</groupId>
41
+                <artifactId>jul-to-slf4j</artifactId>
42
+                <version>${slf4j.version}</version>
43
+            </dependency>
44
+            <dependency>
45
+                <groupId>org.slf4j</groupId>
46
+                <artifactId>log4j-over-slf4j</artifactId>
47
+                <version>${slf4j.version}</version>
48
+            </dependency>
49
+            <dependency>
40 50
                 <groupId>ch.qos.logback</groupId>
41 51
                 <artifactId>logback-classic</artifactId>
42 52
                 <version>${logback.version}</version>