1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
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 | package net.sf.jomic.common; |
17 | |
18 | import java.util.Iterator; |
19 | import java.util.Map; |
20 | import java.util.Properties; |
21 | import java.util.StringTokenizer; |
22 | |
23 | import org.apache.log4j.ConsoleAppender; |
24 | import org.apache.log4j.Level; |
25 | import org.apache.log4j.Logger; |
26 | import org.apache.log4j.PatternLayout; |
27 | |
28 | /** |
29 | * Configurator for logging. This is the simplified version of log4j's BasicConfigurator that works |
30 | * with log4j-core. |
31 | * |
32 | * @author Thomas Aglassinger |
33 | */ |
34 | // TODO: get rid of JomicConfigurator, it was only useful for log4j-core. |
35 | public final class JomicConfigurator |
36 | { |
37 | private static final String LOG4J_APPENDER_STDOUT = "log4j.appender.stdout"; |
38 | private static final String LOGGER_PREFIX = "log4j.logger."; |
39 | |
40 | private static final String ORG_APACHE_LOG4J_CONSOLEAPPENDER = "org.apache.log4j.ConsoleAppender"; |
41 | |
42 | private JomicConfigurator() { |
43 | super(); |
44 | } |
45 | |
46 | public static void setLevel(Properties properties) { |
47 | assert properties != null; |
48 | Iterator rider = properties.entrySet().iterator(); |
49 | Logger root = Logger.getRootLogger(); |
50 | |
51 | while (rider.hasNext()) { |
52 | Map.Entry entry = (Map.Entry) rider.next(); |
53 | String key = (String) entry.getKey(); |
54 | String value = (String) entry.getValue(); |
55 | |
56 | if (key.equals("log4j.rootLogger")) { |
57 | StringTokenizer tokenizer = new StringTokenizer(value, ","); |
58 | |
59 | if (tokenizer.hasMoreTokens()) { |
60 | String level = tokenizer.nextToken(); |
61 | |
62 | root.setLevel(Level.toLevel(level)); |
63 | } else { |
64 | logSetLevelWarning(key, "using appender \"stdout\""); |
65 | } |
66 | } else if (key.startsWith(LOGGER_PREFIX)) { |
67 | String loggerName = key.substring(LOGGER_PREFIX.length()); |
68 | Logger logger = Logger.getLogger(loggerName); |
69 | |
70 | logger.setLevel(Level.toLevel(value)); |
71 | } else if (key.equals(LOG4J_APPENDER_STDOUT)) { |
72 | if (!value.equals(ORG_APACHE_LOG4J_CONSOLEAPPENDER)) { |
73 | logSetLevelWarning(key, "replacing \"" |
74 | + ORG_APACHE_LOG4J_CONSOLEAPPENDER + "\" by \"" |
75 | + value + "\""); |
76 | } |
77 | } else { |
78 | logSetLevelWarning(key, "ignoring unknown key"); |
79 | } |
80 | } |
81 | } |
82 | |
83 | public static void configure() { |
84 | Logger root = Logger.getRootLogger(); |
85 | |
86 | root.addAppender(new ConsoleAppender(new PatternLayout("%d [%t] %-5p %c - %m%n"))); |
87 | } |
88 | |
89 | private static void logSetLevelWarning(String key, String message) { |
90 | Logger root = Logger.getRootLogger(); |
91 | |
92 | root.warn("logger setup key \"" + key + "\": " + message); |
93 | } |
94 | } |