RegEx用于过滤日志消息

时间:2009-12-23 16:39:25

标签: regex flex actionscript-3 actionscript flex3

我需要根据日志级别和日志消息中出现的文本来过滤日志消息。这些消息采用以下形式:

    12/23/2009 17:33:26.379 [INFO] TMSNG.Main Channelset configured with url [http://172.16.34.4:8080/tms-flux/messagebroker/streamingamf]
    12/23/2009 17:33:26.380 [DEBUG] org.springextensions.actionscript.core.command.CompositeCommand Executing composite command '[object CompositeCommand]' in sequence

我已经有了下面的代码,它创建了2个正则表达式(一个用于级别,一个用于文本),但我想知道如何将这些组合成一个。我尝试了几种组合,但由于我的正则表达式技能不存在,我无法使其发挥作用。

任何提示?


代码:

private function filterLogMessage(item:Object):Boolean {
  var logMessage:String = String(item);
  var levelFilter:String = getLevelFilter();
  var levelRegExp:RegExp = new RegExp(levelFilter, "");
  var textFilter:String = StringUtils.trim(filterTextInput.text);
  var textRegExp:RegExp = new RegExp(textFilter, "");
  var match:Boolean = (levelRegExp.test(logMessage) && textRegExp.test(logMessage));
  return match;
}

private function getLevelFilter():String {
  var result:String = "";
  var selectedLevel:String = levelComboBox.selectedLabel;

  switch (selectedLevel) {
    case "DEBUG":
      //result = "\\[(DEBUG|INFO|WARN|ERROR|FATAL)\\]";
      break;
    case "INFO":
      result = "\\[(INFO|WARN|ERROR|FATAL)\\]";
      break;
    case "WARN":
      result = "\\[(WARN|ERROR|FATAL)\\]";
      break;
    case "ERROR":
      result = "\\[(ERROR|FATAL)\\]";
      break;
    case "FATAL":
      result = "\\[(FATAL)\\]";
      break;
    }

  return result;
}

1 个答案:

答案 0 :(得分:2)

以下应该做的伎俩(之前从未使用过flex,所以不能完全确定语法是否正确;-))

private function filterLogMessage(item:Object):Boolean {
    var logMessage:String = String(item);
    var levelFilter:String = getLevelFilter();
    var textFilter:String = StringUtils.trim(filterTextInput.text);
    var filterRegExp:RegExp = new RegExp(levelFilter + ".*" + textFilter, "");
    var match:Boolean = (filterRegExp.test(logMessage));
    return match;
}