简体   繁体   中英

Regex - match an entry in tomcat log file

I'm trying to read each log entry, one by one. So this is the part of log file:

at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 31 more
26.03.2018 14:43:57,113| INFO http-nio-8080-exec-10 configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService |com.ase.common.utils.ConfigurationServiceUtils
26.03.2018 14:43:57,113| WARN http-nio-8080-exec-10 Could not connect to services. |com.ase.common.utils.ConfigurationServiceUtils
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) 

This is the log pattern

ConversionPattern=%d{dd.MM.yyyy HH:mm:ss,SSS}| %p %t %m |%c%n

And I need to get all details from each entry, like date, priority value, thread, message and class. And this what I have so far.

(.*?)\| [A-Z]+ (.*?) (.*?) \|(.*)[\S\s]

It matches ok all entries but without stacktrace. How I need to improve my regex to get the stack trace also?

So I need to have it like this:

Match1 : `26.03.2018 14:43:57,113| INFO http-nio-8080-exec-10 configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService |com.ase.common.utils.ConfigurationServiceUtils` 

Group(1)-> `26.03.2018 14:43:57,113`; Group(2)->`INFO`; Group(3)-> `http-nio-8080-exec-10`; Group(4)->`configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService`; 
Group(5)->`com.ase.common.utils.ConfigurationServiceUtils`

Match2 : `26.03.2018 14:43:57,113| WARN http-nio-8080-exec-10 Could not connect to services. |com.ase.common.utils.ConfigurationServiceUtils
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) ` 

Group(1)-> `26.03.2018 14:43:57,113`; Group(2)->`WARN`; Group(3)-> `http-nio-8080-exec-10`; Group(4)->`Could not connect to services.`; 
Group(5)->`com.ase.common.utils.ConfigurationServiceUtils`; Group(6)->

    java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
        java.net.ConnectException: Connection refused...

You should go with more specific capturing groups in your Regular Expressions which I'm going to point:

^(\d+\.\d+\.\d{4}[^|]+)\|\s+(\S+)\s+(\S+)\s+([^|]+)\|(\S+)\s+((?:(?!^\d+\.)[^|])*)

Breakdown:

  • ^(\\d+\\.\\d+\\.\\d{4}[^|]+)\\| Match lines starting with a date (Captuing Group #1)
  • \\s+(\\S+) Match spaces and capture following non-whitespace chars (CG #2)
  • \\s+(\\S+) The same (CG #3)
  • \\s+([^|]+)\\| Match spaces and any thing other than | (CG #3)
  • (\\S+)\\s+ Match and capture non-whitespace chars followed by spaces (CG #4)
  • ((?:(?!^\\d+\\.)[^|])*) Tempered pattern, that checks if it is at beginning of a line that should be start of next match or not. If not match next immediate character (CG #5, optional group)

Live demo

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM