WebLogic:WTC在DEBUG模式下日志打印过程分析

WebLogic:WTC在DEBUG模式下日志打印过程分析,第1张

WebLogicWTC相关模块都是通过类似如下代码开始打印日志的:

boolean traceEnabled = ntrace.isTraceEnabled(2);
if (traceEnabled)
  ntrace.doTrace("[/TuxedoConnection/tpacall/" + svc); 

当前使用的WebLogic版本为12.2.1.4.0,接下来分析一下WTC模块日志打印的整个调用过程。

(1)com.bea.core.jatmi.jar
com.bea.core.jatmi.common.ntrace
    public static void doTrace(String todo)
         _logger.doTrace(todo);

这个类中比较重要的代码如下,先放到这里,后面还会对这部分代码进行分析

  public static boolean init(LogService n_logger) {
    if (n_logger == null || !(n_logger instanceof LogService))
      return false; 
    _logger = n_logger;
    String tracelevel;
    if ((tracelevel = System.getProperty("Weblogic.wtc.TraceLevel")) != null)
      _level = Integer.parseInt(tracelevel); 
    _logger.setTraceLevel(_level);
    _logger.doTrace("INFO: Logging service enabled.");
    return true;
  }

  public static void doTrace(String todo) {
    if (_logger != null)
      _logger.doTrace(todo); 
  }
  
  public static void doTrace(int type, String todo) {
    if (_logger != null)
      _logger.doTrace(type, todo); 
  }
  
  public static void doTrace(int level, int type, String todo) {
    if (_logger != null)
      _logger.doTrace(level, type, todo); 
  }
  
  public static boolean isTraceEnabled(int type) {
    if (_logger != null)
      return _logger.isTraceEnabled(type); 
    return false;
  }

(2)com.oracle.weblogic.wtc.jar
weblogic.wtc.wls.WlsLogService implements LogService
    public void doTrace(String todo)
        WTCLogger.logDebugMsg(todo);

这个类中比较重要,代码全贴出来:


package weblogic.wtc.wls;

import com.bea.core.jatmi.intf.LogService;
import weblogic.diagnostics.debug.DebugLogger;
import weblogic.wtc.WTCLogger;

public final class WlsLogService implements LogService {
  private int level = -1;
  
  private DebugLogger CorbaEx;
  
  private DebugLogger GwtEx;
  
  private DebugLogger JatmiEx;
  
  private DebugLogger tBridgeEx;
  
  private DebugLogger WtcConfig;
  
  private DebugLogger WtcTdomPdu;
  
  private DebugLogger WtcUData;
  
  private boolean _debug = false;
  
  public WlsLogService() {
    this.CorbaEx = DebugLogger.getDebugLogger("DebugWTCCorbaEx");
    this.GwtEx = DebugLogger.getDebugLogger("DebugWTCGwtEx");
    this.JatmiEx = DebugLogger.getDebugLogger("DebugWTCJatmiEx");
    this.tBridgeEx = DebugLogger.getDebugLogger("DebugWTCtBridgeEx");
    this.WtcConfig = DebugLogger.getDebugLogger("DebugWTCConfig");
    this.WtcTdomPdu = DebugLogger.getDebugLogger("DebugWTCTdomPdu");
    this.WtcUData = DebugLogger.getDebugLogger("DebugWTCUData");
  }
  
  public void setTraceLevel(int l) {
    this.level = l;
  }
  
  public void doTrace(String todo) {
    if (this._debug) {
      WTCLogger.logDebugMsg("WlsLogService:" + todo);
    } else {
      WTCLogger.logDebugMsg(todo);
    } 
  }
  
  private boolean typeStatus(int type) {
    if ((type & 0x4) == 4) {
      if (this.JatmiEx.isDebugEnabled())
        return true; 
      if (this.level >= 55000)
        return true; 
    } 
    if ((type & 0x2) == 2) {
      if (this.GwtEx.isDebugEnabled())
        return true; 
      if (this.level >= 25000)
        return true; 
    } 
    if ((type & 0x1) == 1) {
      if (this.tBridgeEx.isDebugEnabled())
        return true; 
      if (this.level >= 15000)
        return true; 
    } 
    if ((type & 0x8) == 8) {
      if (this.CorbaEx.isDebugEnabled())
        return true; 
      if (this.level >= 65000)
        return true; 
    } 
    if ((type & 0x10) == 16 && 
      this.WtcConfig.isDebugEnabled())
      return true; 
    if ((type & 0x20) == 32 && 
      this.WtcTdomPdu.isDebugEnabled())
      return true; 
    if ((type & 0x40) == 64 && 
      this.WtcUData.isDebugEnabled())
      return true; 
    return false;
  }
  
