author | František Kučera <franta-hg@frantovo.cz> |
Thu, 24 Oct 2019 21:43:08 +0200 | |
branch | v_0 |
changeset 250 | aae5009bd0af |
parent 238 | 4a1864c3e867 |
permissions | -rw-r--r-- |
55 | 1 |
/** |
2 |
* SQL-DK |
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
250
aae5009bd0af
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
238
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
55 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
package info.globalcode.sql.dk.logging; |
|
18 |
||
19 |
import info.globalcode.sql.dk.Constants; |
|
20 |
import java.util.logging.ConsoleHandler; |
|
58
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
21 |
import java.util.logging.Handler; |
55 | 22 |
import java.util.logging.Level; |
23 |
import java.util.logging.Logger; |
|
24 |
||
25 |
/** |
|
26 |
* Configures logging subsystem. |
|
27 |
* Usage: java -Djava.util.logging.config.class=info.globalcode.sql.dk.logging.LoggerInitializer … |
|
28 |
* |
|
29 |
* @author Ing. František Kučera (frantovo.cz) |
|
30 |
*/ |
|
31 |
public class LoggerInitializer { |
|
32 |
||
58
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
33 |
private static final Logger log = Logger.getLogger(LoggerInitializer.class.getName()); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
34 |
public static final String LEVEL_PROPERTY = LoggerInitializer.class.getName() + ".level"; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
35 |
private static final Level DEFAULT_LEVEL = Level.INFO; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
36 |
|
55 | 37 |
public LoggerInitializer() { |
38 |
Logger logger = Logger.getLogger(Constants.JAVA_PACKAGE); |
|
39 |
ConsoleHandler handler = new ConsoleHandler(); |
|
40 |
ColorfulConsoleFormatter formatter = new ColorfulConsoleFormatter(); |
|
41 |
||
42 |
logger.addHandler(handler); |
|
43 |
handler.setFormatter(formatter); |
|
44 |
||
59
5f745ae795a8
logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents:
58
diff
changeset
|
45 |
setLevel(logger, handler, formatter); |
55 | 46 |
|
47 |
||
48 |
/** |
|
155 | 49 |
* TODO: optional FileHandler – detailed logs in file in ~/sql-dk/log/… |
55 | 50 |
*/ |
51 |
} |
|
58
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
52 |
|
59
5f745ae795a8
logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents:
58
diff
changeset
|
53 |
private void setLevel(Logger logger, Handler handler, ColorfulConsoleFormatter formatter) { |
58
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
54 |
boolean levelParseError = false; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
55 |
Level level; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
56 |
String cliLevel = System.getProperty(LEVEL_PROPERTY); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
57 |
if (cliLevel == null) { |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
58 |
level = DEFAULT_LEVEL; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
59 |
} else { |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
60 |
try { |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
61 |
level = Level.parse(cliLevel); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
62 |
} catch (IllegalArgumentException e) { |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
63 |
level = DEFAULT_LEVEL; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
64 |
levelParseError = true; |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
65 |
} |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
66 |
} |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
67 |
|
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
68 |
handler.setLevel(level); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
69 |
logger.setLevel(level); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
70 |
|
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
71 |
if (levelParseError) { |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
72 |
log.log(Level.WARNING, "Invalid logging level „{0}“ specified in „{1}“ → using default level „{2}“", new Object[]{cliLevel, LEVEL_PROPERTY, DEFAULT_LEVEL}); |
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
73 |
} |
59
5f745ae795a8
logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents:
58
diff
changeset
|
74 |
|
5f745ae795a8
logging: print stacktraces if level is less than INFO
František Kučera <franta-hg@frantovo.cz>
parents:
58
diff
changeset
|
75 |
formatter.setPrintStacktrace(level.intValue() < Level.INFO.intValue()); |
58
fd2ac24c6a22
logging: configurable level
František Kučera <franta-hg@frantovo.cz>
parents:
57
diff
changeset
|
76 |
} |
55 | 77 |
} |