当前位置: 首页 > 知识库问答 >
问题:

在Jetty 10中使用自定义日志时,自定义属性登录到新行

谷永贞
2023-03-14
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() ;
    }

}

[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

我只想让他们在请求后打印在同一行。我们有什么办法做吗?

共有1个答案

祁绪
2023-03-14

您首先调用下面的命令,这将很好地格式化请求/响应,并将其记录为新行。

super.log(request, response);

公共无效日志(请求请求,响应响应)

将请求和响应信息写入输出流。

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
 writerObj.write("logStringThatHasRequestAndResponseInformation here");
 类似资料:
  • 将PostSharp用于C#应用程序,我有以下场景: Namespace_ACustomLoggingMethod Namespace_B.DoThings thingMethod(实际上是几种不同的方法) DoMomthingMethod调用CustomLoggingMethod,它以所需的格式创建日志条目并且运行良好。正如预期的那样,日志条目将源记录为CustomLoggingMethod,我

  • 请帮助我plz为Apache日志添加一个自定义部分,我希望在Apache access中获得以下代码片段的结果。日志,不仅在UI上: 现在我有以下的外出日志 (IP)-[24/Apr/2016:16:55:31 0000]“GET/tt.php?cmd=ls HTTP/1.1“200 331”-“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_10_5)AppleWebK

  • 我想有一个共享的应用程序insights实例,将保存来自不同微服务运行的所有日志。 或者说,共享应用程序insights实例并将所有日志和遥测都放在一堆中是不是一个坏主意?

  • 我的数据库中没有插入任何内容。所以我的第一个问题是为什么它不工作,请给我任何解决办法,如果可能的话。 另一个问题是,我将的intLevel调整为10,如下所示: 而现在它正在插入数据库,但也抛出了一些错误。 如有任何帮助,我们将不胜感激,并提前向您表示感谢。

  • 日志采集配置 在应用详情页中间有一个叫作“日志采集”的模块 点击右边的“添加”按钮,在弹出的对话框中选择日志的路径及正则规则 文件路径:你日志文件的位置 日志规则:如果没有特殊需求的话默认就好 提交后服务会自动重启动。 日志采集 如果配置了上面采集器,那么它会向服务所在的Pod注入一个Filebeat采集器对应用服务的业务日志进行采集。把采集到的日志入到kafka集群,然后logstash进行消息

  • 使用JBOSS 6.2 EAP作为应用服务器开发web应用程序。正在尝试自定义日志记录。要为指定位置的不同模块创建不同的日志。 做了一些家庭作业并尝试添加 在standalone的profile部分中。xml 并在同一文件中添加了记录器 此外,无法找到log4j。jboss文件夹中的xml。刚接触JBoss的人,猜一下遗漏了什么或走错了方向。有人能帮忙吗。