Remko Popma の回答がログを記録する最も効率的な方法かもしれませんが、独自にログ ファイルを作成できる小さなクラスを作成しました。
私は受け入れられた回答の解決策を使用すると思うので、XMLファイルを回避するために私が書いたコードは次のとおりです。
import gnu.trove.map.hash.THashMap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.async.AsyncLoggerContext;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.message.FormattedMessageFactory;
import org.apache.logging.log4j.message.MessageFactory;
import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
* Represents a manager for custom log files stored inside a log folder.
*/
public class LoggingManager {
/** The default log file extension */
public static final String FILE_EXTENSION = "log";
/** The global context used for all loggers */
private final LoggerContext context;
/** The global message factory used for all loggers */
private final MessageFactory msgFactory;
/** A map of all created logs */
private final Map<String, Logger> logCache;
/** The folder containing the log files */
private final File logFolder;
public LoggingManager(String name, File logFolder) throws IOException {
this.logFolder = logFolder;
if(!logFolder.exists()) {
if(!logFolder.mkdirs()) {
throw new IOException("Could not create log folder");
}
}
this.logCache = new THashMap<String, Logger>();
// Create logger context
this.context = new AsyncLoggerContext(name);
// Create formatted message factory
this.msgFactory = new FormattedMessageFactory();
}
public Logger getLogger(String name) {
Logger logger = logCache.get(name);
// Create a new one
if(logger == null) {
logger = new SimpleLogger(name);
FileAppender appender = FileAppender.createAppender(
new File(logFolder, name + "." + FILE_EXTENSION).getAbsolutePath(),
"true",
"false",
"file_appender-" + name,
"true",
"false",
"true",
PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, null, "UTF-8", "true"),
null,
"false",
null,
null
);
appender.start();
logger.getContext().getConfiguration().getLoggerConfig("root").addAppender(appender, Level.ALL, null);
// Add to log cache
logCache.put(name, logger);
}
// Return the logger
return logger;
}
private class SimpleLogger extends Logger {
public SimpleLogger(String name) {
super(context, name, msgFactory);
// Set to all levels
this.setLevel(Level.ALL);
}
}
}
使用しない場合は、必要に応じtrove
て通常の Java に置き換えることができHashMap
ます。