PatternLayoutEncoder
接受的模式语法部分在the "layouts documentation中指定,其中
在PatternLayout中,括号可用于对转换模式进行分组。因此,'('和')'具有特殊意义,如果打算用作文字,则需要进行转义。
...
%character具有特殊含义...为了将其包含为文字,需要使用反斜杠进行转义,例如: "%d%p \%%m%n"。
但是,它没有说明字符{
和}
。我希望编写一个模式,输出一个文字的开括号字符。为此,我尝试了logback.xml
:
<encoder>
<pattern>{</pattern>
</encoder>
在解析配置文件时,这会导致Logback失败并显示NullPointerException
。
然后,通过类比文字字符%
,(
和)
的转义字符,我尝试过:
<encoder>
<pattern>\{</pattern>
</encoder>
这会导致Logback以相同的方式失败。
据推测,NullPointerException
是Logback中的一个错误。我如何解决它并输出文字括号字符?
编辑:这里是完整的堆栈跟踪。
17:44:38,953 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.NullPointerException
at ch.qos.logback.core.subst.Parser.T(Parser.java:77)
at ch.qos.logback.core.subst.Parser.E(Parser.java:52)
at ch.qos.logback.core.subst.Parser.C(Parser.java:122)
at ch.qos.logback.core.subst.Parser.T(Parser.java:83)
at ch.qos.logback.core.subst.Parser.E(Parser.java:52)
at ch.qos.logback.core.subst.Parser.parse(Parser.java:48)
at ch.qos.logback.core.subst.NodeToStringTransformer.tokenizeAndParseString(NodeToStringTransformer.java:55)
at ch.qos.logback.core.subst.NodeToStringTransformer.substituteVariable(NodeToStringTransformer.java:46)
at ch.qos.logback.core.util.OptionHelper.substVars(OptionHelper.java:117)
at ch.qos.logback.core.joran.spi.InterpretationContext.subst(InterpretationContext.java:159)
at ch.qos.logback.core.joran.action.NestedBasicPropertyIA.body(NestedBasicPropertyIA.java:87)
at ch.qos.logback.core.joran.spi.Interpreter.callBodyAction(Interpreter.java:295)
at ch.qos.logback.core.joran.spi.Interpreter.characters(Interpreter.java:175)
at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:57)
at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149)
at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135)
at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:269)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1740)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
答案 0 :(得分:0)
尝试使用&nbsp; {&#39;
的HTML unicode字符{
答案 1 :(得分:0)
您可以尝试将单引号/双引号放在'{'之前。对我来说,它解决了一个问题,但提出了另一个问题。我试图使用PatternLayout生成JSON。我可以成功生成用引号引起来的JSON。但是,JSON解析器开始出现异常。如果您也尝试生成JSON并且不能使用附加引号,则可以尝试JsonLayout。在https://www.baeldung.com/java-log-json-output
中提到了解决方案我尝试过的配置
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>'{"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}", "level":"%p", "classname":"%c", "logmsg":"%m'}</pattern>
</layout>
</appender>
它在日志下方生成
'{"timestamp":"2018-08-21T15:18:49.998Z", "level":"INFO", "classname":"com.myproject.log4jTest.logbackTest", "logmsg":"Hello world."}'