我的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} [%sn] [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RandomAccessFile name="randomAccessFile" fileName="temp\\logs.log" immediateFlush="false" append="false">
<!-- <PatternLayout pattern="%d %p %class{2.} [%t] %location %m %ex%n" /> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</RandomAccessFile>
<JDBC name="databaseAppender" tableName="app_logs" bufferSize="1000">
<!-- <DataSource jndiName="java:/comp/env/jdbc/testLog" /> -->
<!-- <BurstFilter level="TRACE" rate="20" maxBurst="2" /> -->
<ConnectionFactory class="com.mc.loggerProvider.ConnectionFactory" method="getDatabaseConnection" />
<Column name="LOG_ID" pattern="%u{TIME}" isUnicode="false" />
<Column name="LOG_TIMESTAMP" isEventTimestamp="true" isUnicode="false" />
<!-- <Column name="CORR_ID" pattern="%X{CORR_ID}" isUnicode="false" /> -->
<Column name="LOG_LEVEL" pattern="%level" isUnicode="false" />
<Column name="LOGGER_NAME" pattern="%logger" isUnicode="false" />
<Column name="MESSAGE" pattern="%msg %ex{full}" isUnicode="false" />
<!-- <Column name="EXCEPTION" pattern="%ex{full}" isUnicode="false" /> -->
</JDBC>
</Appenders>
<Loggers>
<Logger name="console" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="console" />
</Logger>
<AsyncLogger name="asyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="randomAccessFile" />
</AsyncLogger>
<Logger name="syncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="randomAccessFile" />
</Logger>
<AsyncLogger name="asyncDB" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
</AsyncLogger>
<Logger name="syncDB" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
</Logger>
<AsyncLogger name="asyncDBasyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
<AppenderRef ref="randomAccessFile"></AppenderRef>
</AsyncLogger>
<Logger name="syncDBsyncFile" level="trace" includeLocation="false" additivity="false">
<AppenderRef ref="databaseAppender" />
<AppenderRef ref="randomAccessFile"></AppenderRef>
</Logger>
<Root level="info" includeLocation="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
由于我已经为同步/异步文件/数据库记录目的定义了几个记录器。当我想使用任何记录器时,我在这里面临的问题我无法为这些记录器定义名称。
LogManager.getLogger("asyncDB") -- will give me logger with name asyncDB
但是,惯例说创建具有类名的记录器,因为如果在记录输出中写入%logger,则更容易调试。
LogManager.getLogger(MyClass.class)
我的要求要求我创建一个带有类名的记录器,但是根据用户的要求,它应该是异步的或同步的。
我创建了一个小类名LogProvider,它有方法registerFor,它再次具有AppendersType类型的参数。以下是代码。
public class LogProvider {
public static Logger registerFor(AppendersType type) {
switch (type) {
case DATABASE_ASYNC:
return LogManager.getLogger("asyncDB");
case DATABASE_SYNC:
return LogManager.getLogger("syncDB");
case FILE_ASYNC:
return LogManager.getLogger("asyncFile");
case FILE_SYNC:
return LogManager.getLogger("syncFile");
case FILE_DATABASE_SYNC:
return LogManager.getLogger("syncDBsyncFile");
case FILE_DATABASE_ASYNC:
return LogManager.getLogger("asyncDBasyncFile");
default:
break;
}
return null;
}
我想使用asyncLoggers但是具有类的名称而不是XML文件中定义的名称。