我是log4j的新手。我的要求是在每次迭代期间创建一个新的日志文件。基本上在每次迭代中,下面是我需要达到的目标:
我已经模拟了我需要的实现。
log4j.properties
package app.testscript;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.FileAppender;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
public class Log4jTest {
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
System.setProperty("log4j.test.start.timestamp", dateFormat.format(new Date()));
}
Logger traceLogger = Logger.getLogger("traceLogger");
Logger rootLogger = Logger.getRootLogger();
Logger testScriptLogger = null;
public void doSomething() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
PatternLayout patterLayout = new PatternLayout("%m%n");
String folderNameToCreate = dateFormat.format(new Date());
FileAppender fileAppender = new FileAppender(patterLayout, "C:/MyTestLog/" + folderNameToCreate + "/log.txt", true);
fileAppender.setName("A" + folderNameToCreate);
fileAppender.activateOptions();
rootLogger.addAppender(fileAppender);
try {
testScriptLogger = LogManager.getLogger("A" + folderNameToCreate);
}catch(Exception e1) {
System.out.println("Exception e: ");
e1.printStackTrace();
}
//testScriptLogger.setAdditivity(false);
testScriptLogger.info("Message to capture in log - " + folderNameToCreate);
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Log4jTest obj = new Log4jTest();
for (int i=0; i<2; i++) {
obj.doSomething();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java代码
testScriptLogger.setAdditivity(false);
上述代码的结果:
预期结果在两个log.txt文件中都有1行(在迭代1和2期间创建)
使用log4j:WARN No appenders could be found for logger (A20170518_03_49_10_802).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
时,我收到以下警告消息,并且没有任何内容写入该文件。
let Beers = [
{id: 1, size: 4},
{id: 5, size: 1},
{id: 10, size: 0.5},
{id: 11, size: 1},
{id: 12, size: 2},
{id: 13, size: 1},
];
let TrunkSize = 2;
AllCombinations = [];
var combStrings = []
function GetCombinations(currentCombination, beers, remainingSum)
{
if (remainingSum < 0)
return;// Sum is too large; terminate recursion
else {
if (currentCombination.length > 0)
{
currentCombination.sort((a,b)=>{
return a.id > b.id
});
//var uniquePermutation = true;
var tmp = currentCombination.map((cc)=>{
return cc.id;
})
if (combStrings.indexOf(JSON.stringify(tmp)) == -1) {
this.AllCombinations.push(currentCombination);
var tmp = currentCombination.map((cc)=>{
return cc.id;
})
combStrings.push(JSON.stringify(tmp))
}
}
}
for (var i = 0; i < beers.length; i++) {
var newChoices = beers.slice();
var newCombination = currentCombination.concat(newChoices.splice(i, 1));
var newRemainingSum = remainingSum - beers[i].size;
this.GetCombinations(newCombination, newChoices, newRemainingSum);
}
}
GetCombinations([],Beers,TrunkSize)
console.log(AllCombinations,combStrings)
请帮我解决一下。感谢
答案 0 :(得分:0)
找到答案。
Java代码:
package app.testscript;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
public class Log4jTest {
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
System.setProperty("log4j.test.start.timestamp", dateFormat.format(new Date()));
}
Logger scriptLogger = Logger.getLogger("scriptLogger");
String prevAppenderName = "";
public void removeBaseAppenderConfiguredInlog4jProperties() {
scriptLogger.removeAppender("scriptLog");
}
public void doSomething() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
PatternLayout patterLayout = new PatternLayout("%m%n");
String folderNameToCreate = dateFormat.format(new Date());
FileAppender fileAppender = new FileAppender(patterLayout, "C:/MyTestLog/" + folderNameToCreate + "/log.txt", true);
fileAppender.setName("A" + folderNameToCreate);
fileAppender.activateOptions();
if(!prevAppenderName.equalsIgnoreCase("")) {
scriptLogger.removeAppender(prevAppenderName);
}
scriptLogger.addAppender(fileAppender);
prevAppenderName="A" + folderNameToCreate;
scriptLogger.setAdditivity(false);
scriptLogger.info("Message to capture in log - " + folderNameToCreate);
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Log4jTest obj = new Log4jTest();
obj.removeBaseAppenderConfiguredInlog4jProperties();
for (int i=0; i<2; i++) {
obj.doSomething();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}