1 18 package sync4j.syncclient.common.logging; 19 20 import java.util.Calendar ; 21 import java.util.Date ; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 26 import sync4j.syncclient.common.FileSystemTools; 27 28 35 public class Logger { 36 37 39 public static final int NONE = 0 ; 40 public static final int ERROR = 1 ; 41 public static final int INFO = 5 ; 42 public static final int DEBUG = 10 ; 43 44 public static final String DEFAULT_LOG_FILE_NAME = "sync.log"; 45 46 public static final String PROP_NONE = "none" ; 47 public static final String PROP_INFO = "info" ; 48 public static final String PROP_DEBUG = "debug" ; 49 50 private static final int DEFAULT_LOGGER_LEVEL = INFO ; 51 private static final boolean DEFAULT_CONSOLE_ENABLE = true ; 52 53 private static final String MSG_LOG_START = "# SyncClient API J2SE Log"; 54 55 private static boolean fileInitialized = false; 56 private static boolean consoleHandlerInitialized = false; 57 private static boolean handlerInitialized = false; 58 59 private static Handler handler = null; 61 private static Handler consoleHandler = new OutputHandler(); 62 63 private static int level = 0 ; 64 private static boolean consoleEnable = false; 65 private static File logFile = null ; 66 67 69 75 public static void setHandler(Handler h) { 76 if (h != handler) { 77 handlerInitialized = false; 79 } 80 handler = h; 81 } 82 83 86 public static Handler getHandler() { 87 return handler; 88 } 89 90 95 public static void setLevel(int l) { 96 level = l; 97 } 98 99 104 public static void setLevel(String l) { 105 106 if (PROP_NONE.equals(l)) { 107 level = NONE; 108 } else if (PROP_INFO.equals(l)) { 109 level = INFO; 110 } else if (PROP_DEBUG.equals(l)) { 111 level = DEBUG; 112 } 113 } 114 115 118 public static void setDefaultLevel() { 119 level = DEFAULT_LOGGER_LEVEL; 120 } 121 122 127 public static void setLogFile(String fileName) { 128 logFile = new File (fileName); 129 } 130 131 134 public static void setDefaultLogFile() { 135 logFile = new File (DEFAULT_LOG_FILE_NAME); 136 } 137 138 143 public static void setEnableConsole(boolean enable) { 144 consoleEnable = enable; 145 } 146 147 150 public static void setDefaultEnableConsole() { 151 consoleEnable = DEFAULT_CONSOLE_ENABLE; 152 } 153 154 161 public static boolean isLoggable(int l) { 162 if (level >= l) { 163 return true; 164 } else { 165 return false; 166 } 167 } 168 169 174 public static void info(String text) { 175 if (!isLoggable(INFO)) { 176 return; 177 } 178 composeMessage("INFO", text); 179 } 180 181 186 public static void debug(String text) { 187 if (!isLoggable(DEBUG)) { 188 return; 189 } 190 composeMessage("DEBUG", text); 191 } 192 193 199 public static void error(String text) { 200 if (!isLoggable(ERROR)) { 201 return; 202 } 203 composeMessage("ERROR", text); 204 } 205 206 208 214 private static void composeMessage(String level, String text) { 215 checkInit(); 216 217 try { 218 219 text = getDateTime(true) 220 + " [" + level + "] - " 221 + text 222 + "\n" 223 ; 224 225 FileSystemTools.writeTextFile(logFile, text, true); 226 if (consoleEnable) { 227 consoleHandler.printMessage(text); 228 } 229 230 if (handler != null) { 231 handler.printMessage(text); 232 } 233 234 } catch (Exception e) { 235 e.printStackTrace(); 236 } 237 } 238 239 242 private static void checkInit() { 243 if (!fileInitialized) { 244 initLogFile(); 245 } 246 247 if (!consoleHandlerInitialized) { 248 initConsoleHandler(); 249 } 250 251 if (handler != null && !handlerInitialized) { 252 initHandler(); 253 } 254 } 255 256 259 private static synchronized void initLogFile() { 260 261 if (fileInitialized) { 262 return ; 263 } 264 265 try { 266 267 String initMsg = null; 268 269 initMsg = getDateTime(false) + 270 " - " + 271 MSG_LOG_START + 272 "\n" ; 273 274 FileSystemTools.writeTextFile(logFile, initMsg, false); 275 276 fileInitialized = true; 277 278 } catch (Exception e) { 279 System.err.println("Unable to initialize the logger [" + e.getMessage() + "]"); 280 } 281 282 } 283 284 287 private static synchronized void initHandler() { 288 289 if (handlerInitialized) { 290 return ; 291 } 292 293 try { 294 295 String initMsg = null; 296 297 initMsg = getDateTime(false) + 298 " - " + 299 MSG_LOG_START + 300 "\n" ; 301 302 303 handler.printMessage(initMsg); 304 305 handlerInitialized = true; 306 307 } catch (Exception e) { 308 System.err.println("Unable to initialize the handler [" + e.getMessage() + "]"); 309 } 310 311 } 312 313 316 private static synchronized void initConsoleHandler() { 317 318 if (!consoleEnable || consoleHandlerInitialized) { 319 return ; 320 } 321 322 try { 323 324 String initMsg = null; 325 326 initMsg = getDateTime(false) + 327 " - " + 328 MSG_LOG_START + 329 "\n" ; 330 331 332 consoleHandler.printMessage(initMsg); 333 334 consoleHandlerInitialized = true; 335 336 } catch (Exception e) { 337 System.err.println("Unable to initialize the handler [" + e.getMessage() + "]"); 338 } 339 340 } 341 342 348 private static String getDateTime(boolean onlyTime) { 349 350 Calendar date = null ; 351 352 String year = null ; 353 String month = null ; 354 String day = null ; 355 String hour = null ; 356 String minute = null ; 357 String second = null ; 358 String millisecond = null ; 359 360 String time = null ; 361 String dateTime = null ; 362 363 date = Calendar.getInstance(); 364 365 date.setTime(new Date (System.currentTimeMillis())); 366 367 year = String.valueOf( date.get(Calendar.YEAR ) ) ; 368 month = String.valueOf( date.get(Calendar.MONTH ) ) ; 369 day = String.valueOf( date.get(Calendar.DAY_OF_MONTH ) ) ; 370 hour = String.valueOf( date.get(Calendar.HOUR_OF_DAY ) ) ; 371 minute = String.valueOf( date.get(Calendar.MINUTE ) ) ; 372 second = String.valueOf( date.get(Calendar.SECOND ) ) ; 373 millisecond = String.valueOf( date.get(Calendar.MILLISECOND ) ) ; 374 375 if (month.length() == 1 ) { 376 month = "0" + month; 377 } 378 if (day.length() == 1 ) { 379 day = "0" + day; 380 } 381 if (hour.length() == 1 ) { 382 hour = "0" + hour; 383 } 384 if (minute.length() == 1 ) { 385 minute = "0" + minute; 386 } 387 if (second.length() == 1 ) { 388 second = "0" + second; 389 } 390 if (millisecond.length() == 1 ) { 391 millisecond = "00" + millisecond; 392 } 393 if (millisecond.length() == 2 ) { 394 millisecond = "0" + millisecond; 395 } 396 397 time = hour + 398 ":" + 399 minute + 400 ":" + 401 second + 402 ":" + 403 millisecond ; 404 405 if (onlyTime) { 406 return time; 407 } 408 409 dateTime = year + 410 "-" + 411 month + 412 "-" + 413 day + 414 " " + 415 time ; 416 417 return dateTime; 418 419 } 420 421 } 422 | Popular Tags |