• R/O
  • SSH

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

ログ出力先に全レベルのログを出力して、設定ファイルが期待通りかを確認するツール


Commit MetaInfo

Revision9ba58a8759820e096b31360627f67ca7782ea859 (tree)
Time2022-05-28 20:28:58
Authorkemono7h
Commiterkemono7h

Log Message

いろいろ問題があって現在改修中。構造を変える前に念のため現状を保存する。

Change Summary

Incremental Difference

diff -r 2df57ad74576 -r 9ba58a875982 .classpath
--- a/.classpath Tue Jan 25 08:40:42 2022 +0900
+++ b/.classpath Sat May 28 20:28:58 2022 +0900
@@ -14,9 +14,9 @@
1414 </classpathentry>
1515 <classpathentry kind="src" output="bin/test" path="src/test/java">
1616 <attributes>
17+ <attribute name="test" value="true"/>
1718 <attribute name="gradle_scope" value="test"/>
1819 <attribute name="gradle_used_by_scope" value="test"/>
19- <attribute name="test" value="true"/>
2020 </attributes>
2121 </classpathentry>
2222 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
diff -r 2df57ad74576 -r 9ba58a875982 build.gradle
--- a/build.gradle Tue Jan 25 08:40:42 2022 +0900
+++ b/build.gradle Sat May 28 20:28:58 2022 +0900
@@ -23,10 +23,11 @@
2323 }
2424
2525 dependencies {
26- implementation 'org.slf4j:slf4j-api:1.7.33'
27- implementation 'ch.qos.logback:logback-core:1.2.10'
28- implementation 'ch.qos.logback:logback-classic:1.2.10'
26+ implementation 'org.slf4j:slf4j-api:1.7.36'
27+ implementation 'ch.qos.logback:logback-core:1.2.11'
28+ implementation 'ch.qos.logback:logback-classic:1.2.11'
2929 implementation 'log4j:log4j:1.2.17'
30+ testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.3'
3031 }
3132
3233 test {
diff -r 2df57ad74576 -r 9ba58a875982 build/libs/LogConfigTester-1.0.0.jar
Binary file build/libs/LogConfigTester-1.0.0.jar has changed
diff -r 2df57ad74576 -r 9ba58a875982 build/resources/main/logback.xml
--- a/build/resources/main/logback.xml Tue Jan 25 08:40:42 2022 +0900
+++ b/build/resources/main/logback.xml Sat May 28 20:28:58 2022 +0900
@@ -2,8 +2,9 @@
22 <!DOCTYPE logback>
33 <configuration>
44 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+ <target>System.out</target>
56 <encoder>
6- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-5level %logger{36} - %m%n</pattern>
7+ <pattern>★%d{yyyy/MM/dd HH:mm:ss.SSS} %-5level %logger{36} - %m%n</pattern>
78 </encoder>
89 </appender>
910
diff -r 2df57ad74576 -r 9ba58a875982 src/main/java/jp/nanah/logconfchk/LogConfigTester.java
--- a/src/main/java/jp/nanah/logconfchk/LogConfigTester.java Tue Jan 25 08:40:42 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/LogConfigTester.java Sat May 28 20:28:58 2022 +0900
@@ -18,16 +18,16 @@
1818 public class LogConfigTester {
1919
2020 private static final Logger logger = LoggerFactory.getLogger(LogConfigTester.class);
21-
21+
2222 /**
2323 * 指定のファイルまたはフォルダに含まれるloggerの設定ファイルを1つずつ読み込んで
2424 * その出力先に全レベルのログを出力する。
25- *
25+ *
2626 * @param path ロガの設定ファイルまたはフォルダ。フォルダはその階層のみ。
2727 * @param count ログ出力する件数。一定サイズでローテーションする場合に利用。
2828 * @param interval ログ1件を出した後に待つ秒数。指定時間になったらローテーションする場合、件数と組み合わせて使用。
2929 */
30- public void logoutAll(String path, int count, int interval){
30+ public void logoutAll(String path, int count, double interval){
3131 File[] files = null;
3232
3333 File file = new File(path);
@@ -36,50 +36,62 @@
3636 } else {
3737 files = new File[]{file};
3838 }
39-
39+
4040 List<BaseLogger> loggerList = new ArrayList<BaseLogger>();
41- loggerList.add(new LogbackLogger());
42- loggerList.add(new Log4jLogger());
43-
41+ loggerList.add(new LogbackLogger());
42+ loggerList.add(new Log4jLogger());
43+
44+ File prevFile = null;
45+
46+ // 1回ごとにログ出力設定ファイルを設定する方法だと
47+ // ch.qos.logback.core.rolling.SizeBasedTriggeringPolicyのローテートが機能しない。
48+ // なぜならSizeBasedTriggeringPolicyが毎回サイズ超えを数回に1回判断しているため
49+ // ログ出力設定を毎回設定すると毎回1回目となり、ローテートが走らなくなる。
50+ // そのため、ログファイル1つに対しN下位ループする、という方式に変更する。
51+
4452 for (int i=0; i<count; i++) {
45- logger.info("*** {} 回目 ***", i);
53+ logger.info("*** {} 回目 ***", i+1);
4654 if (i > 0) {
4755 try {
48- Thread.sleep(interval * 1000);
56+ Thread.sleep((int)(interval * 1000));
4957 } catch (InterruptedException e) {
5058 //ignore
5159 }
5260 }
53-
61+
5462 for (File f : files) {
5563 try {
5664 //ループ中に書き換えた設定ファイルが反映されるように毎回読み込む
5765 Document doc = loadXmlFile(f);
58-
5966 if (doc == null) {
6067 continue;
6168 }
62-
69+
6370 for (BaseLogger bl : loggerList) {
6471 if (bl.isTarget(doc)) {
72+ logger.info("【全ログ出力】{}:{}", bl.getDocType(), f.getAbsolutePath());
6573 try {
66- bl.setConfigure(f);
74+ if (prevFile != f) {
75+ System.out.println("ロガー設定 [" + f.getAbsolutePath() + "]");
76+ bl.setConfigure(f);
77+ }
78+ prevFile = f;
6779 bl.logging(f, doc, i);
6880 } catch (Throwable th) {
6981 continue;
7082 } finally {
71- BaseLogger.setConfigureForLogback(null);
83+ //BaseLogger.setConfigureForLogback(null);
7284 }
7385 }
7486 }
75-
87+
7688 } catch (Throwable th) {
7789 logger.warn("処理異常発生", th);
7890 }
7991 }
8092 }
8193 }
82-
94+
8395 /**
8496 * ログ出力設定ファイル(XML)を読み込んで返す。
8597 * @param file
@@ -92,7 +104,7 @@
92104 if (file.getName().endsWith(".xml") == false) {
93105 return null;
94106 }
95-
107+
96108 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
97109 try {
98110 dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //DTDを無視する
@@ -104,10 +116,10 @@
104116 logger.error("設定ファイル読み込み異常 (path=" + file.getAbsolutePath() + ")");
105117 th.printStackTrace();
106118 }
107-
119+
108120 return null;
109121 }
110-
122+
111123 /**
112124 * 起動引数
113125 * [0] ログ設定ファイルのあるフォルダまたはファイル
@@ -126,10 +138,10 @@
126138 logger.info("=== START ===");
127139 String path = args[0];
128140 int count = (args.length > 1) ? Integer.parseInt(args[1]) : 1;
129- int interval = (args.length > 2) ? Integer.parseInt(args[2]) : 0;
130-
141+ double interval = (args.length > 2) ? Double.parseDouble(args[2]) : 0;
142+
131143 LogConfigTester app = new LogConfigTester();
132- app.logoutAll(path, count, interval);
144+ app.logoutAll(path, count, interval);
133145 }
134146
135147 }
diff -r 2df57ad74576 -r 9ba58a875982 src/main/java/jp/nanah/logconfchk/output/BaseLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/BaseLogger.java Tue Jan 25 08:40:42 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/BaseLogger.java Sat May 28 20:28:58 2022 +0900
@@ -20,19 +20,52 @@
2020
2121 private static final Logger logger = LoggerFactory.getLogger(BaseLogger.class);
2222
23+ protected String docType;
24+
2325 public boolean isTarget(Document doc) {
2426 return false;
2527 }
26-
28+
29+ public void outputLine(String msg) {
30+ }
31+
2732 public void setConfigure(File file) {
2833 }
29-
34+
3035 public void logging(File file, Document doc, int count) {
3136 }
32-
37+
38+ public String getDocType() {
39+ return docType;
40+ }
41+
42+ /**
43+ * 全Documentから%X{n}または%mdc{n}の場所を探して
44+ * nに相当する文字列を返す。
45+ */
46+ public List<String[]> getMdcList(Document doc) {
47+ List<String[]> mdcList = new ArrayList<String[]>();
48+ // %X{key}
49+ // %10X{key}
50+ // %-10X{key}
51+
52+ // %mdc{key}
53+ // %10mdc{key}
54+ // %-10mdc{key}
55+
56+ // Pattern p = Pattern.compile("%[-]*\\d*(X|mdc){.+}");
57+ // String str = doc.get //"%-10X{key}";
58+ // Matcher m = p.matcher(str);
59+ // if(m.find()){
60+ // System.out.println(str + ":一致! [" + m.group() + "]");
61+ // }
62+
63+ return mdcList;
64+ }
65+
3366 //以下はユーティリティメソッド的に使う。
34-
35-
67+
68+
3669 protected List<String> getLoggerNames(Document doc, String logNameTag){
3770 List<String> loggerNames = new ArrayList<String>();
3871
@@ -43,28 +76,29 @@
4376 Element loggereElem = (Element) loggerNodes.item(i);
4477 loggerNames.add( loggereElem.getAttribute("name") );
4578 }
46-
79+
4780 return loggerNames;
4881 }
4982
50-
83+
5184 public static void setConfigureForLogback(File file) throws JoranException {
5285 LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
53- context.reset();
54- JoranConfigurator configurator = new JoranConfigurator();
55- configurator.setContext(context);
86+ context.reset();
87+ JoranConfigurator configurator = new JoranConfigurator();
88+ configurator.setContext(context);
5689
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- }
66-
67- }
90+ if (file != null) {
91+ // URL configUrl = new URL("file:" + file.getAbsolutePath());
92+ // logger.info("logconffile: {}", "file:" + file.getAbsolutePath() );
93+ // configurator.doConfigure(configUrl);
94+ configurator.doConfigure(file.getAbsolutePath());
95+ System.out.println("ロガー設定 : "+ file.getAbsolutePath());
96+ } else {
97+ URL url = BaseLogger.class.getResource("/logback.xml");
98+ configurator.doConfigure(url);
99+ }
100+
101+ }
68102
69103 public static void setConfigureForLog4j(File file) {
70104 DOMConfigurator.configure(file.getAbsolutePath());
diff -r 2df57ad74576 -r 9ba58a875982 src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java Tue Jan 25 08:40:42 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java Sat May 28 20:28:58 2022 +0900
@@ -8,21 +8,32 @@
88
99 public class Log4jLogger extends BaseLogger{
1010
11+ @Override
1112 public boolean isTarget(Document doc) {
1213 boolean b = doc.getDoctype().getName().contains("log4j");
1314 return b;
1415 }
15-
16+
17+ public void outputLine() {
18+ }
19+
20+ @Override
1621 public void setConfigure(File file) {
1722 setConfigureForLog4j(file);
1823 }
19-
24+
25+ @Override
26+ public String getDocType() {
27+ return "log4j";
28+ }
29+
30+ @Override
2031 public void logging(File file, Document doc, int count) {
2132 Exception warnEx = new RuntimeException("警告時の例外情報です。");
2233 Exception errorEx = new RuntimeException("異常時の例外情報です。");
2334 List<String> names = getLoggerNames(doc, "logger");
2435 String configPath = "[" + file.getName() + "]";
25-
36+
2637 for (String name : names) {
2738 if (name == null) {
2839 name = this.getClass().getCanonicalName();
@@ -37,5 +48,5 @@
3748 log.fatal("ログメッセージを出力します [" + configPath + "][ERROR][" + count + "][" + name + "]", errorEx);
3849 }
3950 }
40-
51+
4152 }
diff -r 2df57ad74576 -r 9ba58a875982 src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java Tue Jan 25 08:40:42 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java Sat May 28 20:28:58 2022 +0900
@@ -7,15 +7,36 @@
77 import org.slf4j.LoggerFactory;
88 import org.w3c.dom.Document;
99
10-public class LogbackLogger extends BaseLogger{
10+import ch.qos.logback.classic.LoggerContext;
11+import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
12+import ch.qos.logback.core.ConsoleAppender;
13+
14+public class LogbackLogger extends BaseLogger {
1115
1216 private static final Logger logger = LoggerFactory.getLogger(LogbackLogger.class);
1317
18+ @Override
1419 public boolean isTarget(Document doc) {
15- boolean b = doc.getDoctype().getName().contains("logback");
20+ boolean b = doc.getDoctype().getName().contains(getDocType());
1621 return b;
1722 }
1823
24+ @Override
25+ public void outputLine(String msg) {
26+ LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
27+
28+ //Pattern - MessageOnly
29+ PatternLayoutEncoder encoder = new PatternLayoutEncoder();
30+ encoder.setContext(loggerContext);
31+ encoder.setPattern("%%m%n");
32+ encoder.start();
33+
34+ //sg
35+ ConsoleAppender appender = new ConsoleAppender();
36+ appender.setContext(loggerContext);
37+ }
38+
39+ @Override
1940 public void setConfigure(File file){
2041 try {
2142 setConfigureForLogback(file);
@@ -23,24 +44,31 @@
2344 throw new RuntimeException(ex);
2445 }
2546 }
26-
47+
48+ @Override
49+ public String getDocType() {
50+ return "logback";
51+ }
52+
53+ @Override
2754 public void logging(File file, Document doc, int count) {
2855 Exception warnEx = new RuntimeException("警告時の例外情報です。");
2956 Exception errorEx = new RuntimeException("異常時の例外情報です。");
3057 List<String> names = getLoggerNames(doc, "logger");
3158 String configPath = "[" + file.getName() + "]";
32-
59+
3360 for (String name : names) {
3461 if (name == null) {
3562 name = this.getClass().getCanonicalName();
3663 }
64+ System.out.println("カテゴリ名[" + name + "]");
3765 Logger log = LoggerFactory.getLogger(name);
3866 // ログメッセージを出力します [logback.xml][DEBUG][0][org.commons.lang3]
39- log.trace("ログメッセージを出力します [{}][TRACE][{}][{}]", configPath, count, name);
40- log.debug("ログメッセージを出力します [{}][DEBUG][{}][{}]", configPath, count, name);
41- log.info ("ログメッセージを出力します [{}][INFO ][{}][{}]", configPath, count, name);
42- log.warn ("ログメッセージを出力します [{}][WARN ][{}][{}]", configPath, count, name, warnEx);
43- log.error("ログメッセージを出力します [{}][ERROR][{}][{}]", configPath, count, name, errorEx);
67+ log.trace("ログメッセージを出力します [{}][{}][TRACE][{}]", count, name, configPath);
68+ log.debug("ログメッセージを出力します [{}][{}][DEBUG][{}]", count, name, configPath);
69+ log.info ("ログメッセージを出力します [{}][{}][INFO ][{}]", count, name, configPath);
70+ log.warn ("ログメッセージを出力します [{}][{}][WARN ][{}]", count, name, configPath, warnEx);
71+ log.error("ログメッセージを出力します [{}][{}][ERROR][{}]", count, name, configPath, errorEx);
4472 }
4573 }
4674
diff -r 2df57ad74576 -r 9ba58a875982 src/main/resources/logback.xml
--- a/src/main/resources/logback.xml Tue Jan 25 08:40:42 2022 +0900
+++ b/src/main/resources/logback.xml Sat May 28 20:28:58 2022 +0900
@@ -2,8 +2,9 @@
22 <!DOCTYPE logback>
33 <configuration>
44 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+ <target>System.out</target>
56 <encoder>
6- <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-5level %logger{36} - %m%n</pattern>
7+ <pattern>★%d{yyyy/MM/dd HH:mm:ss.SSS} %-5level %logger{36} - %m%n</pattern>
78 </encoder>
89 </appender>
910