-1

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?

Ashish Goyanka
  • 207
  • 3
  • 11

1 Answers1

1

You are calling the below first, and that will pretty format the request/response and log that as a new line.

super.log(request, response);

public void log​(Request request, Response response)

Writes the request and response information to the output stream.

log

public void log​(Request request, Response response)

Writes the request and response information to the output stream.

Specified by:
    log in interface RequestLog
Parameters:
    request - The request to log.
    response - The response to log. Note that for some requests the response instance may not have been fully populated (Eg 400 bad

request responses are sent without a servlet response object). Thus for basic log information it is best to consult Response.getCommittedMetaData() and Response.getHttpChannel() directly. See Also: RequestLog.log(Request, Response)


If you dont want to have a new line, then remove super.log(),

and do a custom print yourself:

 writerObj.write("logStringThatHasRequestAndResponseInformation here");
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • See this for getting specific request information you want, to print https://stackoverflow.com/questions/18944302/how-do-i-print-the-content-of-httprequest-request – JCompetence Aug 31 '21 at 12:42
  • Thanks but I am sure there has to be some standard jetty solution for it. I do not want to custom print for the request – Ashish Goyanka Aug 31 '21 at 16:24
  • If you don't want the behaviour of CustomRequestLog then don't use it, implement the RequestLog interface directly. You should also not store the Request as a field, instead pass it to the setCustomAttributesToLog method as an argument. – Lachlan Sep 09 '21 at 01:21