001 package org.shiftone.jrat.util.log; 002 003 004 import org.shiftone.jrat.core.Environment; 005 import org.shiftone.jrat.core.JRatException; 006 import org.shiftone.jrat.util.Assert; 007 import org.shiftone.jrat.util.log.target.*; 008 009 import java.io.PrintWriter; 010 011 012 /** 013 * There are currently 3 ways that logging can be configured... 014 * <li>turned off - using NullLogTarget 015 * <li>logging to a PrintWriter - System.out or file 016 * <li>using thread based logging - each thread can have it's own LogTarget 017 * (this is for the Desktop) 018 * 019 * @author jeff@shiftone.org (Jeff Drost) 020 */ 021 public class LoggerFactory implements Constants { 022 023 private static final NullLogTarget NULL_LOG_TARGET = NullLogTarget.INSTANCE; 024 private static final WriterLogTarget SYSTEM_OUT_TARGET = new WriterLogTarget(System.out); 025 private static final ProxyLogTarget PROXY_LOG_TARGET = new ProxyLogTarget(SYSTEM_OUT_TARGET); 026 private static final ThreadLocalLogTarget THREAD_TARGET = new ThreadLocalLogTarget(SYSTEM_OUT_TARGET); 027 028 public static void initialize() { 029 030 // try { 031 setLevel(getLevelFromName(Environment.getSettings().getLogLevel())); 032 033 // } catch (Exception e) {} 034 } 035 036 037 public static Logger getLogger(Class klass) { 038 039 String className = klass.getName(); 040 String shortName = className.substring(className.lastIndexOf('.') + 1); 041 042 return getLogger(shortName); 043 } 044 045 046 public static Logger getLogger(String topic) { 047 return new Logger(topic, PROXY_LOG_TARGET); 048 } 049 050 051 public static int getLevelFromName(String levelName) { 052 053 Assert.assertNotNull("levelName", levelName); 054 Assert.assertNotNull("LEVEL_NAMES", LEVEL_NAMES); 055 056 levelName = levelName.toUpperCase(); 057 058 for (int i = 0; i < LEVEL_NAMES.length; i++) { 059 if (levelName.equals(LEVEL_NAMES[i])) { 060 return i; 061 } 062 } 063 064 throw new JRatException("log level '" + levelName + "' is not known"); 065 } 066 067 068 public static void setLevel(int level) { 069 PROXY_LOG_TARGET.setCurrentLevel(level); 070 } 071 072 073 public static int getLevel() { 074 return PROXY_LOG_TARGET.getCurrentLevel(); 075 } 076 077 078 public static void disableLogging() { 079 setLogTarget(NULL_LOG_TARGET); 080 } 081 082 083 public static void enableThreadBasedLogging() { 084 setLogTarget(THREAD_TARGET); 085 } 086 087 088 public static void enableSystemOutLogging() { 089 setLogTarget(SYSTEM_OUT_TARGET); 090 } 091 092 public static synchronized void setLogTarget(LogTarget logTarget) { 093 PROXY_LOG_TARGET.setLogTarget(logTarget); 094 } 095 096 public static synchronized LogTarget getLogTarget() { 097 return PROXY_LOG_TARGET.getLogTarget(); 098 } 099 100 public static void redirectLogging(PrintWriter printWriter) { 101 102 TandemTarget tandemTarget = new TandemTarget(SYSTEM_OUT_TARGET, new WriterLogTarget(printWriter)); 103 104 setLogTarget(tandemTarget); 105 } 106 107 108 /** 109 * this will only have any effect on logging if the current mode is using 110 * the ThreadLocalLogTarget - meaning a call to enableDesktopLoggingMode was 111 * made. 112 */ 113 public static void executeInThreadScope(LogTarget newTarget, Runnable runnable) { 114 THREAD_TARGET.executeInScope(newTarget, runnable); 115 } 116 }