2

I've been searching for any example of how to get the remote client's ip address printed in the log4j2 TcpSocketServer server side log file and I'm hoping I'm just not searching for the right thing. Is it possible to the remote client's ip and if so how?

If I use TcpSocketServer I can get this when the server starts up:

2017-04-10 11:30:31,316 [Log4j2-0] DEBUG [TcpSocketServer.java:231] Socket accepted: Socket[addr=/10.64.1.2,port=52412,localport=4560]

What I want is for the ip address to be somehow included on each logging line:

2017-04-10 11:30:31,316 [Log4j2-0] [10.64.1.2] DEBUG [MyFile.java:17] Test Logging line
kasdega
  • 18,396
  • 12
  • 45
  • 89
  • Couldn't you use the [ThreadContext](https://logging.apache.org/log4j/2.x/manual/thread-context.html)? Add the [appropriate IP address](http://stackoverflow.com/a/9482369/3284624) to the context and then add the variable to the pattern layout of the appender(s). – D.B. Apr 11 '17 at 02:14
  • I'm not sure. Do you have an example of how this would be configured? – kasdega Apr 12 '17 at 16:48
  • 1
    pls update with your resolution if you have resolved it. thx. – Leon Aug 05 '18 at 04:30

1 Answers1

1

Here is working example using filter:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.ThreadContext;


/**
 * Servlet Filter implementation class UserIPFilter
 */
@WebFilter(filterName="UserIPFilter")
public class UserIPFilter implements Filter {

    private final String IP_ADDRESS = "ipAddress";

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        boolean successfulRegistration = false;

        HttpServletRequest req = (HttpServletRequest) request;  
        
        if (req.getRemoteHost() != null) {
            String ipAddress = req.getRemoteHost();
            System.out.println("IP: " + ipAddress);
            successfulRegistration = registerUsername(ipAddress);
        }

        try {
            chain.doFilter(request, response);
        } finally {
            if (successfulRegistration) {
                ThreadContext.clearMap();
            }
        }
    }

    public void init(FilterConfig arg0) throws ServletException {
    }

    /**
     * Register the user in the ThreadContext under IP_ADDRESS.
     * 
     * @param username
     * @return true ipAddress can be successfully registered
     */
    private boolean registerUsername(String ipAddress) {
        if (ipAddress != null && ipAddress.trim().length() > 0) {
            ThreadContext.put(IP_ADDRESS, ipAddress);
            return true;
        }
        return false;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
----
----
<filter-mapping>
        <filter-name>UserIPFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>
</xml>

log4j2.xml

<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [%X{ipAddress}] [%t] %-5p (%c{1}.java:%L).%M - %m%n" />
zeeshan ansari
  • 381
  • 3
  • 16