2017-01-22 14 views
2

私はロンボクのlog4j2ロガーを使用しており、ThreadContextマップに基づいてルーティングアペンダーを設定する必要があります。ルートキーはスクリプトで決定されます。ここでは全体log4j2.xml設定ファイルされる:Log4j2ルーティングアペンダーJavaScript設定エラー

<Appenders> 
    <Console name="Console" target="SYSTEM_OUT"> 
     <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable}"/> 
    </Console> 

    <RollingFile name="GeneralRollingFile" filename="log/test-log.log" 
       filepattern="log/test-log-%d{yyyy-MM-dd HH:mm:ss}.log"> 
     <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/> 
     <Policies> 
      <SizeBasedTriggeringPolicy size="25 MB"/> 
     </Policies> 
     <DefaultRolloverStrategy max="20"/> 
    </RollingFile> 

    <Routing name="Routing"> 

     <Routes> 
      <Script name="RoutingInit" language="JavaScript"> 
       <![CDATA[ 
       if (logEvent.getContextMap().containsKey("operation-1")) { 
        return "operation-1"; 
       } else if (logEvent.getContextMap().containsKey("operation-2")) { 
        return "operation-2"; 
       } else { 
        return "general"; 
       } 
      ]]> 
      </Script> 

      <Route key="general" ref="GeneralRollingFile"/> 

      <Route key="operation-1"> 
       <RollingFile name="operation-1-rolling" 
          fileName="log/operation-1/${ctx:operation-1}.log" 
          filePattern="log/operation-1/${ctx:operation-1}-%d{yyyy-MM-dd HH:mm:ss}.log"> 

        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/> 
        <Policies> 
         <SizeBasedTriggeringPolicy size="25 MB"/> 
        </Policies> 
        <DefaultRolloverStrategy max="20"/> 
       </RollingFile> 
      </Route> 

      <Route key="operation-2"> 
       <RollingFile name="operation-2-rolling" 
          fileName="log/operation-2/${ctx:operation-2}.log" 
          filePattern="log/operation-2/${ctx:operation-2}-%d{yyyy-MM-dd HH:mm:ss}.log"> 

        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %c{1}: %msg%n%throwable"/> 
        <Policies> 
         <SizeBasedTriggeringPolicy size="25 MB"/> 
        </Policies> 
        <DefaultRolloverStrategy max="20"/> 
       </RollingFile> 
      </Route> 

     </Routes> 


    </Routing> 

</Appenders> 

<Loggers> 
    <Root level="trace" additivity="false"> 
     <AppenderRef ref="Console"/> 
     <AppenderRef ref="Routing"/> 
    </Root> 
</Loggers> 

しかし、私は次のエラーを取得しています:

main ERROR Error running script RoutingInit javax.script.ScriptException: <eval>:2:24 Invalid return statement 
        return "operation-1"; 

Log4j2のドキュメントは私たちに似たスクリプトの例を示します、それは私のエーテルのために働かない。 私はJSにかなり新しいですが、このコードは有効なスクリプトのようです。何か間違っていますか?

ありがとうございます。

答えて

2

(Diclaimer:私はlog4j2を使用したことがない、また、あなたの質問に答えるためにしようとしたときとにかく、私は周りを見回したなど、JavaでJavascriptを埋め込む...)

はない、それはhttps://stackoverflow.com/a/38034571/118587に似ていますか?

私が正しく理解しているのは、それが関数ではない場合はreturnを使用してはならないということです。返される値は最後の式の値になります。次のように推測し、次のように動作します:

 <Script name="RoutingInit" language="JavaScript"> 
      <![CDATA[ 
      var ret = "general"; 
      if (logEvent.getContextMap().containsKey("operation-1")) { 
       ret = "operation-1"; 
      } else if (logEvent.getContextMap().containsKey("operation-2")) { 
       ret = "operation-2"; 
      } 
      ret; 
     ]]> 
     </Script> 
+0

ありがとう。ソリューションのアプローチは似ていました。単にリターンを取り除くだけです。 – void

関連する問題