IDEMPIERE-313 Logging: Add support for setting of logging level by package

This commit is contained in:
Heng Sin Low 2012-06-20 16:03:25 +08:00
parent 4553a5a20e
commit 722179feb4
5 changed files with 73 additions and 46 deletions

View File

@ -141,7 +141,7 @@ public class CLogFile extends Handler
if (isClient) if (isClient)
fileName += "client."; fileName += "client.";
else else
fileName += (CLogMgt.getRootLoggerName() +"."); fileName += "idempiere.";
m_fileNameDate = getFileNameDate(System.currentTimeMillis()); m_fileNameDate = getFileNameDate(System.currentTimeMillis());
fileName += m_fileNameDate + "_"; fileName += m_fileNameDate + "_";
for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)

View File

@ -48,9 +48,6 @@ import org.compiere.model.MClient;
*/ */
public class CLogMgt 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 * Initialize Logging
* @param isClient client * @param isClient client
@ -147,7 +144,7 @@ public class CLogMgt
// Check Loggers // Check Loggers
if (!handlerNames.contains(CLogErrorBuffer.class.getName())) if (!handlerNames.contains(CLogErrorBuffer.class.getName()))
addHandler(new CLogErrorBuffer()); addHandler(new CLogErrorBuffer());
if (!handlerNames.contains(CLogConsole.class.getName())) if (isClient && !handlerNames.contains(CLogConsole.class.getName()))
addHandler(new CLogConsole()); addHandler(new CLogConsole());
if (!handlerNames.contains(CLogFile.class.getName())) if (!handlerNames.contains(CLogFile.class.getName()))
{ {
@ -164,14 +161,6 @@ public class CLogMgt
} // initialize } // 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 */ /** Logger */
private static Logger log = Logger.getAnonymousLogger(); private static Logger log = Logger.getAnonymousLogger();
/** LOG Levels */ /** LOG Levels */
@ -266,26 +255,40 @@ public class CLogMgt
* @param level log level * @param level log level
*/ */
public static void setLevel (Level 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) if (level == null)
return; return;
Logger rootLogger = getRootLogger(); Logger logger = loggerName == null || loggerName.trim().length() == 0 ? getRootLogger() : CLogger.getCLogger(loggerName, false);
rootLogger.setLevel(level); logger.setLevel(level);
Handler[] handlers = rootLogger.getHandlers();
if (handlers == null || handlers.length == 0) if (loggerName == null || loggerName.trim().length() == 0)
{ {
initialize(true); Handler[] handlers = logger.getHandlers();
handlers = rootLogger.getHandlers(); if (handlers == null || handlers.length == 0)
{
initialize(true);
}
// JDBC if ALL
setJDBCDebug(level.intValue() == Level.ALL.intValue());
//
} }
// else
for (int i = 0; i < handlers.length; i++)
{ {
handlers[i].setLevel(level); if (!logger.getUseParentHandlers())
{
logger.setUseParentHandlers(true);
}
} }
// JDBC if ALL
setJDBCDebug(level.intValue() == Level.ALL.intValue());
//
} // setHandlerLevel } // setHandlerLevel
/** /**
@ -302,6 +305,11 @@ public class CLogMgt
* @param levelString string representation of level * @param levelString string representation of level
*/ */
public static void setLevel (String levelString) public static void setLevel (String levelString)
{
setLevel(null, levelString);
} // setLevel
public static void setLevel(String loggerName, String levelString)
{ {
if (levelString == null) if (levelString == null)
return; return;
@ -310,12 +318,12 @@ public class CLogMgt
{ {
if (LEVELS[i].getName().equals(levelString)) if (LEVELS[i].getName().equals(levelString))
{ {
setLevel(LEVELS[i]); setLevel(loggerName, LEVELS[i]);
return; return;
} }
} }
log.log(Level.CONFIG, "Ignored: " + levelString); log.log(Level.CONFIG, "Ignored: " + levelString);
} // setLevel }
/** /**
* Set JDBC Debug * Set JDBC Debug
@ -670,7 +678,7 @@ public class CLogMgt
private static Logger getRootLogger() private static Logger getRootLogger()
{ {
Logger rootLogger = Logger.getLogger(getRootLoggerName()); Logger rootLogger = Logger.getLogger("");
if (rootLogger.getUseParentHandlers()) if (rootLogger.getUseParentHandlers())
{ {
rootLogger.setUseParentHandlers(false); rootLogger.setUseParentHandlers(false);

View File

@ -45,19 +45,48 @@ public class CLogger extends Logger implements Serializable
* @return Logger * @return Logger
*/ */
public static synchronized CLogger getCLogger (String className) 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(); // CLogMgt.initialize();
LogManager manager = LogManager.getLogManager(); LogManager manager = LogManager.getLogManager();
if (className == null || className.trim().length() == 0) if (className == null || className.trim().length() == 0)
className = CLogMgt.getRootLoggerName(); className = "";
else
className = CLogMgt.getRootLoggerName() + "." + className;
Logger result = manager.getLogger(className); Logger result = manager.getLogger(className);
if (result != null && result instanceof CLogger) if (result != null && result instanceof CLogger)
return (CLogger)result; 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); 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); manager.addLogger(newLogger);
return newLogger; return newLogger;
} // getLogger } // getLogger

View File

@ -81,7 +81,6 @@ public class SessionContextListener implements ExecutionInit,
SessionManager.getAppDesktop().showURL(url, true); SessionManager.getAppDesktop().showURL(url, true);
} }
}); });
ctx.put(CLogMgt.ROOT_LOGGER_NAME_PROPERTY, WebUIServlet.WEBUI_ROOT_LOGGER_NAME);
ctx.setProperty(SERVLET_SESSION_ID, httpSession.getId()); ctx.setProperty(SERVLET_SESSION_ID, httpSession.getId());
session.setAttribute(SESSION_CTX, ctx); session.setAttribute(SESSION_CTX, ctx);
} }

View File

@ -30,7 +30,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.compiere.Adempiere; import org.compiere.Adempiere;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.Ini; import org.compiere.util.Ini;
import org.zkoss.zk.ui.http.DHtmlLayoutServlet; import org.zkoss.zk.ui.http.DHtmlLayoutServlet;
@ -46,8 +45,6 @@ import org.adempiere.util.ServerContextURLHandler;
*/ */
public class WebUIServlet extends DHtmlLayoutServlet public class WebUIServlet extends DHtmlLayoutServlet
{ {
public static final String WEBUI_ROOT_LOGGER_NAME = "webui";
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** Logger for the class * */ /** Logger for the class * */
@ -64,7 +61,6 @@ public class WebUIServlet extends DHtmlLayoutServlet
SessionManager.getAppDesktop().showURL(url, true); SessionManager.getAppDesktop().showURL(url, true);
} }
}); });
serverContext.put(CLogMgt.ROOT_LOGGER_NAME_PROPERTY, WEBUI_ROOT_LOGGER_NAME);
ServerContext.setCurrentInstance(serverContext); ServerContext.setCurrentInstance(serverContext);
String propertyFile = Ini.getFileName(false); String propertyFile = Ini.getFileName(false);
@ -78,20 +74,15 @@ public class WebUIServlet extends DHtmlLayoutServlet
boolean started = Adempiere.startup(false); boolean started = Adempiere.startup(false);
if(!started) 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 = 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
*/ */
} }