【问题标题】:websphere liberty - Not able to generate application specific logging using slf4j or log4jwebsphere liberty - 无法使用 slf4j 或 log4j 生成特定于应用程序的日志记录
【发布时间】:2019-08-13 12:21:53
【问题描述】:

如何在 IBM Websphare Liberty 服务器中生成特定于应用程序的日志,我使用的是 SLF4j,message.log 和 console.log 更新正常,但未生成应用程序特定日志。 如果日志记录问题可以使用 Log4j 解决,那么也对我有用。

尝试在静态块中显式加载 log4j2 文件并将其放置在资源文件夹中,均无效。

能够查看 liberty 服务器日志,但根本不生成应用程序日志。

【问题讨论】:

标签: java log4j slf4j websphere-liberty


【解决方案1】:

看不到 log4j 日志的根本原因通常是因为类加载器没有拾取 log4j2 配置文件。这里有几个选项可以解决您的问题。

  1. 复制到 Liberty 共享库目录。它可以是以下之一:
    • ${shared.config.dir}/lib/global
    • ${server.config.dir}/lib/global

您可以参考 IBM 网站以找出您的 Liberty 安装中 ${shared.config.dir} 和 ${server.config.dir} 的确切位置 at here

  1. 或者,您可以将 log4j 配置文件放在文件系统上的任何位置,然后将以下行添加到 server.xml
    <library id="log4jConfig">
          <folder dir="/{directory containning log4j config file}" scanInterval="5s" />
    </library>

    <webApplication id="myapp" location="myapp.war" name="My App"/>
          <classloader commonLibraryRef="log4jConfig"/>
    </webApplication>
  1. 在 jvm.options 文件中设置为 JVM 参数 -Dlog4j.configurationFile=file:/path/to/log4j2.xml

  2. 将其打包到 src/main/resources 下的 maven 应用程序 war 文件中

【讨论】:

  • Blow 修复对我有用,从记录器类使用下面的代码重新加载 slf4j 配置
【解决方案2】:

Blow 修复对我有用,从应用程序类或入口点使用下面的代码重新加载 slf4j 配置。

私有静态最终 org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(theClassfromwheregeneratinglogs.class);

static {
    try {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    // Call context.reset() to clear any previous configuration, e.g. default 
    // configuration. For multi-step configuration, omit calling context.reset().
    context.reset(); 
    configurator.doConfigure("/appserver/wlpusr/servers/applogs/logback.xml"); //This will reload the slf4j configuration from the given file location.     
  } catch (JoranException je) {
    // StatusPrinter will handle this
  }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2018-10-07
    • 2012-01-31
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多