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