0

My project is in cluster form and while using logger.debug, I want to know which system has received the request and when I check logs, I should also see system IP as a parameter in logs.

When we use log4j2, inside log file you get time & name of class.

[DEBUG ] 2018-03-05 01:16:43.233 <className>

In this line I also want to know system IP.

Tatkal
  • 568
  • 4
  • 9
  • 29

1 Answers1

0

The question seems to be the possible duplicate of Getting IP address of client the below code can be used print the ip Address.hope it helps to an extent

 Something like this:
 MDC.put("ip", PortalUtil.getHttpServletRequest(request).getRemoteAddr() );
 MDC.put("user", request.getRemoteUser());

 With appender configuration:
 <appender name="MY_APPENDER" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%X{ip}][%X{user}][%c{1}:%L] %m%n" />
    </layout>
 </appender>

refer :https://web.liferay.com/community/forums/-/message_boards/view_message/18089322#_19_message_18089322

Ann
  • 463
  • 5
  • 15
  • To avoid coding, I want to know how we can get system IP using log4j2 – Tatkal Mar 05 '18 at 09:49
  • you can add appender as above @Tatkal – Ann Mar 05 '18 at 10:01
  • @Tatkal - I expect that you will find that you can't avoid having to do some coding. But hey ... why is coding a problem for a Java programmer like you? – Stephen C Mar 05 '18 at 10:01
  • @StephenC coding is not a problem for me. Right now I am in chaos. – Tatkal Mar 05 '18 at 10:19
  • So you why are you trying to avoid coding, if it is not a problem for you? As I said, you probably can't avoid it anyway. So by *trying* to avoid it, you are actually making your life *more* difficult, – Stephen C Mar 05 '18 at 10:20