2

In log4j, How do we represent more than one spaces (like : \s+) in the pattern? I have a pattern like this %d{ISO8601}\t%t\t%m %n, but the tab-separation is not reliable when I was parsing through chainsaw and LogMX. I am searching for replacement of multiple-space-separated rather than tab-separated , but I cannot find any.

Any help is appreciated. Thank you.

xav
  • 5,452
  • 7
  • 48
  • 57
Deepak Battu
  • 149
  • 1
  • 1
  • 5

4 Answers4

3

The layout pattern in log4j is not a regular expression pattern, and is not used to search for strings. As its name idicates, it is used for laying out the message to be priinted by log4j. With something like \s+, how would it know how many spaces to print? Therefore, if you need multiple spaces in the message layout, you have to explicitely type these spaces in the pattern.

Lolo
  • 4,277
  • 2
  • 25
  • 24
1

I am not sure what you really want to achieve but you should have a look at the "format modifier" explanation of the PatternLayout documentation.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
0

I've just tried your Log4j pattern and a freshly generated log file was parsed correctly by LogMX (using a Log4jPattern parser with this Pattern). Could you explain "the tab-separation is not reliable when I was parsing" ?

Although, the extra whitespace character between %m and %n is quite strange: every log message shall end with a whitespace character to be parsed correctly.

PS : I agree with @FrVaBe and @Lolo answers

xav
  • 5,452
  • 7
  • 48
  • 57
0

Chainsaw uses log4j's 'LogFilePatternReceiver' to process log files.

LogFilePatternReceiver has code to merge multiple spaces, but that code uses [ ]+ instead of \s+

I'll look into changing it to use \s+.

Scott
  • 1,728
  • 11
  • 11