I want to read username who logged in successfully through SSO and non-sso from log file. I think regular expression would help to achieve, but i need some directions to construct regex.
Input line to read from log:
12:59:39.924 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user1] & realm [SSORealm] successful.
13:21:38.017 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user2] & realm [Realm] successful.
13:19:30.419 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user1] & realm [LdapRealm] failed.
Note: I want to read username who successfully logged in because there will some authentication failure as well and don't want to read those names.
Expected output:
user1
user2
Sample Code which tried to read log file:
public class Log {
public static void main(String as[])
{
try{
FileInputStream fstream = new FileInputStream("C:\\Users\\test.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Appreciate your help
You can use a regular expression in order to get the user name of each line:
String input = "12:59:39.924 INFO AuthenticationStrategy - Authentication for username [user1] & realm [SSORealm] successful.";
Matcher matcher = Pattern.compile(" (\\[.*?\\])").matcher(input);
if (matcher.find(1)) {
String userName = matcher.group(1);
System.out.println(userName);
}
This might be an option to look at too:
(?m)(?<=username\\s\\[)[^\\r\\n\\]]+
If you wish to exclude the failed
ones, we can add a negative lookahead, such as with:
(?m)(?!.*\\bfailed)(?<=username\\s\\[)[^\\r\\n\\]]+
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression{
public static void main(String[] args){
final String regex = "(?m)(?!.*\\bfailed)(?<=username\\s\\[)[^\\r\\n\\]]+";
final String string = " 12:59:39.924 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user1] & realm [SSORealm] successful.\n"
+ " 13:21:38.017 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user2] & realm [Realm] successful.\n"
+ " 13:19:30.419 [ajp-nio-0.0.0.0-8009-exec-87] INFO AuthenticationStrategy - Authentication for username [user1] & realm [LdapRealm] failed.\n"
+ "N";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
}
}
}
Full match: user1
Full match: user2
If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com . You can watch the matching steps or modify them in this debugger link , if you'd be interested. The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.
jex.im visualizes regular expressions:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.