2023年7月17日发(作者:)
关于log4j2⽣成json格式的⽇志⽬录⽬的参考这篇⽂章
我们已经知道 Log4j2 能够输出 json 格式的⽇志,但还是没有很好的⽀持 ELK 的需求,打印出来的每⼀条⽇志是多⾏显⽰的,那么该如何配置具体的格式呢?请往下看。⽅法参照 Log4j 官⽹的说明:@Deprecatedpublic static JsonLayout createLayout(Configuration config,boolean locationInfo,boolean properties,boolean propertiesAsList,boolean complete,boolean compact,boolean eventEol,String headerPattern,String footerPattern,Charset charset,boolean includeStacktrace)Deprecated. Use newBuilder() insteadCreates a JSON ters:config - The plugin onInfo - If “true”, includes the location information in the generated ties - If “true”, includes the thread context map in the generated tiesAsList - If true, the thread context map is included as a list of map entry objects, where each entry has a“key” attribute (whose value is the key) and a “value” attribute (whose value is the value). Defaults to false, inwhich case the thread context map is included as a simple map of key-value te - If “true”, includes the JSON header and footer, and comma between t - If “true”, does not use end-of-lines and indentation, defaults to “false”.eventEol - If “true”, forces an EOL after each log event (even if compact is “true”), defaults to “false”. Thisallows one even per line, even in compact Pattern - The header pattern, defaults to “[” if Pattern - The header pattern, defaults to “]” if t - The character set to use, if null, uses “UTF-8”.includeStacktrace - If “true”, includes the stacktrace of any Throwable in the generated JSON, defaults to “true”.Returns:A JSON Layout.参数说明部分翻译过来是:config - 插件配置。locationInfo - 如果为“true”,则在⽣成的 JSON 中包含位置信息。properties - 如果为“true”,则在⽣成的 JSON 中包含线程上下⽂映射。propertiesAsList - 如果为 true,则将线程上下⽂映射包括为映射条⽬对象的列表,其中每个条⽬具有“key”属性(其值为键)和“value”属性(其值为值)。默认为 false,在这种情况下,线程上下⽂映射包含为键值对的简单映射。complete - 如果为“true”,则包括 JSON 页眉和页脚,以及记录之间的逗号。compact - 如果为“true”,则不使⽤⾏尾和缩进,默认为“false”。eventEol - 如果为“true”,则在每个⽇志事件后强制执⾏ EOL(即使compact为“true”),默认为“false”。即使在紧凑模式下,这也允许⼀⾏甚⾄每⼀⾏。headerPattern- 标题模式,默认为"[“如果是 null。footerPattern- 标题模式,默认为”]"如果是 null。charset- 要使⽤的字符集if null,使⽤“UTF-8”。includeStacktrace - 如果为“true”,则包括⽣成的 JSON 中任何Throwable的堆栈跟踪,默认为“true”。好了,可以知道,满⾜每条⽇志输出为⼀⾏ json 的需求我们该这样设置:完整的 配置如下: 输出的⽇志内容为:{"thread":"main","level":"INFO","loggerName":"aEsApiApplication","message":"Starting BigdataEsApiApplication on DESKTOP-1HMKQ6H with PID 12316 (started by HopeRunner in E:SVNHoperun_BigData实验bigdata-es-api)","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386053,"nanoOfSecond":854000000},"threadId":1,"threadPriority":5,"source":{"pter$Log4jLog","instant":{"epochSecond":1555386053,"nanoOfSecond":854000000},"threadId":1,"threadPriority":5,"source":{"class":"pInfoLogger","method":"logStarting","file":"","line":50}}{"thread":"main","level":"INFO","loggerName":"aEsApiApplication","message":"No active profile set, falling back to default profiles: default","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386053,"nanoOfSecond":925000000},"threadId":1,"threadPriority":5,"source":{"class":"Application","method":"logStartupProfileInfo","file":"","line":675}}{"thread":"main","level":"INFO","loggerName":"toryConfigurationDelegate","message":"Bootstrapping Spring Data repositories in DEFAULT mode.","endOfBatch":false,"loggerFqcn":"4jLogger","instant":{"epochSecond":1555386054,"nanoOfSecond":298000000},"threadId":1,"threadPriority":5,"source":{"class":"toryConfigurationDelegate","method":"registerRepositoriesIn","file":"","line":126}}{"thread":"main","level":"INFO","loggerName":"toryConfigurationDelegate","message":"Finished Spring Data repository scanning in 49ms. Found 3 repository interfaces.","endOfBatch":false,"loggerFqcn":"4jLogger","instant":{"epochSecond":1555386054,"nanoOfSecond":350000000},"threadId":1,"threadPriority":5,"source":{"class":"toryConfigurationDelegate","method":"registerRepositoriesIn","file":"","line":182}}{"thread":"main","level":"INFO","loggerName":"WebServer","message":"Tomcat initialized with port(s): 19999 (http)","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386054,"nanoOfSecond":715000000},"threadId":1,"threadPriority":5,"source":{"class":"WebServer","method":"initialize","file":"","line":90}}{"thread":"main","level":"INFO","loggerName":"11NioProtocol","message":"Initializing ProtocolHandler ["http-nio-19999"]","endOfBatch":false,"loggerFqcn":"","instant":{"epochSecond":1555386054,"nanoOfSecond":730000000},"threadId":1,"threadPriority":5,"source":{"class":"JDKLog","method":"log","file":"","line":173}}{"thread":"main","level":"INFO","loggerName":"rdService","message":"Starting service [Tomcat]","endOfBatch":false,"loggerFqcn":"","instant":{"epochSecond":1555386054,"nanoOfSecond":737000000},"threadId":1,"threadPriority":5,"source":{"class":"JDKLog","method":"log","file":"","line":173}}{"thread":"main","level":"INFO","loggerName":"rdEngine","message":"Starting Servlet engine: [Apache Tomcat/9.0.17]","endOfBatch":false,"loggerFqcn":"","instant":{"epochSecond":1555386054,"nanoOfSecond":738000000},"threadId":1,"threadPriority":5,"source":{"class":"JDKLog","method":"log","file":"","line":173}}{"thread":"main","level":"INFO","loggerName":"nerBase.[Tomcat].[localhost].[/]","message":"Initializing Spring embedded WebApplicationContext","endOfBatch":false,"loggerFqcn":"","instant":{"epochSecond":1555386054,"nanoOfSecond":927000000},"threadId":1,"threadPriority":5,"source":{"class":"JDKLog","method":"log","file":"","line":173}}{"thread":"main","level":"INFO","loggerName":"tLoader","message":"Root WebApplicationContext: initialization completed in 974 ms","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386054,"nanoOfSecond":928000000},"threadId":1,"threadPriority":5,"source":{"class":"tWebServerApplicationContext","method":"prepareWebApplicationContext","file":"","line":296}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"no modules loaded","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":182000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":181}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"loaded plugin [xPlugin]","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":188000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":184}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"loaded plugin [JoinPlugin]","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":189000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":184}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"loaded plugin [atorPlugin]","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":189000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":184}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"loaded plugin [hePlugin]","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":189000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":184}}{"thread":"main","level":"INFO","loggerName":"sService","marker":{"name":"[_client_] "},"message":"loaded plugin [4Plugin]","endOfBatch":false,"loggerFqcn":"ctLogger","instant":{"epochSecond":1555386055,"nanoOfSecond":190000000},"threadId":1,"threadPriority":5,"source":{"class":"sService","method":"logPluginInfo","file":"","line":184}}{"thread":"main","level":"INFO","loggerName":"PoolTaskExecutor","message":"Initializing ExecutorService 'applicationTaskExecutor'","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386057,"nanoOfSecond":714000000},"threadId":1,"threadPriority":5,"source":{"class":"orConfigurationSupport","method":"initialize","file":"","line":171}}{"thread":"main","level":"INFO","loggerName":"11NioProtocol","message":"Starting ProtocolHandler ["http-nio-19999"]","endOfBatch":false,"loggerFqcn":"","instant":{"epochSecond":1555386058,"nanoOfSecond":44000000},"threadId":1,"threadPriority":5,"source":{"class":"JDKLog","method":"log","file":"","line":173}}{"thread":"main","level":"INFO","loggerName":"WebServer","message":"Tomcat started on port(s): 19999 (http) with context path ''","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":155538 19999 (http) with context path ''","endOfBatch":false,"loggerFqcn":"pter$Log4jLog","instant":{"epochSecond":1555386058,"nanoOfSecond":55000000},"threadId":1,"threadPriority":5,"source":{"class":"WebServer","method":"start","file":"","line":204}}结语本⽚⽂章是在 SpringBoot ⼯程基础上对 Log4j 配置的说明,希望能够给⼤家提供帮助。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689542226a264707.html
评论列表(0条)