I am implementing Custom logging in Jetty10 to add the custom attributes with the request and I am able to do it using the below code. I have hardcoded userId, username, lastName, etc params for readability but it will be fetched at the runtime.
package net.codejava.javaee;
import java.io.IOException;
import org.eclipse.jetty.server.CustomRequestLog;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
public class MyJettyLogger extends CustomRequestLog
{
private Request request;
@Override
public void log(Request request, Response response)
{
this.request = request;
request.getMethod();
Writer writerObj = getWriter();
String logString = setCustomAttributesToLog();
super.log(request, response);
try {
writerObj.write(logString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MyJettyLogger(Writer writer, String logStr)
{
super(writer, logStr);
}
private String setCustomAttributesToLog(){
StringBuilder logBuffer = new StringBuilder("");
String userId="1234";
String username = "Ashish";
String lastName ="Goyanka";
String uri = request.getRequestURI();
String requestMethod = request.getMethod();
logBuffer.append(" ");
logBuffer.append(userId);
logBuffer.append(" ");
logBuffer.append(username);
logBuffer.append(" ");
logBuffer.append(lastName);
logBuffer.append(" ");
logBuffer.append(uri);
logBuffer.append(" ");
logBuffer.append(requestMethod);
return logBuffer.toString() ;
}
}
but the problem is they are printing in logs in the new line as below
[0:0:0:0:0:0:0:1] - - [31/Aug/2021:11:53:25 +0000] "GET /MyFirstServlet444/index.jsp HTTP/1.1" 200 481 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
1234 Ashish Goyanka /MyFirstServlet444/index.jsp GET
I just want them to be printed in the same line after the request. Do we have any way to do it?