annotate src/org/tn5250j/tools/logging/TN5250jLogFactory.java @ 5:cbdff98c45ea tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 12:38:09 -0700
parents
children 5949eb469a79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * @(#)TN5250jLogFactory.java
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * @author Kenneth J. Pouncey
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * Copyright: Copyright (c) 2001, 2002, 2003
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 * the Free Software Foundation; either version 2, or (at your option)
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 * any later version.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * GNU General Public License for more details.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 * along with this software; see the file COPYING. If not, write to
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 * Boston, MA 02111-1307 USA
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 package org.tn5250j.tools.logging;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 import java.util.*;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 import org.tn5250j.tools.logging.TN5250jLogger;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 import org.tn5250j.interfaces.ConfigureFactory;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 * An interface defining objects that can create Configure
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 * instances.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 * The model for the HashMap implementation of loggers came from the POI project
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 * thanks to Nicola Ken Barozzi (nicolaken at apache.org) for the reference.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 public final class TN5250jLogFactory {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 // map of TN5250jLogger instances, with classes as keys
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 private static Map<String, TN5250jLogger> _loggers = new HashMap<String, TN5250jLogger>();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 private static boolean log4j;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 private static String customLogger;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 private static int level = TN5250jLogger.INFO;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 * Here we try to do a little more work up front.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 static {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 Properties props =
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 ConfigureFactory.getInstance().getProperties(
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 ConfigureFactory.SESSIONS);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 level = Integer.parseInt(props.getProperty("emul.logLevel",
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 Integer.toString(TN5250jLogger.INFO)));
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 String customLogger = System.getProperty(TN5250jLogFactory.class.getName());
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 if (customLogger == null) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 Class.forName("org.apache.log4j.Logger");
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 log4j = true;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 catch (Exception ignore) { ; }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 catch (Exception ignore) { ; }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 * Set package access only so we have to use getLogger() to return a logger object.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 TN5250jLogFactory() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 * @return An instance of the TN5250jLogger.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 public static TN5250jLogger getLogger (Class<?> clazz) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 return getLogger(clazz.getName());
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 * @return An instance of the TN5250jLogger.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 public static TN5250jLogger getLogger (String clazzName) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 TN5250jLogger logger = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 if (_loggers.containsKey(clazzName)) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 logger = _loggers.get(clazzName);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 } else {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 if (customLogger != null) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 Class<?> classObject = Class.forName(customLogger);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 Object object = classObject.newInstance();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 if (object instanceof TN5250jLogger) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 logger = (TN5250jLogger) object;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 catch (Exception ex) { ; }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 } else {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 if (log4j) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 logger = new Log4jLogger();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 } else {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 // take the default logger.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 logger = new ConsoleLogger();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115 logger.initialize(clazzName);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 logger.setLevel(level);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 _loggers.put(clazzName, logger);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121 return logger;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 public static boolean isLog4j() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 return log4j;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 public static void setLogLevels(int newLevel) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 if (level != newLevel) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 level = newLevel;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 TN5250jLogger logger = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 Set<String> loggerSet = _loggers.keySet();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 Iterator<String> loggerIterator = loggerSet.iterator();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 while (loggerIterator.hasNext()) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 logger = _loggers.get(loggerIterator.next());
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 logger.setLevel(newLevel);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 }