Nipun Talukdar
Nipun Talukdar

Reputation: 5387

How to configure Logback to add host-name/IP to each log event?

I am using Logback for logging. Scribe appenders send the logs in real time to a central Scribe aggregator. But I don't know how to add source machine IP in the logs for each log events. Looking at the aggregated central Scribe logs, it is almost impossible to know which machine is sending the logs. Hence, appending the IP of source machine to each log event will be helpful, and will be really great if we can control that through logback configuration.

Upvotes: 18

Views: 29178

Answers (3)

Ron Ziroby Romero
Ron Ziroby Romero

Reputation: 9459

According to the Logback docs, there's now a CanonicalHostNamePropertyDefiner expressly to add a hostname to your logs. Add a define to your project:

<define name="hostname" class="ch.qos.logback.core.property.CanonicalHostNamePropertyDefiner"/>

and access it as ${hostname}

Upvotes: 9

iTake
iTake

Reputation: 4200

It's possible to pass down hostname to remote receiver thru contextName.
Add following to logback.xml on all appenders:

<contextName>${HOSTNAME}</contextName>

Then, on aggregator instance, it will be available for inclusion in the pattern:

<pattern>%contextName %d %-5level %logger{35} - %msg %n</pattern>

Upvotes: 14

Mr.Q
Mr.Q

Reputation: 4524

well if you are working on a client server project then u can use MDC feature of slf4j/logback full document here and in this case you can have a well structured log file that you can identify which log is for which client

hope this helps!

Upvotes: 2

Related Questions