Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

logback

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.

like image 826
Nipun Talukdar Avatar asked May 12 '14 09:05

Nipun Talukdar


People also ask

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

What is Appender in Logback xml?

Logback Architecture The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations.

Which of the following is the configuration file for Logback?

As mentioned earlier, logback will try to configure itself using the files logback-test. xml or logback. xml if found on the class path.


2 Answers

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>
like image 180
iTake Avatar answered Oct 11 '22 12:10

iTake


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}

like image 45
Ron Romero Avatar answered Oct 11 '22 11:10

Ron Romero