我正在使用Log4J 2.10。
我正在尝试以编程方式创建异步logger \ appender。我需要有两件事: 1.我应该能够在运行时指定日志文件的文件路径。 2.我需要在运行时指定模式。
我可以在下面提出代码。但我不能将RollingFileAppender添加到AsyncAppender。网上有一些使用AsyncAppender.wrap的例子。但是这个API似乎不适用于Log4J 2.10。
你知道我怎么做到这一点吗?
void createLog4JLogger(final String logFilePath) {
LoggerContext context = (LoggerContext) LogManager.getContext();
final Configuration config = context.getConfiguration();
final PatternLayout patternLayout = PatternLayout.newBuilder().withPattern(CONVERSION_PATTERN).withCharset(Charset.defaultCharset()).build();
final RollingFileAppender fileAppender =
RollingFileAppender.newBuilder().withName(APPENDER_NAME).withLayout(patternLayout).withFileName(logFilePath).build();
AppenderRef ref = AppenderRef.createAppenderRef(APPENDER_NAME, null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
final AsyncAppender asyncAppender = AsyncAppender.newBuilder().setAppenderRefs(refs).setName(APPENDER_NAME).setConfiguration(config).build();
LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, LOGGER_NAME, LOGGER_NAME, refs, null, null, null);
loggerConfig.addAppender(asyncAppender, null, null);
config.addAppender(asyncAppender);
config.addLogger(LOGGER_NAME, loggerConfig);
context.updateLoggers(config);
final Logger logger = LogManager.getContext().getLogger(LOGGER_NAME);
logger.info("HELLO_WORLD");
}
提前致谢!
答案 0 :(得分:0)
您可以使用以下代码在log4j 2中创建异步记录器。
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.newBuilder().withConfiguration(ctx.getConfiguration()).withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - test %msg%n").build();
Appender appender = FileAppender.newBuilder().withFileName("logs/test.log").setLayout(layout)
.setConfiguration(config).setName("TestApp").build();
AppenderRef ref = AppenderRef.createAppenderRef("TestApp",Level.INFO, null);
config.addAppender(appender);
appender.start();
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig= AsyncLoggerConfig.createLogger(true, Level.INFO, "test","test", refs, null, config, null);
loggerConfig.addAppender(appender, Level.INFO, null);
config.addLogger("test", loggerConfig);
loggerConfig.start();
答案 1 :(得分:0)
我需要使用没有配置文件的多线程log4j记录器。因此,我最终将来自多个源的代码(答案中的引用)组合在一起。它已经使用了最近几年了(抱歉,回复延迟)。也许对其他人有用:
创建记录器的类:
Const Url$ = "Web Page"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet1")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("txtUserName")(0)
Set oPassword = .document.getElementsByName("txtPassword")(0)
oLogin.Value = UserName
oPassword.Value = Password
End With
ie.document.getElementsByName("btnLogin")(0).Click
ie.navigate "Web Page"
ieBusy ie
Set Region = ie.document.getElementById("sregion")
Region.selectedIndex = 3
Set Sbg = ie.document.getElementById("sSBG")
Sbg.selectedIndex = 1
Set mth = ie.document.getElementById("smonth")
mth.selectedIndex = 0
Set htmlDoc = ie.document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "button" Then
htmlInput.Click
Exit For
End If
Next htmlInput
'I need to insert code here to extract report
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.READYSTATE < 4
DoEvents
Loop
End Sub
}
自定义配置工厂:
class MyCustomLogger {
private static Logger createLog4JLogger(final String logFilePath, final String logLevel) {
try {
final Level level = (null != logLevel && 0 == logLevel.toLowerCase().compareTo(DEBUG_MODE_STRING)) ? Level.DEBUG : Level.INFO;
ConfigurationFactory.setConfigurationFactory(new LogCustomConfigurationFactory(logFilePath, level)); // This must be called before any other calls to Log4j
dumpStringToSysOut("created configuration factory");
Logger logger = LogManager.getLogger();
dumpStringToSysOut("logger is " + ((null == logger) ? "null" : "not null"));
return logger;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}