1

我使用 IBM Rational Application Developer (RAD) 创建了一个动态 Web 项目。我使用 Logback 作为日志框架。我将 logback.xml 放在 WEB-INF/classes 中。但应用程序不会拾取此配置文件。日志信息记录在控制台中。但我希望这会登录一个文件。请参阅下面的 logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>myApp.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>myApp.%i.log.zip</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>2MB</MaxFileSize>
        </triggeringPolicy>

    </appender>

    <logger name="com.nyl.ltc.logging.handler" level="ALL" />

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>

</configuration>

我将以下罐子放在 WEB-INF/lib

  1. logback-classic-1.0.11.jar
  2. logback-core-1.0.11.jar
  3. slf4j-api-1.7.5.jar

我正在使用 WebSphere Application Server v7.0。

请帮我解决这个问题。

注意:我在 RAD 的内部浏览器中运行该应用程序。

4

3 回答 3

1

尝试在 websphere 中设置 jvm 系统属性:

logback.configurationFile=/tmp/logback.xml

于 2013-08-13T08:32:22.980 回答
0

你可以使用 ServletContextListener 并将 logback 设置为上下文。

web.xml

<context-param>
    <param-name>logbackLocation</param-name>
    <param-value>/WEB-INF/classes/logback.xml</param-value>
</context-param>

<listener>
    <listener-class>com.listener.LogbackListener</listener-class>
</listener>

回溯监听器

package com.listener;

import java.io.*;
import java.net.URL;
import javax.servlet.*;
import org.slf4j.*
import ch.qos.logback.*;

public class LogbackListener implements ServletContextListener {
    public static final String CONFIG_LOCATION = "logbackLocation";

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        String location = servletContext.getInitParameter(CONFIG_LOCATION);
        // check location

        InputStream inputStream = openInputStream(servletContext, location);
        // check inputStream
        try {
            configureLogback(/*params*/);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                 servletContext.log("Could not close logback config inputstream.", e);
            }
        }
    }

    private InputStream openInputStream(ServletContext servletContext, String location) {
        InputStream inputStream = null;
        if (location == null)
            return inputStream;
        if (location.startsWith("/"))
            inputStream = servletContext.getResourceAsStream(location);
        else
            try {
                inputStream = new URL(location).openStream();
            } catch (IOException e) {
            //ommited
            }
        if (inputStream == null)
            try {
                inputStream = new FileInputStream(location);
            } catch (FileNotFoundException e) {
            //ommited
            }
        return inputStream;
    }

    private void configureLogback(/*params*/) {
        //implementation ommited
    }

     public void contextDestroyed(ServletContextEvent sce) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.stop();
    }
}
于 2013-04-09T14:19:51.237 回答
0

将共享库添加到 Websphere 管理控制台,到您的 logback.xml

于 2019-08-16T10:57:29.030 回答