import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.LoggerConfig; public class Log4jFilterExample { private static final Logger logger = LogManager.getLogger(Log4jFilterExample.class); public static void main(String[] args) { Configurator.setLevel(Log4jFilterExample.class.getName(), Level.INFO); Filter levelFilter = Filter.createFilter("level-match", Filter.Result.ACCEPT, Filter.Result.DENY); levelFilter.start(); levelFilter.addAttribute("levelToMatch", Level.WARN.toString()); LoggerConfig loggerConfig = LogManager.getContext(false).getConfiguration().getLoggerConfig(logger.getName()); loggerConfig.addFilter(levelFilter); Configurator.reconfigure(); logger.trace("This is a TRACE message."); logger.debug("This is a DEBUG message."); logger.info("This is an INFO message."); logger.warn("This is a WARN message."); logger.error("This is an ERROR message."); logger.fatal("This is a FATAL message."); } } import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.layout.PatternLayout; public class Log4jLayoutExample { private static final Logger logger = LogManager.getLogger(Log4jLayoutExample.class); public static void main(String[] args) { Configurator.setLayout(Log4jLayoutExample.class.getName(), PatternLayout.createLayout( "%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n", null, null, null, null)); logger.info("This is an example of a custom log layout."); } } <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> <Filters> <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="NEUTRAL"/> </Filters> </Console> </Appenders> <Loggers> <Root level="INFO"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>


上一篇:
下一篇:
切换中文