question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regexp for an slf4j log format

See original GitHub issue

Opening 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:open
  • Created 6 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

10reactions
rala72commented, May 7, 2019

I’ve found another solution for my Spring logs. It works for example for this one:

2019-05-06 15:06:19.038 DEBUG 1484 --- [main] a.a.t.s.g.backend.BackendApplication     : Running with Spring Boot v2.1.2.RELEASE, Spring v5.1.4.RELEASE

my config:

message pattern: ^([^ ]* [^ ]*)\s*([^ ]*)\s*(\d*)\s*-*\s*(\[[^]]*\])\s*([^ ]*)\s*:\s*(.*)$
message start pattern: ^\d
timeformat: yyyy-MM-dd HH:mm:ss.SSS
time capture group: 1
severity capture group: 3
category capture group: 4

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 the ing of warning optional with this pattern: (?i)^\s*w(arn(ing)?)?\s*$. It works now for messages like the following:

2019-05-06 19:48:16.804  WARN 16156 --- [http-nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: [...]
4reactions
knahcommented, Sep 18, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found