1 23 24 package org.apache.slide.util.logger; 25 26 import java.util.Date ; 27 import java.util.Locale ; 28 import java.text.SimpleDateFormat ; 29 30 34 public class SimpleLogger implements Logger { 35 36 37 39 40 43 protected int loggerLevel = 0; 44 45 46 49 protected SimpleDateFormat dateFormat = 50 new SimpleDateFormat ("dd MMM yyyy HH:mm:ss", Locale.US); 51 52 53 56 protected String [] loggingLevels = { "EMERGENCY", "CRITICAL", "ERROR", 57 "", "WARNING", "", "INFO", "DEBUG" }; 58 59 60 62 63 68 public void setLoggerLevel(String channel, int loggerLevel) { 69 this.loggerLevel = loggerLevel; 70 } 71 72 73 78 public void setLoggerLevel(int loggerLevel) { 79 setLoggerLevel(DEFAULT_CHANNEL, loggerLevel); 80 } 81 82 83 88 public int getLoggerLevel() { 89 return getLoggerLevel(DEFAULT_CHANNEL); 90 } 91 92 93 94 99 public int getLoggerLevel(String channel) { 100 return loggerLevel; 101 } 102 103 104 109 public void setDateFormat(String pattern) { 110 dateFormat = new SimpleDateFormat (pattern); 111 } 112 113 114 116 117 125 public void log(Object data, Throwable throwable, String channel, int level) { 126 if (isEnabled(channel, level)) { 127 String levelValue = ""; 128 if (channel.equals(DEFAULT_CHANNEL)) 129 channel = ""; 130 else 131 channel = channel + " - "; 132 if ((level >= 0) && (level < loggingLevels.length)) 133 levelValue = loggingLevels[level]; 134 if (dateFormat == null) { 135 System.out.println(System.currentTimeMillis() + " - " 136 + channel + levelValue + " - " 137 + data); 138 } else { 139 System.out.println(dateFormat.format(new Date ()) + " - " 140 + channel + levelValue + " - " 141 + data); 142 } 143 throwable.printStackTrace(); 144 } 145 } 146 147 148 155 public void log(Object data, String channel, int level) { 156 if (isEnabled(channel, level)) { 157 String levelValue = ""; 158 if (channel.equals(DEFAULT_CHANNEL)) 159 channel = ""; 160 else 161 channel = channel + " - "; 162 if ((level >= 0) && (level < loggingLevels.length)) 163 levelValue = loggingLevels[level]; 164 if (dateFormat == null) { 165 System.out.println(System.currentTimeMillis() + " - " 166 + channel + levelValue + " - " 167 + data); 168 } else { 169 System.out.println(dateFormat.format(new Date ()) + " - " 170 + channel + levelValue + " - " 171 + data); 172 } 173 if (data instanceof Throwable ) 174 ((Throwable ) data).printStackTrace(); 175 } 176 } 177 178 179 185 public void log(Object data, int level) { 186 this.log(data, DEFAULT_CHANNEL, level); 187 } 188 189 190 195 public void log(Object data) { 196 this.log(data, DEFAULT_CHANNEL, Logger.DEBUG); 197 } 198 199 200 201 202 203 210 211 public boolean isEnabled(String channel, int level) { 212 return getLoggerLevel() >= level; 213 } 214 215 216 217 222 223 public boolean isEnabled(int level) { 224 return isEnabled(DEFAULT_CHANNEL, level); 225 } 226 227 228 } 229 | Popular Tags |