15

目前我有 logback 配置文件logback.xml ,即src/main/resources. 我想设置日志记录级别,但我想在战争之外进行控制。

我能想到的一种解决方案是将其外部化到属性文件中,在服务器启动时加载它并替换占位符。有没有更好的方法呢?我们不能保持logback.xml在战争档案之外吗?

    <configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- keep 7 days' worth of history -->
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>
</configuration>
4

5 回答 5

10

如果您只想更改不需要覆盖整个文件的日志级别,可以这样做:

<root level="${log.level:-INFO}">

然后,如果您设置系统属性:

-Dlog.level=DEBUG

它将覆盖默认的“信息”。

于 2013-02-18T15:56:28.553 回答
9

我建议你使用 2 个 logback 配置文件。类路径中的第一个定义为:

<configuration scan="true">
    <property file="/otherfolder/project.properties" />
    <include file="/otherfolder/logback.xml"/>
</configuration>

然后你把所有的配置放在另一个里面。Logback 将扫描这两个文件以获取更新。

于 2014-12-03T22:15:33.213 回答
6

外部属性是从外部控制日志记录级别的一种方式。Logback 的文件包含功能是另一个。顺便说一句,自动重新加载功能适用于包含的配置文件,但不适用于属性文件。

于 2013-01-12T00:09:36.707 回答
4

我已经设法用 tomcat/Spring logback 放置了一个外部配置文件

tomcat 的上下文文件:

<?xml version='1.0' encoding='utf-8'?>
<Context path="/appName">
<Environment name="pathCfg" value="${user.home}/applications/appName/cfg" type="java.lang.String" override="false" />
</Context>

网页.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/web-application-context.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Listener for external logback configuration file -->
<listener>
    <listener-class>com.package.client.servlet.LogbackConfiguratorContextListener</listener-class>
</listener>

web-application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- Configuration -->
<jee:jndi-lookup id="configurationPath" jndi-name="pathCfg"></jee:jndi-lookup>
<bean id="configurationPathResource" class="org.springframework.core.io.FileSystemResource">
    <constructor-arg ref="configurationPath" />
</bean>
<util:properties id="environnementProperties" location="file:${java:comp/env/pathCfg}/environnement.properties" />
</beans>

logback 配置器类:

package com.package.client.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;

import ch.qos.logback.classic.BasicConfigurator;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;

/**
 * Servlet context listener in order to use an external configuration file for logback.
 * 
 */
public class LogbackConfiguratorContextListener implements ServletContextListener
{

    private static final Logger LOGGER = LoggerFactory.getLogger(LogbackConfiguratorContextListener.class);

    /**
     * Logback configuration file uri.
     */
    @Value("#{environnementProperties['logback.configuration.file.uri']}")
    private String logbackConfigurationUri;

    /**
     * Configuration path resource for configuration files.
     */
    @Value("#{configurationPathResource}")
    private Resource configurationPathResource;

    /**
     * initialize logback with external configuration file.
     */
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent)
    {
        autowireThis(servletContextEvent.getServletContext());
        Resource logbackConfigurationResource = null;
        if (StringUtils.isNotBlank(logbackConfigurationUri))
        {
            if (StringUtils.containsAny(logbackConfigurationUri, new char[] { '/', '\\' }))
            {
                logbackConfigurationResource = new FileSystemResource(logbackConfigurationUri);
            }
            else if (configurationPathResource != null)
            {
                try
                {
                    logbackConfigurationResource = new FileSystemResource(new File(configurationPathResource.getFile(),
                            logbackConfigurationUri));
                }
                catch (IOException exception)
                {
                    LOGGER.error("Can't read resource " + configurationPathResource.getFilename(), exception);
                }
            }
        }
        if (logbackConfigurationResource != null)
        {
            if (logbackConfigurationResource.exists())
            {
                LOGGER.info("Found logback configuration " + logbackConfigurationResource.getDescription()
                        + " - Overriding default configuration");
                JoranConfigurator configurator = new JoranConfigurator();
                LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
                loggerContext.reset();
                configurator.setContext(loggerContext);
                try
                {
                    configurator.doConfigure(logbackConfigurationResource.getFile());
                    LOGGER.info("default configuration overrided by logback configuration "
                            + logbackConfigurationResource.getDescription());
                }
                catch (Exception exception)
                {
                    try
                    {
                        new ContextInitializer(loggerContext).autoConfig();
                    }
                    catch (JoranException e)
                    {
                        BasicConfigurator.configureDefaultContext();
                        LOGGER.error("Can't configure default configuration", exception);
                    }
                    LOGGER.error(
                            "Can't configure logback with specified " + logbackConfigurationResource.getDescription()
                                    + " - Keep default configuration", exception);
                }
            }
            else
            {
                LOGGER.error("Can't read logback configuration specified file at "
                        + logbackConfigurationResource.getDescription() + " - Keep default configuration");
            }
        }
        else
        {
            LOGGER.info("No logback configuration file specified - Keep default configuration");
        }
    }

    /**
     * Autowire this bean by spring
     * @param servletContext le contexte de la servlet
     */
    private void autowireThis(ServletContext servletContext)
    {
        WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getAutowireCapableBeanFactory()
                .autowireBean(this);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce)
    {
    }
}

最后是 environment.properties

# Configuration file
logback.configuration.file.uri=C:/Users/olejacques/applications/appName/cfg/log.xml
于 2013-06-10T09:13:24.517 回答
0

它对我们有用的一种方法是将配置文件外部化,如下所示。如果这不适用于您,请忽略弹簧型材部分。因此,一旦您在 中更新配置logback-include.xml,假设将根级别更改为 info,应用程序会在 30 秒内将其拾取(可配置)。

参考:https ://logback.qos.ch/manual/configuration.html#fileInclusion

web.xml
我们使用了 spring 配置文件,但即使没有它也可以工作。

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>
    <listener>
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>logbackConfigLocation</param-name>
        <param-value>classpath:properties/${spring.profiles.active}/logback.xml</param-value> <!-- with profiles, config file in classpath includes ext. file -->
<!--        <param-value>file:logback-ext-config-path/logback.xml</param-value> --> <!-- directly using external config -->
<!--        <param-value>classpath:logback.xml</param-value> --> <!-- without profiles -->
    </context-param>

logback.xml(在类路径中)
注意debug="true"配置更新时会告诉你的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
    <include file="${logback-ext-config-include-file-path}/logback-include.xml"/>
</configuration>

logback-include.xml (外部位置)注意使用<included>代替配置。不要错过这部分!

<included>
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${logfile-path}/log-file.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- daily rollover -->
                <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- keep 7 days' worth of history -->
                <maxHistory>7</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
            </encoder>
        </appender>

        <root level="DEBUG">
            <appender-ref ref="FILE"/>
        </root>
</included>
于 2017-09-26T19:23:33.637 回答