当前位置: 首页 > 知识库问答 >
问题:

log4j2如何记录一个包?

皇甫展
2023-03-14

我需要支持将特定包登录到聊天中。将错误记录到错误中。日志如何做到这一点?到目前为止,我在几个appender中创建了阈值过滤器,但出现了错误。日志保持为空并进行聊天。日志有一个条目,它不属于特定的包。毕竟,我仔细阅读了log4j的文档,也尝试了2天来解决这个问题,并在网站和youtube上学习了一些教程。我还尝试了以下方法:使用Log4j2进行apachelog4j2包特定的日志记录。xml我也尝试了旧的log4j教程,但最终还是出现了错误。

这些是我在maven项目结构中使用的文件。src/main/resources/log4j2。性质

status = error
name = PropertiesConfig

######################################
# Global properties as varies
######################################
property.logPath = logs

######################################
# Filters and properties of filters
######################################
filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = all

######################################
# Appenders Section for roles
######################################
appenders = rolling, console, rolling2, errorAppender, service

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.immediateFlush= true
appender.rolling.append = true
appender.rolling.fileName = ${logPath}/server.log
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
appender.rolling.filter.threshold.type = LevelRangeFilter
appender.rolling.filter.threshold.minLevel = INFO
appender.rolling.filter.threshold.maxLevel = INFO
appender.rolling.filter.threshold.onMatch = ACCEPT
appender.rolling.filter.threshold.onMismatch = DENY

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling2.type = RollingFile
appender.rolling2.name = debugLog
appender.rolling2.immediateFlush= true
appender.rolling2.append = true
appender.rolling2.fileName = ${logPath}/debug.log
appender.rolling2.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling2.layout.type = PatternLayout
appender.rolling2.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling2.policies.type = Policies
appender.rolling2.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling2.policies.time.interval = 1
appender.rolling2.policies.time.modulate = true
appender.rolling2.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling2.policies.size.size=10MB
appender.rolling2.strategy.type = DefaultRolloverStrategy
appender.rolling2.strategy.max = 20
appender.rolling2.filter.threshold.type = LevelRangeFilter
appender.rolling2.filter.threshold.minLevel = DEBUG
appender.rolling2.filter.threshold.maxLevel = DEBUG
appender.rolling2.filter.threshold.onMatch = ACCEPT
appender.rolling2.filter.threshold.onMismatch = DENY

appender.errorAppender.type = RollingFile
appender.errorAppender.name = errorLog
appender.errorAppender.immediateFlush= true
appender.errorAppender.append = true
appender.errorAppender.fileName = ${logPath}/error.log
appender.errorAppender.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.errorAppender.layout.type = PatternLayout
appender.errorAppender.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.errorAppender.policies.type = Policies
appender.errorAppender.policies.time.type = TimeBasedTriggeringPolicy
appender.errorAppender.policies.time.interval = 1
appender.errorAppender.policies.time.modulate = true
appender.errorAppender.policies.size.type = SizeBasedTriggeringPolicy
appender.errorAppender.policies.size.size=10MB
appender.errorAppender.strategy.type = DefaultRolloverStrategy
appender.errorAppender.strategy.max = 20
appender.errorAppender.filter.threshold.type = LevelRangeFilter
appender.errorAppender.filter.threshold.minLevel = ERROR
appender.errorAppender.filter.threshold.maxLevel = ERROR
appender.errorAppender.filter.threshold.onMatch = ACCEPT
appender.errorAppender.filter.threshold.onMismatch = DENY

appender.service.type = RollingFile
appender.service.name = chats
appender.service.append = true
appender.service.fileName = ${logPath}/chats.log
appender.service.filePattern = chats-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.service.layout.type = PatternLayout
appender.service.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.service.policies.type = Policies
appender.service.policies.time.type = TimeBasedTriggeringPolicy
appender.service.policies.time.interval = 1
appender.service.policies.time.modulate = true
appender.service.policies.size.type = SizeBasedTriggeringPolicy
appender.service.policies.size.size=10MB
appender.service.strategy.type = DefaultRolloverStrategy
appender.service.strategy.max = 20
appender.service.filter.threshold.type = LevelRangeFilter
appender.service.filter.threshold.minLevel = INFO
appender.service.filter.threshold.maxLevel = INFO
appender.service.filter.threshold.onMatch = ACCEPT
appender.service.filter.threshold.onMismatch = DENY

