1 package org.exoplatform.services.log.impl; 2 3 import java.util.*; 4 import org.apache.commons.logging.Log; 5 import org.exoplatform.services.log.LogService; 6 7 11 12 16 public class LogServiceImpl implements LogService { 17 18 private HashMap logs_; 19 20 private HashMap configure_; 21 22 public LogServiceImpl() { 23 logs_ = new HashMap(); 24 configure_ = new HashMap(); 25 configure_.put("org.exoplatform.portal", new Integer (ExoLog.INFO)); 26 configure_.put("org.exoplatform.services.portletcontainer", 27 new Integer (ExoLog.INFO)); 28 } 29 30 public void start() { 31 } 32 33 public void stop() { 34 35 } 36 37 public Log getLog(String name) { 38 Log log = (Log) logs_.get(name); 39 if (log == null) { 40 synchronized (logs_) { 41 int level = ExoLog.INFO; 42 try { 43 level = getDefaultLogLevel(name); 44 } catch (Exception ex) { 45 } 46 log = new ExoLog(name, level); 47 logs_.put(name, log); 48 } 49 } 50 return log; 51 } 52 53 public Log getLog(Class clazz) { 54 String name = clazz.getName(); 55 int idx = name.lastIndexOf("."); 56 name = name.substring(0, idx); 57 return getLog(name); 58 } 59 60 public Collection getLogs() { 61 return logs_.values(); 62 } 63 64 public int getLogLevel(String name) throws Exception { 65 ExoLog log = (ExoLog) logs_.get(name); 66 if (log != null) 67 return log.getLevel(); 68 return INFO; 69 } 70 71 public void setLogLevel(String name, int level, boolean recursive) 72 throws Exception { 73 if (recursive) { 74 Iterator i = logs_.values().iterator(); 75 while (i.hasNext()) { 76 ExoLog log = (ExoLog) i.next(); 77 if (log.getLogCategory().startsWith(name)) { 78 log.setLevel(level); 79 } 80 } 81 } else { 82 ExoLog log = (ExoLog) logs_.get(name); 83 if (log != null) { 84 log.setLevel(level); 85 } 86 } 87 } 88 89 public List getLogBuffer() { 90 return ExoLog.getLogBuffer(); 91 } 92 93 public List getErrorBuffer() { 94 return ExoLog.getErrorBuffer(); 95 } 96 97 private int getDefaultLogLevel(String name) throws Exception { 98 while (name != null) { 99 Integer level = (Integer ) configure_.get(name); 100 if (level != null) 101 return level.intValue(); 102 int index = name.lastIndexOf("."); 103 if (index > 0) 104 name = name.substring(0, index); 105 else 106 name = null; 107 } 108 return ExoLog.INFO; 109 } 110 } | Popular Tags |