jboss 根据环境变量在启动时启用Wildfly json-formatter

vsaztqbk  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(68)

我想启用WildFly的json-formatter日志记录,这取决于服务器启动时环境变量的值。
当服务器已经启动并且我通过jboss-cli.sh连接到它时,以下CLI命令可以正常工作。但是在启动服务器时,我如何控制此配置是否处于活动状态?

if (result == "true") of :resolve-expression(expression=${env.JSON_FORMATTER_ENABLED})
    /subsystem=logging/json-formatter=JSON:add(pretty-print=false, exception-output-type=formatted)
    /subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value="JSON")
end-if

字符串

4nkexdtk

4nkexdtk1#

我不确定这是否是最好的解决方案,但至少这是我能想到的最好的解决方案。它似乎可以完成这项工作,但我想还有更优雅的解决方案。
我添加第二个console-handler,它使用json-formatter

# add separate console-handler with json-formatter
/subsystem=logging/json-formatter=JSON:add(pretty-print=false, exception-output-type=formatted)
/subsystem=logging/console-handler=CONSOLE-JSON:add(named-formatter="JSON", level=INFO)
/subsystem=logging/root-logger=ROOT:add-handler(name=CONSOLE-JSON)

字符串
然后,根据环境变量,我启用/禁用相应的console-handler,因为它似乎是唯一支持表达式的WildFly资源:

/subsystem=logging/console-handler=CONSOLE-JSON:write-attribute(name=enabled, value="${env.JSON_CONSOLE_HANDLER_ENABLED}"
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=enabled, value="${env.DEFAULT_CONSOLE_HANDLER_ENABLED}"


最后,在Docker启动脚本docker-entrypoint.sh中:

if [ "${JSON_FORMATTER_ENABLED}" == "true" ]; then
  export JSON_CONSOLE_HANDLER_ENABLED="true"
  export DEFAULT_CONSOLE_HANDLER_ENABLED="false"
else
  export JSON_CONSOLE_HANDLER_ENABLED="false"
  export DEFAULT_CONSOLE_HANDLER_ENABLED="true"
fi

相关问题