我的要求是将不同的日志写入需要不同模式的控制台。
我已经使用了多个附加器,并且使用了一个单独的记录器,但是它没有采用我为附加器提供的模式,而是采用了另一种模式。
下面是log4j2.properties
name = PropertiesConfig
appenders = console, console1
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %t %-5p %c{6}:%L - %m%n
appender.console1.type = Console
appender.console1.name = STDOUT1
appender.console1.layout.type = PatternLayout
appender.console1.layout.pattern = %m%n
loggers = rolling
logger.rolling.name = org.apache.logging.log4j.core.appender.rolling
#Configure module specific
logger.org.apache.kafka.common.network.Selector = info
logger.com.test.test1 = fine
logger.com.test.test1.service.impl.RSStd=info
#configure rootLogger and attach all the appenders to it
rootLogger.level = info
rootLogger.appenderRef.console.ref = STDOUT
logger.performancelog.name = PERFORMANCE_LOG
logger.performancelog.additivity = false
logger.performancelog.level = info
logger.performancelog.appenderRef.console1.ref = STDOUT1
每次它选择这个模式
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %t %-5p %c{6}:%L - %m%n
代替
appender.console1.layout.pattern = %m%n
下面是我的java代码GlobalLogger
import java.util.Objects;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.owasp.esapi.ESAPI;
import org.apache.logging.log4j.core.config.Configuration;
import com.test.sample.common.constants.CommonConstants;
import com.test.sample.obj.ProcessRuntime;
public class GlobalLogger {
private static final String Ref = ", Ref=";
private final Logger logger;
private final Logger testLogger;
public static GlobalLogger getLogger(Class<?> cls) {
return getLogger (cls.getName());
}
public static GlobalLogger getLogger(String pkgName) {
return new GlobalLogger(LogManager.getLogger(pkgName));
}
private GlobalLogger(Logger logger) {
this.testLogger=LogManager.getLogger("PERFORMANCE_LOG");
this.logger = logger;
}
public static GlobalLogger getTESTLogger(String loggerName) { //1.1
return new GlobalLogger(LogManager.getLogger(loggerName));
}
public void logIbgm(Object message){ //1.1
testLogger.info("(PERFORMANCE_LOG) " + validateInput(message));
}
public boolean isTrace() {
return logger.isTraceEnabled();
}
public void trace(Object message){
if (isTrace()) {
logger.trace(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " +validateInput( message));
}
}
public void trace(Object message, Throwable t){
if(isTrace()){
logger.trace(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId()) + ", " + validateInput(message), t);
}
}
public boolean isDebug() {
return logger.isDebugEnabled();
}
public void debug(Object message){
if(isDebug()){
logger.debug(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " + validateInput(message));
}
}
public void debug(Object message, Throwable t){
if(isDebug()){
logger.debug(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " +validateInput( message), t);
}
}
public boolean isInfo() {
return logger.isInfoEnabled();
}
public void info(Object message){
if(isInfo()){
logger.info(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId()) + ", " +validateInput(message));
}
}
public void info(Object message, Throwable t){
if(isInfo()){
logger.info(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " + validateInput(message), t);
}
}
public boolean isWarn() {
return logger.isWarnEnabled();
}
public void warn(Object message){
if(isWarn()){
logger.warn(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " + validateInput(message));
}
}
public void warn(Object message, Throwable t){
if(isWarn()){
logger.warn(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " + validateInput(message), t);
}
}
public void error(Object message){
logger.error(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId())+ ", " + validateInput(message));
}
public void error(Object message, Throwable t){
logger.error(lineNumber() + Ref + (Objects.equals(null, ProcessRuntime.getRequestId())?"":ProcessRuntime.getRequestId()) + ", " + validateInput(message), t);
}
/*
* Get the Actual Line Number
*/
private String lineNumber() {
StackTraceElement[] threadTrackArray = Thread.currentThread().getStackTrace();
if (threadTrackArray.length > 3) {
return ":" + Integer.toString(threadTrackArray[3].getLineNumber()) + "-";
}
return "";
}
public static void consoleLoggerInit () {
consoleLoggerInit (null);
}
public static void consoleLoggerInit (Level logLevel) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
if (config != null) {
config.getRootLogger().getAppenders().clear();
if (logLevel != null) {
config.getRootLogger().setLevel(logLevel);
} else {
config.getRootLogger().setLevel(Level.INFO);
}
config.addAppender(ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.newBuilder().withPattern("%5p [%t] (%c{1}) %m%n")
.withConfiguration(config).build()));
}
}
public static String validateInput(Object msg) {
try {
String aString="";
if(msg!=null)
aString=msg.toString();
return ESAPI.validator().getValidInput(CommonConstants.ESAPI_CONSTANTS.CONTEXT_INPUT_VALIDATION, aString,
CommonConstants.ESAPI_CONSTANTS.VAL_TYPE_SOMETHING_ELSE, aString.length()+1,false);
} catch (Exception e) {
return "";
}
}
}
我使用this.testLogger=LogManager.getLogger("PERFORMANCE_LOG");
和方法
public void logIbgm(Object message){ //1.1
testLogger.info("(PERFORMANCE_LOG) " + validateInput(message));
}
PERFORMANCE_LOG is the name that is the same used for the logger name in log4j2.properties
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
1条答案
按热度按时间guykilcj1#
您正在使用
appenders
和loggers
属性指定附加器和记录程序标识符的列表。虽然这些属性仍受支持,但从版本2.6(参见LOG4J2-1308)起不再需要它们。要启用“PERFORMANCE_LOG”记录程序,请执行以下操作之一:loggers
属性(默认情况下启用所有记录器ID),*或将其设置为所有已启用记录器的列表,例如:
备注:虽然允许在同一个输出流上有多个appender,但这肯定不是推荐的配置。Log4j2中的
PatternLayout
支持“模式选择器”(参见文档),可以在您的场景中使用。使用
MarkerPatternSelector
的可能解决方案需要:PERFORMANCE
标记进行日志记录,您可以使用任何日志记录器,只需使用带有标记参数的Logger
方法或LogBuilder
: