Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java log4j2中不同附加器上的不同级别(包括自定义级别)_Java_Log4j2 - Fatal编程技术网

Java log4j2中不同附加器上的不同级别(包括自定义级别)

Java log4j2中不同附加器上的不同级别(包括自定义级别),java,log4j2,Java,Log4j2,我想将我的两个类中的特殊消息记录到DB中,还想在控制台上写入我的所有程序日志。为了实现这一点,我为JDBC appender定义了一个intLevel=50的自定义级别(managerLogsLevel),但我无法设置log4j2.xml来实现我的确切目的。这是我的xml文件: <CustomLevels> <CustomLevel name="managerLogsLevel" intLevel="50" /> </CustomLevels>

我想将我的两个类中的特殊消息记录到DB中,还想在控制台上写入我的所有程序日志。为了实现这一点,我为JDBC appender定义了一个intLevel=50的自定义级别(managerLogsLevel),但我无法设置log4j2.xml来实现我的确切目的。这是我的xml文件:

<CustomLevels>

    <CustomLevel name="managerLogsLevel" intLevel="50" />

</CustomLevels>

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{yyyy-MMM-dd hh:mm:ss a } %level %c - %m %n" />
    </Console>
    <JDBC name="MySQLDatabase" tableName="phonebook_finalproject.eventlog">
        <ConnectionFactory class="ir.maktabsharif.core.base.ConnectionFactory"
            method="getConnection" />
        <Column name="time" isEventTimestamp="true" />
        <Column name="name" pattern="%logger" />
        <Column name="level" pattern="%level" />
        <Column name="description" pattern="%m" />
    </JDBC>
</Appenders>

<Loggers>

    <Root level="info" additivity="false">
        <AppenderRef ref="Console" level="info" />
    </Root>

    <Logger name="org.hibernate" level="warn" />

    <Logger name="ir.maktabsharif.api" level="managerLogsLevel" >
        <AppenderRef ref="MySQLDatabase" level="managerLogsLevel" />
        <AppenderRef ref="Console" level="info" />
    </Logger>

</Loggers>



正如我在评论中提到的,我不认为自定义日志级别是适合您的用例的最佳解决方案:

我想把我的两个类中的特殊消息记录到DB中,还想把我所有的程序日志都写在控制台上

我在评论中提出了两种不同的解决方案,但我想提供一个更完整的答案和一些例子。请注意,这些示例不向数据库发送消息,但您可以根据需要修改附加器。这些示例只是为了提供一些您可以使用的通用技术的说明

下面的第一个java类将使用
Marker
/
MarkerFilter
方法:

package example;

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

public class SomeClass {

    private static final Logger LOG = LogManager.getLogger();
    private static final Marker SPECIAL_MESSAGE = MarkerManager.getMarker("SPECIAL_MESSAGE");   

    public static void main(String[] args){

        if(LOG.isDebugEnabled())
            LOG.debug("This is some debug!");
        LOG.info("Here's some info!");
        //To log a special kind of message use the same logger but include the marker
        LOG.info(SPECIAL_MESSAGE, "Something special goes here");
        LOG.error("Some error happened!");
    }
}
package example;

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

public class SomeClass2 {

    private static final Logger LOG = LogManager.getLogger();
    private static final Logger SPECIAL_LOG = LogManager.getLogger("SPECIAL_LOGGER");   

    public static void main(String[] args){

        if(LOG.isDebugEnabled())
            LOG.debug("This is some debug!");
        LOG.info("Here's some info!");
        //To log a special kind of message use the special logger
        SPECIAL_LOG.info("Something special goes here");
        LOG.error("Some error happened!");
    }
}
下一类(以下)将使用“特殊记录器”方法:

package example;

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

public class SomeClass {

    private static final Logger LOG = LogManager.getLogger();
    private static final Marker SPECIAL_MESSAGE = MarkerManager.getMarker("SPECIAL_MESSAGE");   

    public static void main(String[] args){

        if(LOG.isDebugEnabled())
            LOG.debug("This is some debug!");
        LOG.info("Here's some info!");
        //To log a special kind of message use the same logger but include the marker
        LOG.info(SPECIAL_MESSAGE, "Something special goes here");
        LOG.error("Some error happened!");
    }
}
package example;

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

public class SomeClass2 {

    private static final Logger LOG = LogManager.getLogger();
    private static final Logger SPECIAL_LOG = LogManager.getLogger("SPECIAL_LOGGER");   

    public static void main(String[] args){

        if(LOG.isDebugEnabled())
            LOG.debug("This is some debug!");
        LOG.info("Here's some info!");
        //To log a special kind of message use the special logger
        SPECIAL_LOG.info("Something special goes here");
        LOG.error("Some error happened!");
    }
}
以下是log4j2.xml文件,其中包含两种方法的配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <File name="SpecialAppender" fileName="logs/special.log" immediateFlush="false"
            append="false">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
            <MarkerFilter marker="SPECIAL_MESSAGE" onMatch="ACCEPT" onMismatch="DENY"/>
        </File>

        <File name="SpecialAppenderNoFilter" fileName="logs/specialNoFilter.log" immediateFlush="false"
            append="false">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>

        <!-- This logger uses the MarkerFilter method -->
        <Logger name="example.SomeClass" level="debug">
            <AppenderRef ref="SpecialAppender" />
        </Logger>

        <!-- The following logger is used for the "special logger" method -->

        <Logger name="SPECIAL_LOGGER" level="info">
            <AppenderRef ref="SpecialAppenderNoFilter" />
        </Logger>
    </Loggers>
</Configuration>


因此,如果您移动到一个具有slf4j绑定的框架,那么该标记很有可能仍然有效。

-还有,为什么您要使用自定义日志级别,而不是简单地为您的“特殊消息”使用特殊日志记录程序?@D.B.您所说的特殊日志记录程序是什么意思?你的意思是使用普通级别吗?我的意思是上面提到的
是许多可能的解决方案之一,另一种是使用
MarkerFilter
(请参阅),这样你的特殊附加器只接受带有特殊标记的消息(请参阅)。@D.B.谢谢你的指导:)我使用了你的“特殊记录器”解决方案,它可以工作,谢谢。谢谢你的回答@D.B。