From 722179feb492a98baf24ff403204879a0a129e7c Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 20 Jun 2012 16:03:25 +0800 Subject: [PATCH] IDEMPIERE-313 Logging: Add support for setting of logging level by package --- .../src/org/compiere/util/CLogFile.java | 2 +- .../src/org/compiere/util/CLogMgt.java | 64 +++++++++++-------- .../src/org/compiere/util/CLogger.java | 37 +++++++++-- .../webui/session/SessionContextListener.java | 1 - .../adempiere/webui/session/WebUIServlet.java | 15 +---- 5 files changed, 73 insertions(+), 46 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/util/CLogFile.java b/org.adempiere.base/src/org/compiere/util/CLogFile.java index def98f9b4c..8bb1651fff 100644 --- a/org.adempiere.base/src/org/compiere/util/CLogFile.java +++ b/org.adempiere.base/src/org/compiere/util/CLogFile.java @@ -141,7 +141,7 @@ public class CLogFile extends Handler if (isClient) fileName += "client."; else - fileName += (CLogMgt.getRootLoggerName() +"."); + fileName += "idempiere."; m_fileNameDate = getFileNameDate(System.currentTimeMillis()); fileName += m_fileNameDate + "_"; for (int i = 0; i < 100; i++) diff --git a/org.adempiere.base/src/org/compiere/util/CLogMgt.java b/org.adempiere.base/src/org/compiere/util/CLogMgt.java index 71b4f74e4d..e928f28a4e 100644 --- a/org.adempiere.base/src/org/compiere/util/CLogMgt.java +++ b/org.adempiere.base/src/org/compiere/util/CLogMgt.java @@ -48,9 +48,6 @@ import org.compiere.model.MClient; */ public class CLogMgt { - public static final String DEFAULT_ROOT_LOGGER_NAME = "adempiere"; - public static final String ROOT_LOGGER_NAME_PROPERTY = "org.adempiere.log.root"; - /** * Initialize Logging * @param isClient client @@ -147,7 +144,7 @@ public class CLogMgt // Check Loggers if (!handlerNames.contains(CLogErrorBuffer.class.getName())) addHandler(new CLogErrorBuffer()); - if (!handlerNames.contains(CLogConsole.class.getName())) + if (isClient && !handlerNames.contains(CLogConsole.class.getName())) addHandler(new CLogConsole()); if (!handlerNames.contains(CLogFile.class.getName())) { @@ -164,14 +161,6 @@ public class CLogMgt } // initialize - public static String getRootLoggerName() { - String root = Env.getCtx().getProperty(ROOT_LOGGER_NAME_PROPERTY); - if (root == null || root.trim().length() == 0) - root = DEFAULT_ROOT_LOGGER_NAME; - return root; - } - - /** Logger */ private static Logger log = Logger.getAnonymousLogger(); /** LOG Levels */ @@ -266,26 +255,40 @@ public class CLogMgt * @param level log level */ public static void setLevel (Level level) + { + setLevel(null, level); + } + + /** + * Set Level for all handlers + * @param level log level + */ + public static void setLevel (String loggerName, Level level) { if (level == null) return; - Logger rootLogger = getRootLogger(); - rootLogger.setLevel(level); - Handler[] handlers = rootLogger.getHandlers(); - if (handlers == null || handlers.length == 0) + Logger logger = loggerName == null || loggerName.trim().length() == 0 ? getRootLogger() : CLogger.getCLogger(loggerName, false); + logger.setLevel(level); + + if (loggerName == null || loggerName.trim().length() == 0) { - initialize(true); - handlers = rootLogger.getHandlers(); + Handler[] handlers = logger.getHandlers(); + if (handlers == null || handlers.length == 0) + { + initialize(true); + } + + // JDBC if ALL + setJDBCDebug(level.intValue() == Level.ALL.intValue()); + // } - // - for (int i = 0; i < handlers.length; i++) + else { - handlers[i].setLevel(level); + if (!logger.getUseParentHandlers()) + { + logger.setUseParentHandlers(true); + } } - - // JDBC if ALL - setJDBCDebug(level.intValue() == Level.ALL.intValue()); - // } // setHandlerLevel /** @@ -302,6 +305,11 @@ public class CLogMgt * @param levelString string representation of level */ public static void setLevel (String levelString) + { + setLevel(null, levelString); + } // setLevel + + public static void setLevel(String loggerName, String levelString) { if (levelString == null) return; @@ -310,12 +318,12 @@ public class CLogMgt { if (LEVELS[i].getName().equals(levelString)) { - setLevel(LEVELS[i]); + setLevel(loggerName, LEVELS[i]); return; } } log.log(Level.CONFIG, "Ignored: " + levelString); - } // setLevel + } /** * Set JDBC Debug @@ -670,7 +678,7 @@ public class CLogMgt private static Logger getRootLogger() { - Logger rootLogger = Logger.getLogger(getRootLoggerName()); + Logger rootLogger = Logger.getLogger(""); if (rootLogger.getUseParentHandlers()) { rootLogger.setUseParentHandlers(false); diff --git a/org.adempiere.base/src/org/compiere/util/CLogger.java b/org.adempiere.base/src/org/compiere/util/CLogger.java index 9181a43b65..c9ea1d3a69 100644 --- a/org.adempiere.base/src/org/compiere/util/CLogger.java +++ b/org.adempiere.base/src/org/compiere/util/CLogger.java @@ -45,19 +45,48 @@ public class CLogger extends Logger implements Serializable * @return Logger */ public static synchronized CLogger getCLogger (String className) + { + return getCLogger(className, true); + } + + /** + * Get Logger + * @param className class name + * @param usePackageLevel + * @return Logger + */ + public static synchronized CLogger getCLogger (String className, boolean usePackageLevel) { // CLogMgt.initialize(); LogManager manager = LogManager.getLogManager(); if (className == null || className.trim().length() == 0) - className = CLogMgt.getRootLoggerName(); - else - className = CLogMgt.getRootLoggerName() + "." + className; + className = ""; + Logger result = manager.getLogger(className); if (result != null && result instanceof CLogger) return (CLogger)result; + + Logger packageLogger = null; + if (className.indexOf(".") > 0 && usePackageLevel) + { + String s = className.substring(0, className.lastIndexOf(".")); + while(s.indexOf(".") > 0) + { + result = manager.getLogger(s); + if (result != null && result instanceof CLogger) + { + packageLogger = result; + break; + } + s = s.substring(0, s.lastIndexOf(".")); + } + } // CLogger newLogger = new CLogger(className, null); - newLogger.setLevel(CLogMgt.getLevel()); + if (packageLogger != null && packageLogger.getLevel() != null) + newLogger.setLevel(packageLogger.getLevel()); + else + newLogger.setLevel(CLogMgt.getLevel()); manager.addLogger(newLogger); return newLogger; } // getLogger diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java index ba19b52f57..353baaf944 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java @@ -81,7 +81,6 @@ public class SessionContextListener implements ExecutionInit, SessionManager.getAppDesktop().showURL(url, true); } }); - ctx.put(CLogMgt.ROOT_LOGGER_NAME_PROPERTY, WebUIServlet.WEBUI_ROOT_LOGGER_NAME); ctx.setProperty(SERVLET_SESSION_ID, httpSession.getId()); session.setAttribute(SESSION_CTX, ctx); } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/WebUIServlet.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/WebUIServlet.java index 38bf71ee70..290cb93ed1 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/WebUIServlet.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/WebUIServlet.java @@ -30,7 +30,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.compiere.Adempiere; -import org.compiere.util.CLogMgt; import org.compiere.util.CLogger; import org.compiere.util.Ini; import org.zkoss.zk.ui.http.DHtmlLayoutServlet; @@ -46,8 +45,6 @@ import org.adempiere.util.ServerContextURLHandler; */ public class WebUIServlet extends DHtmlLayoutServlet { - public static final String WEBUI_ROOT_LOGGER_NAME = "webui"; - private static final long serialVersionUID = 1L; /** Logger for the class * */ @@ -64,7 +61,6 @@ public class WebUIServlet extends DHtmlLayoutServlet SessionManager.getAppDesktop().showURL(url, true); } }); - serverContext.put(CLogMgt.ROOT_LOGGER_NAME_PROPERTY, WEBUI_ROOT_LOGGER_NAME); ServerContext.setCurrentInstance(serverContext); String propertyFile = Ini.getFileName(false); @@ -78,20 +74,15 @@ public class WebUIServlet extends DHtmlLayoutServlet boolean started = Adempiere.startup(false); if(!started) { - throw new ServletException("Could not start ADempiere"); + throw new ServletException("Could not start iDempiere"); } } - else - { - CLogMgt.initialize(false); - CLogMgt.setLevel(Ini.getProperty(Ini.P_TRACELEVEL)); - } logger = CLogger.getCLogger(WebUIServlet.class); - logger.log(Level.OFF, "ADempiere web ui service started successfully"); + logger.log(Level.OFF, "iDempiere web ui service started successfully"); /** - * End ADempiere Start + * End iDempiere Start */ }