001 package toolbus.logging;
002
003 import java.util.HashSet;
004 import java.util.Set;
005
006 //TODO make configurable.
007
008 /**
009 * Factory for creating a logger. Follows singleton design pattern.
010 *
011 * @author Arnold Lankamp
012 */
013 public class LoggerFactory{
014 private final static Set<String> registeredTypes = new HashSet<String>();
015 private final static Set<String> registeredNames = new HashSet<String>();
016 private static int nrOfRegisteredNames = 0;
017 private static String filter = "";
018
019 /*
020 * All configuration is now done here in line in the code;
021 * This should be moved to the property file and/or commandline.
022 */
023 static{
024 // Register all the log types that should be logged.
025 registerType(IToolBusLoggerConstants.TOOL);
026 registerType(IToolBusLoggerConstants.TOOLINSTANCE);
027 registerType(IToolBusLoggerConstants.COMMUNICATION);
028 //registerType(IToolBusLoggerConstants.PARSING);
029 //registerType(IToolBusLoggerConstants.MESSAGES);
030 //registerType(IToolBusLoggerConstants.NOTES);
031 //registerType(IToolBusLoggerConstants.CALLS);
032 //registerType(IToolBusLoggerConstants.EXECUTE);
033 //registerType(IToolBusLoggerConstants.TOOLCOM);
034
035 //registerName("EditSyntax");
036 //filter = "EditSyntax";
037 }
038
039 /**
040 * Default constructor. Private to prevent instantiation.
041 */
042 private LoggerFactory(){
043 super();
044 }
045
046 /**
047 * Registers the given string as a log type identifier. All registered log types will be logged.
048 * All messages with log types that aren't registered will be ignored.
049 *
050 * @param type
051 * The log type identifier.
052 */
053 public static void registerType(String type){
054 registeredTypes.add(type);
055 }
056
057 /**
058 * Deregisters the given string as log type identifier, so the messages that are associated with
059 * it will no longer be logged.
060 *
061 * @param type
062 * The log type identifier.
063 */
064 public static void deregisterType(String type){
065 registeredTypes.remove(type);
066 }
067
068 /**
069 * Registers the given string as a name. Only messages containing name will be logged.
070 *
071 * @param name
072 * The name of interest.
073 */
074 public static void registerName(String name){
075 registeredNames.add(name);
076 nrOfRegisteredNames++;
077 }
078
079 /**
080 * Deregisters the given string as a name, so the messages that are associated with
081 * it will no longer be logged.
082 *
083 * @param name
084 * The name of interest.
085 */
086 public static void deregisterName(String name){
087 registeredNames.remove(name);
088 nrOfRegisteredNames--;
089 }
090
091 /**
092 * Convenience method for debugging.
093 * @param processName TODO
094 * @param message
095 * The message that needs to be logged.
096 * @param type
097 * The type of the message.
098 */
099 public static void log(String processName, String message, String type){
100 if((nrOfRegisteredNames == 0 || registeredNames.contains(processName)) &&
101 registeredTypes.contains(type)){
102 String msg = "[" + processName + "]: " + message;
103 //if(msg.contains(filter)){
104 //System.err.println("contains: " + filter + " in " + msg);
105 getLogger().log(msg, ILogger.DEBUG);
106 //}
107 }
108 }
109
110 /**
111 * Convenience method for logging.
112 *
113 * @param message
114 * The message that needs to be logged.
115 * @param loglevel
116 * The log level of the message.
117 */
118 public static void log(String message, int loglevel, String type){
119 if(!registeredTypes.contains(type)) return;
120 if(message.contains(filter)){
121 getLogger().log(message, loglevel);
122 }
123 }
124
125 /**
126 * Convenience method for logging.
127 *
128 * @param message
129 * The message that needs to be logged.
130 * @param throwable
131 * The stacktrace associated with the message.
132 * @param loglevel
133 * The log level of the message.
134 */
135 public static void log(String message, Throwable throwable, int loglevel, String type){
136 if(!registeredTypes.contains(type)) return;
137 if(message.contains(filter)){
138 getLogger().log(message, throwable, loglevel);
139 }
140 }
141
142 /**
143 * Returns a reference to the instantiated logger.
144 *
145 * @return A reference to the instantiated logger.
146 */
147 public static ILogger getLogger(){
148 return LoggerKeeper.logger;
149 }
150
151 /**
152 * Instance keeper; for ensuring thread-safe, lazy intialization of the logger.
153 *
154 * @author Arnold Lankamp
155 */
156 private static class LoggerKeeper{
157 public final static ILogger logger;
158
159 static{
160 // TODO Increase the log level to warning later.
161 // TODO make the logger type configurable.
162 logger = new CommandLineLogger(ILogger.WARNING);
163 //logger = new FileLogger(ILogger.DEBUG);
164 }
165 }
166 }