######################################
# Loggers Section for file write
######################################
loggers = rolling, package

logger.rolling.name = org.remad.logging
logger.rolling.appenderRefs = rolling, debugLog, errorLog
logger.rolling.level = all
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref = RollingFile
logger.rolling.appenderRef.rolling2.ref = debugLog

logger.package.name = org.remad.logging.protocol.Protocol
logger.package.appenderRefs = service
logger.package.level = info
logger.package.additivity = false
logger.package.appenderRef.service.ref = chats

######################################
# RootLogger Section for settings
######################################
rootLogger.level = all
rootLogger.appenderRefs = stdout, chats, errorLog
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.service.ref = chats
rootLogger.apenderRef.errorAppender.ref = errorLog

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.remad</groupId>
    <artifactId>Logging</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- ====================================================================== -->
    <!-- Global project properties                                              -->
    <!-- ====================================================================== -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- ====================================================================== -->
    <!-- project Dependencies                                                   -->
    <!-- ====================================================================== -->
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.13.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.13.2</version>
        </dependency>
    </dependencies>

    <!-- ====================================================================== -->
    <!-- project build with plugins                                             -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.2</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.remad.logging.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

src/main/org。雷马德。登录中。主要的JAVA

package org.remad.logging;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.remad.logging.protocol.Protocol;

public class Main {

    private static final Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        String message = "Hello there!";
        LoggerEntryPoint loggerEntryPoint = new LoggerEntryPoint();
        Protocol protocol = new Protocol();
        logger.trace(message);
        logger.debug(message);
        logger.info(message);
        logger.warn(message);
        logger.error(message);
        logger.fatal(message);
        loggerEntryPoint.entries();
        protocol.logSomeMessages();
    }
}

组织。雷马德。登录中。LoggerEntryPoint。JAVA

package org.remad.logging;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LoggerEntryPoint {

    private static final Logger logger = LogManager.getLogger(LoggerEntryPoint.class);

    public LoggerEntryPoint() {}

    public void entries() {
        logger.info("now in " + getClass().getCanonicalName());
        logger.error("now error.");
        logger.debug("hggxdfghcdft");
    }
}

org.remad.logging.protocol.Protocol.java

package org.remad.logging.protocol;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Protocol {

    public Protocol() {}

    private static final Logger logger = LogManager.getLogger(Protocol.class);

    public void logSomeMessages() {
        logger.fatal("Fatal error.");
        logger.error("Error happened.");
        logger.warn("Warning message.");
        logger.info("This message informs.");
        logger.debug("Debug message.");
        logger.info("This is also chat message");
    }
}

日志/服务器。日志

2020-05-18 17:24:42 INFO  Main:17 - Hello there!
2020-05-18 17:24:42 INFO  LoggerEntryPoint:13 - now in org.remad.logging.LoggerEntryPoint

日志/error.log


日志/debug.log

2020-05-18 17:24:42 DEBUG Main:16 - Hello there!
2020-05-18 17:24:42 DEBUG LoggerEntryPoint:15 - hggxdfghcdft

日志/聊天。日志

2020-05-18 17:24:42 INFO  Main:17 - Hello there!
2020-05-18 17:24:42 INFO  LoggerEntryPoint:13 - now in org.remad.logging.LoggerEntryPoint
2020-05-18 17:24:42 INFO  Protocol:16 - This message informs.
2020-05-18 17:24:42 INFO  Protocol:18 - This is also chat message

共有2个答案

萧阳波
2023-03-14

这很有效。

status = error
name = PropertiesConfig

######################################
# Global properties as varies
######################################
property.logPath = logs

