• R/O
  • SSH

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

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

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


Commit MetaInfo

Revision1286f0649eebaaa18cc3e38d93d856f92f2c547b (tree)
Zeit2022-01-23 10:58:45
Autorkemono7h
Commiterkemono7h

Log Message

log4j対応(途中)

Ändern Zusammenfassung

Diff

diff -r 3b7dde09df50 -r 1286f0649eeb build.gradle
--- a/build.gradle Sun Jan 23 01:10:23 2022 +0900
+++ b/build.gradle Sun Jan 23 10:58:45 2022 +0900
@@ -4,9 +4,9 @@
44 }
55
66 group = 'nanah'
7-version = '0.0.1-SNAPSHOT'
7+version = '1.0.0'
88 sourceCompatibility = '1.8'
9-mainClassName = 'jp.nanah.logconfchk.LogConfCheck'
9+mainClassName = 'jp.nanah.logconfchk.LogConfigTester'
1010
1111 configurations {
1212 compileOnly {
@@ -22,9 +22,20 @@
2222 implementation 'org.slf4j:slf4j-api:1.7.33'
2323 implementation 'ch.qos.logback:logback-core:1.2.10'
2424 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'
2626 }
2727
2828 test {
2929 //useJUnitPlatform()
3030 }
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
diff -r 3b7dde09df50 -r 1286f0649eeb build/resources/main/logback.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/build/resources/main/logback.xml Sun Jan 23 10:58:45 2022 +0900
@@ -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
diff -r 3b7dde09df50 -r 1286f0649eeb src/main/java/jp/nanah/logconfchk/LogConfigTester.java
--- a/src/main/java/jp/nanah/logconfchk/LogConfigTester.java Sun Jan 23 01:10:23 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/LogConfigTester.java Sun Jan 23 10:58:45 2022 +0900
@@ -42,6 +42,7 @@
4242 loggerList.add(new Log4jLogger());
4343
4444 for (int i=0; i<count; i++) {
45+ logger.info("*** {} 回目 ***", i);
4546 if (i > 0) {
4647 try {
4748 Thread.sleep(interval * 1000);
@@ -61,8 +62,14 @@
6162
6263 for (BaseLogger bl : loggerList) {
6364 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+ }
6673 }
6774 }
6875
diff -r 3b7dde09df50 -r 1286f0649eeb src/main/java/jp/nanah/logconfchk/output/BaseLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/BaseLogger.java Sun Jan 23 01:10:23 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/BaseLogger.java Sun Jan 23 10:58:45 2022 +0900
@@ -5,6 +5,7 @@
55 import java.util.ArrayList;
66 import java.util.List;
77
8+import org.apache.log4j.xml.DOMConfigurator;
89 import org.slf4j.Logger;
910 import org.slf4j.LoggerFactory;
1011 import org.w3c.dom.Document;
@@ -13,6 +14,7 @@
1314
1415 import ch.qos.logback.classic.LoggerContext;
1516 import ch.qos.logback.classic.joran.JoranConfigurator;
17+import ch.qos.logback.core.joran.spi.JoranException;
1618
1719 public class BaseLogger {
1820
@@ -46,25 +48,25 @@
4648 }
4749
4850
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);
5556
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+ }
6166
62- } catch (Throwable th) {
63- th.printStackTrace();
64- }
6567 }
6668
6769 public static void setConfigureForLog4j(File file) {
68- //comming soon
70+ DOMConfigurator.configure(file.getAbsolutePath());
6971 }
7072 }
diff -r 3b7dde09df50 -r 1286f0649eeb src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java Sun Jan 23 01:10:23 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/Log4jLogger.java Sun Jan 23 10:58:45 2022 +0900
@@ -1,7 +1,9 @@
11 package jp.nanah.logconfchk.output;
22
33 import java.io.File;
4+import java.util.List;
45
6+import org.apache.log4j.Logger;
57 import org.w3c.dom.Document;
68
79 public class Log4jLogger extends BaseLogger{
@@ -15,8 +17,25 @@
1517 setConfigureForLog4j(file);
1618 }
1719
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+ }
2039 }
2140
2241 }
diff -r 3b7dde09df50 -r 1286f0649eeb src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java
--- a/src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java Sun Jan 23 01:10:23 2022 +0900
+++ b/src/main/java/jp/nanah/logconfchk/output/LogbackLogger.java Sun Jan 23 10:58:45 2022 +0900
@@ -16,8 +16,12 @@
1616 return b;
1717 }
1818
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+ }
2125 }
2226
2327 public void logging(File file, Document doc, int count) {