# 样例
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="DEBUG" monitorInterval="30"> <Appenders> <Console name="my_console_appender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> <MarkerFilter marker="ADMIN" onMatch="ACCEPT" onMismatch="DENY" /></Console> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="my_console_appender" /> </Root> </Loggers> </Configuration>
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 HelloWorld { private static final Logger logger = LogManager.getLogger(); private static final Marker ADMIN_USER = MarkerManager.getMarker("ADMIN"); private static final Marker GENERAL_USER = MarkerManager .getMarker("GENERAL"); public static void adminActions() { logger.info(ADMIN_USER, "Admin actions are triggered"); } public static void generalActions() { logger.info(GENERAL_USER, "User actions are triggered"); } public static void main(String args[]) { adminActions(); generalActions(); } }
输出:
22:52:14.093 [main] INFO HelloWorld - Admin actions are triggered
# Reference
http://self-learning-java-tutorial.blogspot.jp/2015/10/log4j2-markers.html Makers
时间: 2024-10-25 00:24:38