######################################
# Appenders Section for log-files
######################################
appenders = rolling, console, rolling2, errorAppender, service

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.immediateFlush= true
appender.rolling.append = true
appender.rolling.fileName = ${logPath}/server.log
appender.rolling.filePattern = server-log-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
appender.rolling.filter.threshold.type = LevelRangeFilter
appender.rolling.filter.threshold.minLevel = info
appender.rolling.filter.threshold.maxLevel = info
appender.rolling.filter.threshold.onMatch = ACCEPT
appender.rolling.filter.threshold.onMismatch = DENY

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling2.type = RollingFile
appender.rolling2.name = debugLog
appender.rolling2.immediateFlush= true
appender.rolling2.append = true
appender.rolling2.fileName = ${logPath}/debug.log
appender.rolling2.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling2.layout.type = PatternLayout
appender.rolling2.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling2.policies.type = Policies
appender.rolling2.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling2.policies.time.interval = 1
appender.rolling2.policies.time.modulate = true
appender.rolling2.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling2.policies.size.size=10MB
appender.rolling2.strategy.type = DefaultRolloverStrategy
appender.rolling2.strategy.max = 20
appender.rolling2.filter.threshold.type = LevelRangeFilter
appender.rolling2.filter.threshold.minLevel = DEBUG
appender.rolling2.filter.threshold.maxLevel = DEBUG
appender.rolling2.filter.threshold.onMatch = ACCEPT
appender.rolling2.filter.threshold.onMismatch = DENY

appender.errorAppender.type = RollingFile
appender.errorAppender.name = errorLog
appender.errorAppender.immediateFlush= true
appender.errorAppender.append = true
appender.errorAppender.fileName = ${logPath}/error.log
appender.errorAppender.filePattern = error-log-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.errorAppender.layout.type = PatternLayout
appender.errorAppender.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.errorAppender.policies.type = Policies
appender.errorAppender.policies.time.type = TimeBasedTriggeringPolicy
appender.errorAppender.policies.time.interval = 1
appender.errorAppender.policies.time.modulate = true
appender.errorAppender.policies.size.type = SizeBasedTriggeringPolicy
appender.errorAppender.policies.size.size=10MB
appender.errorAppender.strategy.type = DefaultRolloverStrategy
appender.errorAppender.strategy.max = 20
appender.errorAppender.filter.threshold.type = ThresholdFilter
appender.errorAppender.filter.threshold.level = warn

appender.service.type = RollingFile
appender.service.name = chats
appender.service.append = true
appender.service.fileName = ${logPath}/chats.log
appender.service.filePattern = chats-log-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.service.layout.type = PatternLayout
appender.service.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.service.policies.type = Policies
appender.service.policies.time.type = TimeBasedTriggeringPolicy
appender.service.policies.time.interval = 1
appender.service.policies.time.modulate = true
appender.service.policies.size.type = SizeBasedTriggeringPolicy
appender.service.policies.size.size=10MB
appender.service.strategy.type = DefaultRolloverStrategy
appender.service.strategy.max = 20
appender.service.filter.threshold.type = LevelRangeFilter
appender.service.filter.threshold.minLevel = INFO
appender.service.filter.threshold.maxLevel = INFO
appender.service.filter.threshold.onMatch = ACCEPT
appender.service.filter.threshold.onMismatch = DENY

######################################
# Loggers for logging different packages
######################################
loggers = package

# RootLogger write all into logs, besides chats.log
rootLogger.level = debug
rootLogger.additivity = false
rootLogger.appenderRefs = stdout, rolling, rolling2, errorLog
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.rolling.ref = RollingFile
rootLogger.appenderRef.rolling.level = info
rootLogger.appenderRef.rolling2.ref = debugLog
rootLogger.appenderRef.rolling2.level = debug
rootLogger.appenderRef.errorAppender.ref = errorLog
rootLogger.appenderRef.errorAppender.level = warn

# Logs only chat messages of class: org.remad.logging.protocol.Protocol
logger.package.name = org.remad.logging.protocol.Protocol
logger.package.additivity = false
logger.package.level = info
logger.package.appenderRefs = service
logger.package.appenderRef.service.ref = chats
logger.package.appenderRef.service.level = info
冯流觞
2023-03-14

