用于过滤日志消息的正则表达式
我需要根据日志级别和日志消息中出现的文本来过滤日志消息。这些消息采用以下形式:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下应该可以解决问题(之前从未使用过 flex,所以不完全确定语法是否正确;-))
The following should do the trick (never worked with flex before, so not entirely sure whether syntax is correct ;-) )