gpt4 book ai didi

json - 在一行中使用 JSONLayout 在 JSON 中使用 Log4j2 打印堆栈跟踪

转载 作者:行者123 更新时间:2023-12-04 15:12:43 25 4
gpt4 key购买 nike

我正在尝试使用 Log4j2 配置 JsonLayaout。我想在 JSON 中的一条消息中包含堆栈跟踪。我知道我必须使用属性“完整”。

我创建的示例类是:

public class MyApp {

private static final Logger logger = LogManager.getLogger(MyApp.class);

public static void main(final String... args) {
logger.debug("Entering application.");

try{
int i = 0;
int a = 5 / i;
}catch(Exception e){
//StringWriter sw = new StringWriter();
//e.printStackTrace(new PrintWriter(sw));
//String exceptionAsString = sw.toString();
logger.error(exceptionAsString);
}
}
}

log4j2.properties 是:
name=PropertiesConfig
property.filename = logs
appenders = file, console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = JSONLayout
appender.console.layout.complete="false"
#appender.console.layout.compact = true
#appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/var/tmp/propertieslogs.log
appender.file.layout.type=JSONLayout
appender.file.layout.complete="true"
#appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout, logfile
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.logfile.ref = LOGFILE

我想要的输出:
[
{
"logger":"com.foo.Bar",
"timestamp":"1376681196470",
"level":"INFO",
"threadId":1,
"thread":"main",
"threadPriority":1,
"message":"Message flushed with immediate flush=true"
},
{
"logger":"com.foo.Bar",
"timestamp":"1376681196471",
"level":"ERROR",
"threadId":1,
"thread":"main",
"threadPriority":1,
"message":"Message flushed with immediate flush=true",
"throwable":"java.lang.IllegalArgumentException: badarg\\n\\tat org.apache.logging.log4j.core.appender.JSONCompleteFileAppenderTest.testFlushAtEndOfBatch(JSONCompleteFileAppenderTest.java:54)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\\n\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n\\tat java.lang.reflect.Method.invoke(Method.java:606)\\n\\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)\\n\\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)\\n\\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)\\n\\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)\\n\\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\\n\\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)\\n\\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)\\n\\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)\\n\\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)\\n\\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)\\n\\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\\n\\tat org.junit.runners.ParentRunner.run(ParentRunner.java:309)\\n\\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\\n\\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\\n"
}
]

但我得到:
{
"timeMillis" : 1474362834903,
"thread" : "main",
"level" : "ERROR",
"loggerName" : "test.MyApp",
"message" : "Exception:",
"thrown" : {
"commonElementCount" : 0,
"localizedMessage" : "/ by zero",
"message" : "/ by zero",
"name" : "java.lang.ArithmeticException",
"extendedStackTrace" : [ {
"class" : "test.MyApp",
"method" : "main",
"file" : "MyApp.java",
"line" : 24,
"exact" : true,
"location" : "classes/",
"version" : "?"
}, {
"class" : "sun.reflect.NativeMethodAccessorImpl",
"method" : "invoke0",
"file" : "NativeMethodAccessorImpl.java",
"line" : -2,
"exact" : false,
"location" : "?",
"version" : "1.8.0_91"
}, {
"class" : "sun.reflect.NativeMethodAccessorImpl",
"method" : "invoke",
"file" : "NativeMethodAccessorImpl.java",
"line" : 62,
"exact" : false,
"location" : "?",
"version" : "1.8.0_91"
}, {
"class" : "sun.reflect.DelegatingMethodAccessorImpl",
"method" : "invoke",
"file" : "DelegatingMethodAccessorImpl.java",
"line" : 43,
"exact" : false,
"location" : "?",
"version" : "1.8.0_91"
}, {
"class" : "java.lang.reflect.Method",
"method" : "invoke",
"file" : "Method.java",
"line" : 498,
"exact" : false,
"location" : "?",
"version" : "1.8.0_91"
}, {
"class" : "com.intellij.rt.execution.application.AppMain",
"method" : "main",
"file" : "AppMain.java",
"line" : 147,
"exact" : true,
"location" : "idea_rt.jar",
"version" : "?"
} ]
},
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 1,
"threadPriority" : 5
}

我应该像在代码中那样为消息构建一个字符串,还是如何使用所有堆栈跟踪完成日志中的 throwable 属性?

如果我使用此代码:
   public static void main(final String... args) {

// Set up a simple configuration that logs on the console.
logger.debug("Entering application.");

try{
int i = 0;
int a = 5 / i;
}catch(Exception e){
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();

logger.error(exceptionAsString);
}
}
}

我几乎得到了我想要的:
{
"timeMillis" : 1474542115319,
"thread" : "main",
"level" : "DEBUG",
"loggerName" : "MyApp",
"message" : "Entering application.",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"contextMap" : [ ]
}{
"timeMillis" : 1474542115573,
"thread" : "main",
"level" : "ERROR",
"loggerName" : "MyApp",
"message" : "java.lang.ArithmeticException: / by zero\n\tat MyApp.main(MyApp.java:23)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)\n",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"contextMap" : [ ]
}

但是,我想我可以在不将堆栈跟踪解析为字符串的情况下自动完成。

最佳答案

<JsonLayout compact="true" eventEol="true" stacktraceAsString="true" >

这会有所帮助。

关于json - 在一行中使用 JSONLayout 在 JSON 中使用 Log4j2 打印堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com