您有一个全局阈值过滤器,其级别设置为ALL。这意味着每个日志事件都将被发送到appender,而不管在记录器上设置的日志级别如何。您还将根日志记录级别设置为ALL。这是一个非常糟糕的做法,因为它会导致性能问题,因为每个日志记录调用都将创建一个日志事件,并且因为您正在捕获行号,所以每个事件都需要通过遍历线程的堆栈跟踪来捕获位置信息。

摆脱全局阈值过滤器。你的案子不需要它。将根日志记录级别设置为DEBUG,然后将appender引用的日志记录级别设置为与要记录的内容相匹配。

我已将您的配置修改为我认为会起作用的配置,但我没有尝试过。我更喜欢XML语法,所以如果我输入了错别太惊讶。

status = error
name = PropertiesConfig

######################################
# Global properties as varies
######################################
property.logPath = logs


######################################
# Appenders Section for roles
######################################
appenders = rolling, console, rolling2, errorAppender, service

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.immediateFlush= true
appender.rolling.append = true
appender.rolling.fileName = ${logPath}/server.log
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
appender.rolling.filter.threshold.type = LevelRangeFilter
appender.rolling.filter.threshold.minLevel = INFO
appender.rolling.filter.threshold.maxLevel = INFO
appender.rolling.filter.threshold.onMatch = ACCEPT
appender.rolling.filter.threshold.onMismatch = DENY

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling2.type = RollingFile
appender.rolling2.name = debugLog
appender.rolling2.immediateFlush= true
appender.rolling2.append = true
appender.rolling2.fileName = ${logPath}/debug.log
appender.rolling2.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling2.layout.type = PatternLayout
appender.rolling2.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling2.policies.type = Policies
appender.rolling2.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling2.policies.time.interval = 1
appender.rolling2.policies.time.modulate = true
appender.rolling2.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling2.policies.size.size=10MB
appender.rolling2.strategy.type = DefaultRolloverStrategy
appender.rolling2.strategy.max = 20
appender.rolling2.filter.threshold.type = LevelRangeFilter
appender.rolling2.filter.threshold.minLevel = DEBUG
appender.rolling2.filter.threshold.maxLevel = DEBUG
appender.rolling2.filter.threshold.onMatch = ACCEPT
appender.rolling2.filter.threshold.onMismatch = DENY

appender.errorAppender.type = RollingFile
appender.errorAppender.name = errorLog
appender.errorAppender.immediateFlush= true
appender.errorAppender.append = true
appender.errorAppender.fileName = ${logPath}/error.log
appender.errorAppender.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.errorAppender.layout.type = PatternLayout
appender.errorAppender.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.errorAppender.policies.type = Policies
appender.errorAppender.policies.time.type = TimeBasedTriggeringPolicy
appender.errorAppender.policies.time.interval = 1
appender.errorAppender.policies.time.modulate = true
appender.errorAppender.policies.size.type = SizeBasedTriggeringPolicy
appender.errorAppender.policies.size.size=10MB
appender.errorAppender.strategy.type = DefaultRolloverStrategy
appender.errorAppender.strategy.max = 20
appender.errorAppender.filter.threshold.type = LevelRangeFilter
appender.errorAppender.filter.threshold.minLevel = ERROR
appender.errorAppender.filter.threshold.maxLevel = ERROR
appender.errorAppender.filter.threshold.onMatch = ACCEPT
appender.errorAppender.filter.threshold.onMismatch = DENY

appender.service.type = RollingFile
appender.service.name = chats
appender.service.append = true
appender.service.fileName = ${logPath}/chats.log
appender.service.filePattern = chats-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.service.layout.type = PatternLayout
appender.service.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.service.policies.type = Policies
appender.service.policies.time.type = TimeBasedTriggeringPolicy
appender.service.policies.time.interval = 1
appender.service.policies.time.modulate = true
appender.service.policies.size.type = SizeBasedTriggeringPolicy
appender.service.policies.size.size=10MB
appender.service.strategy.type = DefaultRolloverStrategy
appender.service.strategy.max = 20
appender.service.filter.threshold.type = LevelRangeFilter
appender.service.filter.threshold.minLevel = INFO
appender.service.filter.threshold.maxLevel = INFO
appender.service.filter.threshold.onMatch = ACCEPT
appender.service.filter.threshold.onMismatch = DENY