  public void doTrace(int type, String todo) {
    if (typeStatus(type) == true)
      WTCLogger.logDebugMsg(todo); 
  }
  
  public void doTrace(int level, int type, String todo) {
    if (typeStatus(type) == true)
      WTCLogger.logDebugMsg(todo); 
  }
  
  public boolean isTraceEnabled(int type) {
    switch (type) {
      case 4:
        if (this.JatmiEx.isDebugEnabled())
          return true; 
        if (this.level >= 55000)
          return true; 
        break;
      case 2:
        if (this.GwtEx.isDebugEnabled())
          return true; 
        if (this.level >= 25000)
          return true; 
        break;
      case 1:
        if (this.tBridgeEx.isDebugEnabled())
          return true; 
        if (this.level >= 15000)
          return true; 
        break;
      case 8:
        if (this.CorbaEx.isDebugEnabled())
          return true; 
        if (this.level >= 65000)
          return true; 
        break;
      case 16:
        if (this.WtcConfig.isDebugEnabled())
          return true; 
        break;
      case 32:
        if (this.WtcTdomPdu.isDebugEnabled())
          return true; 
        break;
      case 64:
        if (this.WtcUData.isDebugEnabled())
          return true; 
        break;
    } 
    return false;
  }
  
  public boolean isMixedTraceEnabled(int type) {
    return typeStatus(type);
  }
}

DebugLogger.getDebugLogger(...)会读取JVM参数,这些参数会带上weblogic.debug.前缀
因此,可使用如下JVM参数打开对应组件的debug模式:

-Dweblogic.debug.DebugWTCCorbaEx=true
-Dweblogic.debug.DebugWTCGwtEx=true
-Dweblogic.debug.DebugWTCJatmiEx=true
-Dweblogic.debug.DebugWTCtBridgeEx=true
-Dweblogic.debug.DebugWTCConfig=true
-Dweblogic.debug.DebugWTCTdomPdu=true
-Dweblogic.debug.DebugWTCUData=true

另外,如果不想配置其中的一部分,通过(1)中的代码可以看到,也可以使用Weblogic.wtc.TraceLevel一次性打开多个功能模块的debug模式。
-DWeblogic.wtc.TraceLevel=25000,会打开如下2个功能模块的debug模式

tBridgeEx
GwtEx

-DWeblogic.wtc.TraceLevel=55000,会打开如下3个功能模块的debug模式

JatmiEx
tBridgeEx
GwtEx

(3)com.bea.core.jatmi.jar
weblogic.wtc.WTCLogger
    public static String logDebugMsg(String arg0)
        MessageLoggerInitializer.INSTANCE.messageLogger.log((LogMessage)catalogMessage)

(4)com.bea.core.logging.jar
com.bea.logging.LoggingService implements MessageLogger, BaseLoggerFactory, BaseLogRecordFactory, PropertyChangeListener
    public void log(LogMessage logMessage)
        logger.log(BaseLogRecord);

这一步中,会将LogMessage(severity=128) 转化为 BaseLogRecord(LogLevel= DEBUG, level = 495) ,于是,这个日志记录就有了最终的日志级别。其中severity -> level的转化过程,使用到了一个很重要的类LogLevel,下面就是它的代码。

com.bea.core.logging.jar:
com.bea.logging.LogLevel extends java.util.logging.Level

package com.bea.logging;

import java.util.logging.Level;

public class LogLevel extends Level {
  public static final int OFF_INT = 2147483647;
  
  public static final int EMERGENCY_INT = 1090;
  
  public static final int ALERT_INT = 1060;
  
  public static final int CRITICAL_INT = 1030;
  
  public static final int ERROR_INT = 980;
  
  public static final int WARNING_INT = Level.WARNING.intValue();
  
  public static final int NOTICE_INT = 880;
  
  public static final int INFO_INT = Level.INFO.intValue();
  
  public static final int DEBUG_INT = 495;
  
  public static final int TRACE_INT = 295;
  
  public static final LogLevel OFF = new LogLevel("Off", 2147483647, 0);
  
  public static final LogLevel EMERGENCY = new LogLevel("Emergency", 1090, 1);
  
  public static final LogLevel CRITICAL = new LogLevel("Critical", 1030, 4);
  
  public static final LogLevel ERROR = new LogLevel("Error", 980, 8);
  
  public static final LogLevel ALERT = new LogLevel("Alert", 1060, 2);
  
