Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SLF4J to log to two different files based on type of msg..?

i am running a client server program in spring. i am trying to implement SLF4J + Logback for logging.

Now the thing is my client (which in real life would be a device/sensor) will send me data in string format which contains various fields seperated by comma) exact pattern is like this : deviceID,DeviceName,DeviceLocation,TimeStamp,someValue

now what i want is to filter the message in Logback using deviceID and then write the whole string to file which has name like device.log suppose for example 1,indyaah,Scranton,2011-8-10 12:00:00,34 should be logged in to file device1.log dynamically. so how can i use evaluateFilter in logback/janino.

Thanks in advance.

like image 375
Anuj Patel Avatar asked Aug 10 '11 10:08

Anuj Patel


1 Answers

Logback provides all the features you need out of the box. You need to learn about SiftingAppender and probably MDC.

SiftingAppender wraps several homogeneous appenders and picks single one per each logging message based on user-defined criteria (called distriminator). The documentation is pretty good, and it has some nice examples.

like image 189
Tomasz Nurkiewicz Avatar answered Oct 17 '22 14:10

Tomasz Nurkiewicz