######################################
# Loggers Section for file write
######################################
loggers = rolling, package

logger.rolling.name = org.remad.logging
logger.rolling.appenderRefs = rolling, rolling2
logger.rolling.level = DEBUG
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref = RollingFile
logger.rolling.appenderRef.rolling.level = INFO
logger.rolling.appenderRef.rolling2.ref = debugLog
logger.rolling.appenderRef.rolling2.ref = DEBUG

logger.package.name = org.remad.logging.protocol.Protocol
logger.package.appenderRefs = service
logger.package.level = info
logger.package.additivity = false
logger.package.appenderRef.service.ref = chats
logger.package.appenderRef.service.level = INFO

######################################
# RootLogger Section for settings
######################################
rootLogger.level = INFO
rootLogger.appenderRefs = stdout, chats, errorLog
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.stdout.level = INFO
rootLogger.appenderRef.service.ref = chats
rootLogger.appenderRef.service.level = INFO
rootLogger.apenderRef.errorAppender.ref = errorLog
rootLogger.appenderRef.errorAppender.ref = ERROR

我离开过滤器上的应用程序只过滤你想要的水平。我不知道为什么你只想要INFO或ERROR消息在一个特定的Appender,而不是如果该级别的默认和更具体,但我离开了它。如果你真的不需要,然后删除这些过滤器。

 类似资料:
  • 问题内容: 我在应用程序中使用log4j2。 我想要的是将要进行调试的所有内容都放入控制台,将要进行“信息”的所有内容都进入myapp.log,仅将“信息”的所有内容都进入“ myapp- audit.log”。 原因是,INFO主要由成功的数据修改组成(例如,“用户创建”,“用户更新”,“用户删除”等)。如果有效,则为数据修改的审核日志。 但是我不知道该怎么做。 如何仅获取“信息”以登录到“ m

  • 我定义了多个记录器,如下所示(com.xyz和org.xyz)。Log4j2忽略了第一个记录器定义,只加载第二个。在示例中,org.xyz未加载。 在log4j2调试消息下方查找。 我的配置正确吗?

  • 由此我从NetBeans得到以下错误消息: 所有异常消息都写入控制台,包括用户日志的消息。所以我知道两个伐木工都在工作。记录器附加器写入文件OK。userLog记录器不会创建任何文件,它使用几乎相同的附加器。 看来我还没有正确地命名一些东西来允许log4j2找到记录器。我试过各种命名的组合来弄对它,但我搞不清问题出在哪里。 我必须在每个类的记录器初始化中包含类名吗? 编辑:我需要包含包名。我花了一

  • 我试图创建一个完全独立的第二个上下文/配置/记录器-而不是现有配置/上下文中的记录器。日志消息将发送到STDOUT。 当前代码 上面的.error()调用转到标准输出。请注意,我尝试使用reconfigure()而不是initialize(),但这会打乱我的原始配置。 当我打印出来时,记录器似乎是错误的,它们的名称和上下文似乎是正确的,但处于错误级别。还有一些事情要做,不要做附加物。 从生成器生成

  • 问题内容: 我正在尝试使用 Log4j2 将spring日志打印到文件和控制台中。我猜这是我的Log4j2配置中的问题。我无法使其正常运行。我的log4j2.xml文件中有以下配置: 这些是我在pom文件中具有的依赖项:(可能其中一些不是必需的) 我没有在Java代码中做任何与spring相关的事情。我正在使用Apache Camel的Main类,该类读取我的spring配置并加载bean。 我究

  • 给定:我在Kafka中有两个主题,假设主题A和主题B。Kafka流从主题A中读取一条记录,对其进行处理,并产生与所消耗记录相对应的多条记录(假设recordA和recordB)。现在的问题是我如何使用Kafka流来实现这一点。 在这里,读取的记录是消息;处理之后,它返回一个消息列表。如何将此列表划分为两个生产者流?任何帮助都将不胜感激。