  public static final LogLevel NOTICE = new LogLevel("Notice", 880, 32);
  
  public static final LogLevel DEBUG = new LogLevel("Debug", 495, 128);
  
  public static final LogLevel WARNING = new LogLevel("Warning", WARNING_INT, 16);
  
  public static final LogLevel INFO = new LogLevel("Info", INFO_INT, 64);
  
  public static final LogLevel TRACE = new LogLevel("Trace", 295, 256);
  
  public static final String LOGGING_TEXT_PROPS = "weblogic.i18n.logging.LoggingTextLocalizer";
  
  private static final long serialVersionUID = 1796084591280954044L;
  
  private final int severity;
  
  private String localizedName;
  
  protected LogLevel(String name, int value, int severity) {
    super(name, value, "weblogic.i18n.logging.LoggingTextLocalizer");
    this.severity = severity;
    this.localizedName = super.getLocalizedName();
  }
  
  @Deprecated
  protected String getHeader(Level level) {
    return level.getLocalizedName();
  }
  
  public int getSeverity() {
    return this.severity;
  }
  
  public String getLocalizedName() {
    return this.localizedName;
  }
  
  public boolean equals(Object obj) {
    if (obj != null && obj instanceof LogLevel) {
      LogLevel other = (LogLevel)obj;
      return (intValue() == other.intValue() && getName().equals(other.getName()));
    } 
    return false;
  }
  
  public int hashCode() {
    return intValue();
  }
  
  public static Level getLevel(int severity) {
    switch (severity) {
      case 0:
        return OFF;
      case 1:
        return EMERGENCY;
      case 2:
        return ALERT;
      case 4:
        return CRITICAL;
      case 32:
        return NOTICE;
      case 8:
        return ERROR;
      case 16:
        return WARNING;
      case 64:
        return INFO;
      case 128:
        return DEBUG;
      case 256:
        return TRACE;
    } 
    return INFO;
  }
  
  public static int getSeverity(Level level) {
    int levelValue = level.intValue();
    if (levelValue == Integer.MAX_VALUE)
      return 0; 
    if (levelValue >= 1090)
      return 1; 
    if (levelValue >= 1060)
      return 2; 
    if (levelValue >= 1030)
      return 4; 
    if (levelValue >= 980)
      return 8; 
    if (levelValue >= WARNING_INT)
      return 16; 
    if (levelValue >= 880)
      return 32; 
    if (levelValue >= INFO_INT)
      return 64; 
    if (levelValue >= 495)
      return 128; 
    return 256;
  }
}

(5)com.oracle.weblogic.logging.jar
weblogic.logging.WLLogger extends BaseLogger
    public void log(LogRecord record)
        super.log(wlLogRecord)

Weblogic启动时,会根据如下属性中的日志级别,设置WLLogger的日志级别,此日志级别实际上是保存在基类java.util.logging.Logger中的:
【环境】->【服务器】->    ->【日志记录】->【一般信息】->【要记录的最低严重性】

(6)com.bea.core.logging.jar
com.bea.logging.BaseLogger extends java.util.logging.Logger
    public void log(LogRecord record)
        super.log(LogRecord);

    
(7)JDK rt.jar
java.util.logging.Logger
    public void log(LogRecord record)

其中比较重要的方法如下:

public void log(LogRecord record) {
	if (!isLoggable(record.getLevel())) {
		return;
	}
	Filter theFilter = filter;
	if (theFilter != null && !theFilter.isLoggable(record)) {
		return;
	}

	// Post the LogRecord to all our Handlers, and then to
	// our parents' handlers, all the way up the tree.

	Logger logger = this;
	while (logger != null) {
		final Handler[] loggerHandlers = isSystemLogger
			? logger.accessCheckedHandlers()
			: logger.getHandlers();

		for (Handler handler : loggerHandlers) {
			handler.publish(record);
		}

		final boolean useParentHdls = isSystemLogger
			? logger.useParentHandlers
			: logger.getUseParentHandlers();

		if (!useParentHdls) {
			break;
		}

		logger = isSystemLogger ? logger.parent : logger.getParent();
	}
}

public boolean isLoggable(Level level) {
    if (level.intValue() < levelValue || levelValue == offValue) {
        return false;
    }
    return true;
}

从以上代码可以分析到,java.util.logging.Logger会与LogRecord进行日志级别比较,小于等于时则打印。

总结,WebLogic的功能模块的日志是否打印主要由2方面决定:

  1. WebLogic的功能模块要开启DEBUG模式
  2. WebLogic的Logger日志级别要设置成DEBUG

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/923820.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存