1 8 package org.jivesoftware.util.log; 9 10 import org.jivesoftware.util.log.format.PatternFormatter; 11 import org.jivesoftware.util.log.output.io.StreamTarget; 12 import org.jivesoftware.util.log.util.DefaultErrorHandler; 13 14 20 public class Hierarchy { 21 private static final String FORMAT = 23 "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}"; 24 25 private static final Hierarchy c_hierarchy = new Hierarchy(); 27 28 private ErrorHandler m_errorHandler; 30 31 private Logger m_rootLogger; 33 34 45 public static Hierarchy getDefaultHierarchy() { 46 return c_hierarchy; 47 } 48 49 53 public Hierarchy() { 54 m_errorHandler = new DefaultErrorHandler(); 55 m_rootLogger = new Logger(new InnerErrorHandler(), "", null, null); 56 57 final PatternFormatter formatter = new PatternFormatter(FORMAT); 59 final StreamTarget target = new StreamTarget(System.out, formatter); 60 61 setDefaultLogTarget(target); 62 } 63 64 70 public void setDefaultLogTarget(final LogTarget target) { 71 if (null == target) { 72 throw new IllegalArgumentException ("Can not set DefaultLogTarget to null"); 73 } 74 75 final LogTarget[] targets = new LogTarget[]{target}; 76 getRootLogger().setLogTargets(targets); 77 } 78 79 85 public void setDefaultLogTargets(final LogTarget[] targets) { 86 if (null == targets || 0 == targets.length) { 87 throw new IllegalArgumentException ("Can not set DefaultLogTargets to null"); 88 } 89 90 for (int i = 0; i < targets.length; i++) { 91 if (null == targets[i]) { 92 throw new IllegalArgumentException ("Can not set DefaultLogTarget element to null"); 93 } 94 } 95 96 getRootLogger().setLogTargets(targets); 97 } 98 99 105 public void setDefaultPriority(final Priority priority) { 106 if (null == priority) { 107 throw new IllegalArgumentException ("Can not set default Hierarchy Priority to null"); 108 } 109 110 getRootLogger().setPriority(priority); 111 } 112 113 118 public void setErrorHandler(final ErrorHandler errorHandler) { 119 if (null == errorHandler) { 120 throw new IllegalArgumentException ("Can not set default Hierarchy ErrorHandler to null"); 121 } 122 123 m_errorHandler = errorHandler; 124 } 125 126 132 public Logger getLoggerFor(final String category) { 133 return getRootLogger().getChildLogger(category); 134 } 135 136 159 private class InnerErrorHandler 160 implements ErrorHandler { 161 168 public void error(final String message, 169 final Throwable throwable, 170 final LogEvent event) { 171 m_errorHandler.error(message, throwable, event); 172 } 173 } 174 175 183 protected final Logger getRootLogger() { 184 return m_rootLogger; 185 } 186 } 187 | Popular Tags |