我开始使用Eclipse插件 Log4E 并创建了一个 log4j2.properties 文件,如下所示:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\Robinsonfile\log\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.de.test=warn
log4j.rootLogger=warn
这是一个修改示例,从文档中创建一个写入 log4j.appender.file.File 的属性文件,我已经尝试过使用\\转义\但同样的结果文件没有创建。谁能告诉我我做错了什么?在我的代码下面
/**
* Logger for this class
*/
private static final Logger logger = LogManager.getLogger(RobinsonAutomation.class.getName());
private static final String ROBINSON = "Bitte robinson-lite.exe auswaehlen";
private static final String WHITELIST = "Bitte Whitelist Textdatei ";
private static final String ALL_CONTACTS = "Bitte die original Textdatei (alle Emailkontakte) auswaehlen";
private final FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text File", "txt");
private final FileNameExtensionFilter exeFilter = new FileNameExtensionFilter("Robinson.exe", "exe");
private List<String> getContacts(File file) {
if (logger.isDebugEnabled()) {
logger.debug("getContacts(File) - start"); //$NON-NLS-1$
}
checkNotNull(file);
try {
List<String> returnList = Files.readLines(file, Charset.forName("UTF-8"));
if (logger.isDebugEnabled()) {
logger.debug("getContacts(File) - end"); //$NON-NLS-1$
}
return returnList;
} catch (IOException e) {
logger.error("getContacts(File)", e); //$NON-NLS-1$
String message = "Error could not read file\n".concat(e.getMessage());
JOptionPane.showMessageDialog(new JFrame(), message, "Error", JOptionPane.ERROR_MESSAGE);
}
if (logger.isDebugEnabled()) {
logger.debug("getContacts(File) - end"); //$NON-NLS-1$
}
return null;
}
private File getFile(DataSources dataSource) {
if (logger.isDebugEnabled()) {
logger.debug("getFile(DataSources) - start"); //$NON-NLS-1$
}
if (dataSource.equals(DataSources.ROBINSON))
return returnFile(this.getROBINSON(), exeFilter);
else if (dataSource.equals(DataSources.ALL_CONTACTS))
return returnFile(RobinsonAutomation.ALL_CONTACTS, textFilter);
else
return returnFile(RobinsonAutomation.WHITELIST, textFilter);
}
private File returnFile(String title, FileNameExtensionFilter fileFilter) {
if (logger.isDebugEnabled()) {
logger.debug("returnFile(String, FileNameExtensionFilter) - start"); //$NON-NLS-1$
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(title);
fileChooser.setFileFilter(fileFilter);
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(this);
checkArgument(result == JFileChooser.APPROVE_OPTION);
File returnFile = fileChooser.getSelectedFile();
if (logger.isDebugEnabled()) {
logger.debug("returnFile(String, FileNameExtensionFilter) - end"); //$NON-NLS-1$
}
return returnFile;
}
public static void main(String[] args) throws IOException {
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.DEBUG);
System.out.println(logger.isDebugEnabled());
if (logger.isDebugEnabled()) {
logger.debug("main(String[]) - start"); //$NON-NLS-1$
}
RobinsonAutomation robinsonAutomation = new RobinsonAutomation();
File robinsonEXE = robinsonAutomation.getFile(DataSources.ROBINSON);
String pathToRobinson = robinsonEXE.getAbsolutePath();
CommandLine commandLine = new CommandLine(pathToRobinson);
DefaultExecutor executor = new DefaultExecutor();
executor.execute(commandLine);
try {
Thread.sleep(40000);
} catch (InterruptedException e) {
logger.error("main(String[])", e); //$NON-NLS-1$
}
File originalContacts = robinsonAutomation.getFile(DataSources.ALL_CONTACTS);
String current_directory = originalContacts.getAbsolutePath();
ArrayList<String> orig_contacts = Lists.newArrayList(robinsonAutomation.getContacts(originalContacts));
checkNotNull(orig_contacts, "Bitte keine leere Datei hinzufuegen");
File whitelist_file = robinsonAutomation.getFile(DataSources.WHITELIST);
ArrayList<String> whitelist_contacts = Lists.newArrayList(robinsonAutomation.getContacts(whitelist_file));
checkNotNull(whitelist_contacts, "Keine Whitelist-Kontakte vorhanden");
ArrayList<String> blacklist = (ArrayList<String>) DataReader.createBlackList(orig_contacts, whitelist_contacts);
checkNotNull(blacklist, "Keine Blacklist-Kontakte vorhanden");
DataReader.writeBlacklistFile(blacklist);
if (logger.isDebugEnabled()) {
logger.debug("main(String[]) - end"); //$NON-NLS-1$
}
}
答案 0 :(得分:0)
安东的回答是正确的,但你也有两行log4j.rootLogger
:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# ...
log4j.rootLogger=warn
应用最后一个,它将根记录器级别设置为WARN
,但不为其分配任何appender。您可能想要删除此行。