简体   繁体   中英

Add FQDN to log messages in SLF4J/Logback

I am using Logback to feed data into Logstash via a TCP appender .

The problem I have is that I can get the unqualified hostname (eg myhostname ) of the application creating logs out-of-the-box, but I can't see a way to get the FQDN (eg myhostname.mydomain.com ) to be logged instead.

I can see here that Logback comes with the HOSTNAME variable already configured in its Context, but I can't expand that to get the fqdn instead...

Edit: What I need is a way, from the logback configuration itself , to get hold og this info. Unfortunately, I can't modify the code that will emit the log message itself.

Any idea?

Thanks

You can try and resolve your name to IP address or to FQDN. Use InetAddress class and its static method getByName(String name) to try and resolve your name. After that you can retrieve from it IP address or FQDN by method "public String getCanonicalHostName()"

As mentioned in the logback documentation , HOSTNAME is automatically defined as variable within context scope as is available from within the configuration file.

The fully qualified domain is not defined automatically. However, you could define it on the fly with a custom property definer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM