ログ出力先に全レベルのログを出力して、設定ファイルが期待通りかを確認するツール
Revision | 1286f0649eebaaa18cc3e38d93d856f92f2c547b (tree) |
---|---|
Zeit | 2022-01-23 10:58:45 |
Autor | kemono7h |
Commiter | kemono7h |
log4j対応(途中)
@@ -4,9 +4,9 @@ | ||
4 | 4 | } |
5 | 5 | |
6 | 6 | group = 'nanah' |
7 | -version = '0.0.1-SNAPSHOT' | |
7 | +version = '1.0.0' | |
8 | 8 | sourceCompatibility = '1.8' |
9 | -mainClassName = 'jp.nanah.logconfchk.LogConfCheck' | |
9 | +mainClassName = 'jp.nanah.logconfchk.LogConfigTester' | |
10 | 10 | |
11 | 11 | configurations { |
12 | 12 | compileOnly { |
@@ -22,9 +22,20 @@ | ||
22 | 22 | implementation 'org.slf4j:slf4j-api:1.7.33' |
23 | 23 | implementation 'ch.qos.logback:logback-core:1.2.10' |
24 | 24 | implementation 'ch.qos.logback:logback-classic:1.2.10' |
25 | - implementation 'org.apache.logging.log4j:log4j-core:2.17.1' | |
25 | + implementation 'log4j:log4j:1.2.17' | |
26 | 26 | } |
27 | 27 | |
28 | 28 | test { |
29 | 29 | //useJUnitPlatform() |
30 | 30 | } |
31 | + | |
32 | +jar { | |
33 | + manifest { | |
34 | + attributes('Main-Class': 'jp.nanah.logconfchk.LogConfigTester') | |
35 | + baseName "LogConfigTester" | |
36 | + } | |
37 | + from configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } | |
38 | + exclude("META-INF/**/*") | |
39 | +} | |
40 | + | |
41 | +//defalutTasks: clean, jar | |
\ No newline at end of file |
@@ -0,0 +1,13 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<!DOCTYPE logback> | |
3 | +<configuration> | |
4 | + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | |
5 | + <encoder> | |
6 | + <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-5level %logger{36} - %m%n</pattern> | |
7 | + </encoder> | |
8 | + </appender> | |
9 | + | |
10 | + <root level="DEBUG"> | |
11 | + <appender-ref ref="STDOUT" /> | |
12 | + </root> | |
13 | +</configuration> | |
\ No newline at end of file |
@@ -42,6 +42,7 @@ | ||
42 | 42 | loggerList.add(new Log4jLogger()); |
43 | 43 | |
44 | 44 | for (int i=0; i<count; i++) { |
45 | + logger.info("*** {} 回目 ***", i); | |
45 | 46 | if (i > 0) { |
46 | 47 | try { |
47 | 48 | Thread.sleep(interval * 1000); |
@@ -61,8 +62,14 @@ | ||
61 | 62 | |
62 | 63 | for (BaseLogger bl : loggerList) { |
63 | 64 | if (bl.isTarget(doc)) { |
64 | - bl.setConfigure(f); | |
65 | - bl.logging(f, doc, i); | |
65 | + try { | |
66 | + bl.setConfigure(f); | |
67 | + bl.logging(f, doc, i); | |
68 | + } catch (Throwable th) { | |
69 | + continue; | |
70 | + } finally { | |
71 | + BaseLogger.setConfigureForLogback(null); | |
72 | + } | |
66 | 73 | } |
67 | 74 | } |
68 | 75 |
@@ -5,6 +5,7 @@ | ||
5 | 5 | import java.util.ArrayList; |
6 | 6 | import java.util.List; |
7 | 7 | |
8 | +import org.apache.log4j.xml.DOMConfigurator; | |
8 | 9 | import org.slf4j.Logger; |
9 | 10 | import org.slf4j.LoggerFactory; |
10 | 11 | import org.w3c.dom.Document; |
@@ -13,6 +14,7 @@ | ||
13 | 14 | |
14 | 15 | import ch.qos.logback.classic.LoggerContext; |
15 | 16 | import ch.qos.logback.classic.joran.JoranConfigurator; |
17 | +import ch.qos.logback.core.joran.spi.JoranException; | |
16 | 18 | |
17 | 19 | public class BaseLogger { |
18 | 20 |
@@ -46,25 +48,25 @@ | ||
46 | 48 | } |
47 | 49 | |
48 | 50 | |
49 | - public static void setConfigureForLogback(File file) { | |
50 | - try { | |
51 | - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | |
52 | - context.reset(); | |
53 | - JoranConfigurator configurator = new JoranConfigurator(); | |
54 | - configurator.setContext(context); | |
51 | + public static void setConfigureForLogback(File file) throws JoranException { | |
52 | + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | |
53 | + context.reset(); | |
54 | + JoranConfigurator configurator = new JoranConfigurator(); | |
55 | + configurator.setContext(context); | |
55 | 56 | |
56 | - if (file != null) { | |
57 | - URL configUrl = new URL("file:" + file.getAbsolutePath()); | |
58 | - logger.info("logconffile: {}", "file:" + file.getAbsolutePath() ); | |
59 | - configurator.doConfigure(configUrl); | |
60 | - } | |
57 | + if (file != null) { | |
58 | +// URL configUrl = new URL("file:" + file.getAbsolutePath()); | |
59 | +// logger.info("logconffile: {}", "file:" + file.getAbsolutePath() ); | |
60 | +// configurator.doConfigure(configUrl); | |
61 | + configurator.doConfigure(file.getAbsolutePath()); | |
62 | + } else { | |
63 | + URL url = BaseLogger.class.getResource("/logback.xml"); | |
64 | + configurator.doConfigure(url); | |
65 | + } | |
61 | 66 | |
62 | - } catch (Throwable th) { | |
63 | - th.printStackTrace(); | |
64 | - } | |
65 | 67 | } |
66 | 68 | |
67 | 69 | public static void setConfigureForLog4j(File file) { |
68 | - //comming soon | |
70 | + DOMConfigurator.configure(file.getAbsolutePath()); | |
69 | 71 | } |
70 | 72 | } |
@@ -1,7 +1,9 @@ | ||
1 | 1 | package jp.nanah.logconfchk.output; |
2 | 2 | |
3 | 3 | import java.io.File; |
4 | +import java.util.List; | |
4 | 5 | |
6 | +import org.apache.log4j.Logger; | |
5 | 7 | import org.w3c.dom.Document; |
6 | 8 | |
7 | 9 | public class Log4jLogger extends BaseLogger{ |
@@ -15,8 +17,25 @@ | ||
15 | 17 | setConfigureForLog4j(file); |
16 | 18 | } |
17 | 19 | |
18 | - public void logging(File file, Document doc) { | |
19 | - | |
20 | + public void logging(File file, Document doc, int count) { | |
21 | + Exception warnEx = new RuntimeException("警告時の例外情報です。"); | |
22 | + Exception errorEx = new RuntimeException("異常時の例外情報です。"); | |
23 | + List<String> names = getLoggerNames(doc, "logger"); | |
24 | + String configPath = "[" + file.getName() + "]"; | |
25 | + | |
26 | + for (String name : names) { | |
27 | + if (name == null) { | |
28 | + name = this.getClass().getCanonicalName(); | |
29 | + } | |
30 | + Logger log = Logger.getLogger(name); | |
31 | + // ログメッセージを出力します [logback.xml][DEBUG][0][org.commons.lang3] | |
32 | + log.trace("ログメッセージを出力します [" + configPath + "][TRACE][" + count + "][" + name + "]"); | |
33 | + log.debug("ログメッセージを出力します [" + configPath + "][DEBUG][" + count + "][" + name + "]"); | |
34 | + log.info ("ログメッセージを出力します [" + configPath + "][INFO ][" + count + "][" + name + "]"); | |
35 | + log.warn ("ログメッセージを出力します [" + configPath + "][WARN ][" + count + "][" + name + "]", warnEx); | |
36 | + log.error("ログメッセージを出力します [" + configPath + "][ERROR][" + count + "][" + name + "]", errorEx); | |
37 | + log.fatal("ログメッセージを出力します [" + configPath + "][ERROR][" + count + "][" + name + "]", errorEx); | |
38 | + } | |
20 | 39 | } |
21 | 40 | |
22 | 41 | } |
@@ -16,8 +16,12 @@ | ||
16 | 16 | return b; |
17 | 17 | } |
18 | 18 | |
19 | - public void setConfigure(File file) { | |
20 | - setConfigureForLogback(file); | |
19 | + public void setConfigure(File file){ | |
20 | + try { | |
21 | + setConfigureForLogback(file); | |
22 | + } catch (Exception ex) { | |
23 | + throw new RuntimeException(ex); | |
24 | + } | |
21 | 25 | } |
22 | 26 | |
23 | 27 | public void logging(File file, Document doc, int count) { |