Why Logger name prints incorrect class name & How to customize that.?

60 views Asked by At

Here is my codes of LogUtils class. Which I used to format my log line. But it prints incorrect Class name in there. How to fix it.?

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.ext.logging.event.LogEvent;
import org.apache.cxf.ext.logging.slf4j.Slf4jVerboseEventSender;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.slf4j.MDC;
    public class LogUtils {
    
        private static final String TRACK_ID = "TRACK_ID";
    
        public static void setLogTrackingId(Object aLogTrackingId) {
            MDC.put(TRACK_ID, String.valueOf(aLogTrackingId));
        }
    
        public static LoggingInInterceptor getLoggingInInterceptor() {
            return new LoggingInInterceptor(getMsgSender()) {
                @Override
                public void handleMessage(Message message) throws Fault {
                    HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
                    String logTrackingId = request.getHeader(Constants.SYS_USER_ID);
                    if (StringUtils.isNotBlank(logTrackingId)) {
                        setLogTrackingId(logTrackingId);
                    } else {
                        setLogTrackingId(String.valueOf(System.nanoTime()));
                    }
                    super.handleMessage(message);
                }
            };
        }
    
        private static Slf4jVerboseEventSender getMsgSender() {
            return new Slf4jVerboseEventSender() {
                @Override
                protected String getLogMessage(LogEvent event) {
                    event.setAddress(event.getAddress());
                    event.setPayload(event.getPayload());
                    return super.getLogMessage(event);
                }
            };
        }
    }

So I'm getting below log line in my console. but that EmployeeAPIImpl is not expected but it prints when invoke other API classes as well. (EG : when invoke departmentAPI or attendanceAPI)

01:943 INFO  xyz123456789-01 EmployeeAPIImpl.REQ_IN [HRMService-UBUNTU_PC] [123123123123] REQ_IN

and here is the logger configurations on log4j

log4j.appender.A1.layout.ConversionPattern=%d{ss:SSS} %-5p %t %c{2} [%X{hrModuleName}_%X{hrModuleId}] [%X{TRACK_ID}] %m %n

How do I customize the Logger name from EmployeeAPIImpl to proper class name.?

EDIT When I tried with %c it will appear like below org.apache.cxf.services.EmployeeApiImpl

  • Thanks
1

There are 1 answers

1
M. Pour On

You might want to use %c instead of %c{2}, as this pattern only shows the last 2 elements of the logger name.

After the change the full class name will be displayed, something like com.example.EmployeeAPIImpl.

Good Luck!