用于过滤日志消息的正则表达式

发布于 2024-08-15 09:26:00 字数 1509 浏览 3 评论 0原文

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

    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;
}

I need to filter log messages based on the log level and a text appearing in the log message. These messages are in the following form:

    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

I already have the code below that creates 2 regular expressions (one for the level and one for the text) but I was wondering how I could combine these into one. I tried several combinations, but since my regexp skills are non-existing I couldn't get this to work.

Any hints?


Code:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2024-08-22 09:26:00

以下应该可以解决问题(之前从未使用过 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;
}

The following should do the trick (never worked with flex before, so not entirely sure whether syntax is correct ;-) )

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文