Regexp for an slf4j log format
See original GitHub issueOpening this as an issue to get your opinion. I was fairly confident the PR would be rejected since the format depends on the slf4 log formatter people use. But I added a regexp for a fairly used slf4j log format:
log line:
11:22:19.068 [Something Something #1] INFO a.b.c.d.e.f.gh.KLMNO - Some words that make sense and numbers 10.2.32.84
regex:
Message pattern: ^([^\[]+)\s*(\[.*])\s*([A-Z]+)\s*\s*(\S*)\s*-(.+)$
Message start pattern: ^\d
Time format: HH:mm:ss.SSS
Time capture group: 1
Severity capture group: 3
Category capture group: 4
feel free to test it at https://regex101.com
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:7
Top Results From Across the Web
Common Log Format - Regular Expressions Cookbook, 2nd ...
Common Log Format Problem You need a regular expression that matches each line in the log files produced by a web server that...
Read more >Slf4j Format String Example - 2022
In this example, we are going to see how String log messages can be constructed in SLF4J with parameter substitution. 1.
Read more >Regex Pattern for a Java Log - Stack Overflow
1 Answer 1 · ^ - start of string · (?<date>\d{4}-\d{2}-\d{2}) - date: 4 digits, - , 2 digits, - , 2 digits...
Read more >Regular Expression (Regex) Tutorial
Python also uses backslash ( \ ) for escape sequences (i.e., you need to write \\ for \ , \\d for \d ),...
Read more >How do I collect different types of logs in full regex mode?
This way, you can use multiple Logtail configurations to collect logs from a log file and extract the fields that you specify. Note...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve found another solution for my Spring logs. It works for example for this one:
my config:
I also made the default patterns for severity case insensitve by adding
(?i)
at the beginning of all of them and added a debug pattern:(?i)^\s*d(ebug)?\s*$
with a dark blue color. Finally I made theing
ofwarning
optional with this pattern:(?i)^\s*w(arn(ing)?)?\s*$
. It works now for messages like the following:It’s understandable that people use different log formats. This means that included formats serve more as examples, rather than something deemed universally useful, although including some common log formats is definitely nice.
Regarding your format: there is
\s*\s*
- probably that can be just one\s*
. Additionally, it might be better to use[^\]]*
instead of.*
for the text in square brackets. Besides, it looks like there are two groups that have category-like functionality - the one in square brackets, and the one with what appears to be class name. Probably we need a more flexible approach for categories, given that there are multiple log format that contain more than one.As another side note, we probably should include sample log lines with provided formats - trying to decipher the regex to get a feel for what a log format looks like is a bit too bothersome.