本文整理汇总了Java中org.apache.logging.log4j.core.appender.RollingFileAppender类的典型用法代码示例。如果您正苦于以下问题:Java RollingFileAppender类的具体用法?Java RollingFileAppender怎么用?Java RollingFileAppender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RollingFileAppender类属于org.apache.logging.log4j.core.appender包,在下文中一共展示了RollingFileAppender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSystemProperties1
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
@Test
public void testSystemProperties1() throws Exception {
final String tempFileName = System.getProperty("java.io.tmpdir") + "/hadoop.log";
final Path tempFilePath = new File(tempFileName).toPath();
Files.deleteIfExists(tempFilePath);
try {
final Configuration configuration = getConfiguration("config-1.2/log4j-system-properties-1.properties");
final RollingFileAppender appender = configuration.getAppender("RFA");
appender.stop(10, TimeUnit.SECONDS);
System.out.println("expected: " + tempFileName + " Actual: " + appender.getFileName());
assertEquals(tempFileName, appender.getFileName());
} finally {
try {
Files.deleteIfExists(tempFilePath);
} catch (FileSystemException e) {
e.printStackTrace();
}
}
}
示例2: registerLogFileTailThreads
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private void registerLogFileTailThreads() throws IOException {
final Collection<String> outputFileNames = new HashSet<>();
final Collection<Appender> loggerAppenders = this.loggerContext.getConfiguration().getAppenders().values();
loggerAppenders.forEach(appender -> {
if (appender instanceof FileAppender) {
outputFileNames.add(((FileAppender) appender).getFileName());
} else if (appender instanceof RandomAccessFileAppender) {
outputFileNames.add(((RandomAccessFileAppender) appender).getFileName());
} else if (appender instanceof RollingFileAppender) {
outputFileNames.add(((RollingFileAppender) appender).getFileName());
} else if (appender instanceof MemoryMappedFileAppender) {
outputFileNames.add(((MemoryMappedFileAppender) appender).getFileName());
} else if (appender instanceof RollingRandomAccessFileAppender) {
outputFileNames.add(((RollingRandomAccessFileAppender) appender).getFileName());
}
});
outputFileNames.forEach(s -> {
final Tailer t = new Tailer(new File(s), new LogTailerListener(), 100, false, true);
final Thread thread = new Thread(t);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName(s);
thread.start();
});
}
示例3: extractFileName
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/**
* File appender types do not share a "write-to-file" superclass.
*/
private String extractFileName(Appender a) {
if (a instanceof FileAppender) {
return ((FileAppender) a).getFileName();
} else if (a instanceof RollingFileAppender) {
return ((RollingFileAppender) a).getFileName();
} else if (a instanceof RollingRandomAccessFileAppender) {
return ((RollingRandomAccessFileAppender) a).getFileName();
} else if (a instanceof RandomAccessFileAppender) {
return ((RandomAccessFileAppender) a).getFileName();
} else if (a instanceof MemoryMappedFileAppender) {
return ((MemoryMappedFileAppender) a).getFileName();
} else {
return null;
}
}
示例4: createFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private static RollingFileAppender createFileAppender(String name, String logFile, String maxSizeKey,
Map<String, Exception> errors)
{
RollingFileAppender.Builder builder = RollingFileAppender.newBuilder();
builder.setConfiguration(new NullConfiguration());
builder.withName(name);
builder.withLayout(getDetailedLayout());
builder.withFileName(logFile);
builder.withFilePattern(logFile);
builder.withAppend(true);
builder.withImmediateFlush(true);
int limit = getLimit(maxSizeKey, errors);
if (limit == 0)
limit = 900 * 1024 * 1024; // Default in Logging.DEFAULT_MAX_FILE_SIZE
builder.withPolicy(SizeBasedTriggeringPolicy.createPolicy(Integer.toString(limit)));
return builder.build();
}
示例5: getLogFolder
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private File getLogFolder() {
org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger("fr.cls.atoll.motu.web.bll.request");
org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger;
org.apache.logging.log4j.core.LoggerContext context = coreLogger.getContext();
RollingFileAppender rfa = context.getConfiguration().getAppender("log-file-infos.queue");
String fileNamePattern = rfa.getFileName();
String logFolderStr = StringUtils.substringBeforeLast(fileNamePattern, "/");
File logFolder = null;
if (logFolderStr.equalsIgnoreCase("motu-log-dir")) {
logFolder = new File(System.getProperty("motu-log-dir"));
} else {
logFolder = new File(logFolderStr);
}
return logFolder;
}
示例6: testRollingFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private void testRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException {
final Configuration configuration = getConfiguration(configResource);
final Appender appender = configuration.getAppender(name);
assertNotNull(appender);
assertEquals(name, appender.getName());
assertTrue(appender.getClass().getName(), appender instanceof RollingFileAppender);
final RollingFileAppender rfa = (RollingFileAppender) appender;
assertEquals("target/hadoop.log", rfa.getFileName());
assertEquals(filePattern, rfa.getFilePattern());
final TriggeringPolicy triggeringPolicy = rfa.getTriggeringPolicy();
assertNotNull(triggeringPolicy);
assertTrue(triggeringPolicy.getClass().getName(), triggeringPolicy instanceof CompositeTriggeringPolicy);
final CompositeTriggeringPolicy ctp = (CompositeTriggeringPolicy) triggeringPolicy;
final TriggeringPolicy[] triggeringPolicies = ctp.getTriggeringPolicies();
assertEquals(1, triggeringPolicies.length);
final TriggeringPolicy tp = triggeringPolicies[0];
assertTrue(tp.getClass().getName(), tp instanceof SizeBasedTriggeringPolicy);
final SizeBasedTriggeringPolicy sbtp = (SizeBasedTriggeringPolicy) tp;
assertEquals(256 * 1024 * 1024, sbtp.getMaxFileSize());
final RolloverStrategy rolloverStrategy = rfa.getManager().getRolloverStrategy();
assertTrue(rolloverStrategy.getClass().getName(), rolloverStrategy instanceof DefaultRolloverStrategy);
final DefaultRolloverStrategy drs = (DefaultRolloverStrategy) rolloverStrategy;
assertEquals(20, drs.getMaxIndex());
configuration.start();
configuration.stop();
}
示例7: testDailyRollingFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private void testDailyRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException {
final Configuration configuration = getConfiguration(configResource);
final Appender appender = configuration.getAppender(name);
assertNotNull(appender);
assertEquals(name, appender.getName());
assertTrue(appender.getClass().getName(), appender instanceof RollingFileAppender);
final RollingFileAppender rfa = (RollingFileAppender) appender;
assertEquals("target/hadoop.log", rfa.getFileName());
assertEquals(filePattern, rfa.getFilePattern());
final TriggeringPolicy triggeringPolicy = rfa.getTriggeringPolicy();
assertNotNull(triggeringPolicy);
assertTrue(triggeringPolicy.getClass().getName(), triggeringPolicy instanceof CompositeTriggeringPolicy);
final CompositeTriggeringPolicy ctp = (CompositeTriggeringPolicy) triggeringPolicy;
final TriggeringPolicy[] triggeringPolicies = ctp.getTriggeringPolicies();
assertEquals(1, triggeringPolicies.length);
final TriggeringPolicy tp = triggeringPolicies[0];
assertTrue(tp.getClass().getName(), tp instanceof TimeBasedTriggeringPolicy);
final TimeBasedTriggeringPolicy tbtp = (TimeBasedTriggeringPolicy) tp;
assertEquals(1, tbtp.getInterval());
final RolloverStrategy rolloverStrategy = rfa.getManager().getRolloverStrategy();
assertTrue(rolloverStrategy.getClass().getName(), rolloverStrategy instanceof DefaultRolloverStrategy);
final DefaultRolloverStrategy drs = (DefaultRolloverStrategy) rolloverStrategy;
assertEquals(Integer.MAX_VALUE, drs.getMaxIndex());
configuration.start();
configuration.stop();
}
示例8: testAccessManagerWithBuilder
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/**
* Not a real test, just make sure we can compile access to the typed manager.
*
* @throws IOException
*/
@Test
public void testAccessManagerWithBuilder() throws IOException {
try (final LoggerContext ctx = LoggerContext.getContext(false)) {
final Configuration config = ctx.getConfiguration();
final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
file.deleteOnExit();
// @formatter:off
final RollingFileAppender appender = RollingFileAppender.newBuilder()
.withFileName(file.getCanonicalPath())
.withFilePattern("FilePattern")
.withName("Name")
.withPolicy(OnStartupTriggeringPolicy.createPolicy(1))
.setConfiguration(config)
.build();
// @formatter:on
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
}
}
示例9: testAccessManagerWithStrings
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/**
* Not a real test, just make sure we can compile access to the typed manager.
*
* @throws IOException
*/
@Test
public void testAccessManagerWithStrings() throws IOException {
try (final LoggerContext ctx = LoggerContext.getContext(false)) {
final Configuration config = ctx.getConfiguration();
final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
file.deleteOnExit();
final RollingFileAppender appender = RollingFileAppender.createAppender(file.getCanonicalPath(),
"FilePattern", null, "Name", null, null, null, OnStartupTriggeringPolicy.createPolicy(1), null,
null, null, null, null, null, config);
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
}
}
示例10: testBuilder
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private void testBuilder() {
// @formatter:off
final RollingFileAppender raf = RollingFileAppender.newBuilder()
.withName("test1")
.withFileName("target/testcmd1.log")
.withFilePattern("target/testcmd1.log.%d{yyyy-MM-dd}")
.withPolicy(createPolicy())
.withStrategy(createStrategy())
.setConfiguration(configuration)
.build();
// @formatter:on
Assert.assertNotNull(raf);
}
示例11: initLogger
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
@SuppressWarnings("deprecation")
private RollingFileAppender initLogger(String loggerName, String filePrefix) {
String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString();
String filePattern = Paths.get(rootPath, filePrefix + "." + loggerName + "-%i.dat").toString();
PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.DEFAULT_CONVERSION_PATTERN).build();
SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);
DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(String.valueOf(maxFileCount),
null,
null,
null,
null,
false,
config);
//TODO:use RollingFileAppender.newBuilder throw No such static method exception,will improve later!
return RollingFileAppender
.createAppender(fileName,
filePattern,
"true",
loggerName,
"true",
null,
"true",
policy,
strategy,
layout,
null,
null,
null,
null,
config);
}
示例12: createRollingFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/**
* Creates a new {@link RollingFileAppender}
* @param fileName The log file
* @param loggingConfig The logging config
* @param layout The layout
* @param strategy the rollover strategy
* @param rolledFileFormat The log file roll pattern
* @param trigger The roll trigger
* @return the new appender
*/
protected Appender createRollingFileAppender(final String fileName, Configuration loggingConfig, PatternLayout layout,
final DefaultRolloverStrategy strategy, final String rolledFileFormat, final TriggeringPolicy trigger) {
RollingFileManager fileManager = RollingFileManager.getFileManager(
fileName,
rolledFileFormat,
false,
true,
trigger,
strategy,
null,
layout,
8192,
true
);
trigger.initialize(fileManager);
return RollingFileAppender.createAppender(
fileName, // file name
rolledFileFormat, // rolled file name pattern
"true", // append
getClass().getSimpleName(), // appender name
"true", // buffered io
"8192", // buffer size
"true", // immediate flush
trigger, // triggering policy
strategy, // rollover strategy
layout, // layout
null, // filter
"true", // ignore exceptions
null, // advertise
null, // advertise uri
loggingConfig); // config
}
示例13: createFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/**
* Creates a {@link FileAppender}.
*
* @param configuration the owner configuration
* @param fileName the name of log file
* @param fileNamePattern the pattern of the name of the log
* file
* @param appenderName the name of the appender
* @param layout the {@link PatternLayout} to use for the
* appender
* @param rolloverInterval how often the log files should be
* rolled over (in DAYS)
* @param maximumFilesToKeep the maximum number of file to
* keep after every roll-over
* @return an instance of {@link Appender}
*/
protected static Appender createFileAppender(final Configuration configuration,
final String fileName, final String fileNamePattern, final String appenderName,
final PatternLayout layout, String rolloverInterval, String maximumFilesToKeep) {
final String append = Boolean.TRUE.toString();
final String bufferedIO = Boolean.TRUE.toString();
final String bufferSizeStr = null;
final String immediateFlush = Boolean.TRUE.toString();
final Filter filter = null;
final String ignore = null;
final String advertise = null;
final String advertiseURI = null;
// Trigger Policy
final String modulate = Boolean.TRUE.toString();
final TriggeringPolicy policy =
TimeBasedTriggeringPolicy.createPolicy(rolloverInterval, modulate);
// Rollover strategy
final String minFilesToKeep = "1";
final String fileIndex = null;
final String compressionLevelStr = Integer.toString(Deflater.DEFAULT_COMPRESSION);
final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy(
maximumFilesToKeep, minFilesToKeep, fileIndex, compressionLevelStr, configuration);
return RollingFileAppender.createAppender(fileName, fileNamePattern, append, appenderName,
bufferedIO, bufferSizeStr, immediateFlush, policy, rolloverStrategy, layout, filter, ignore,
advertise, advertiseURI, configuration);
}
示例14: fileName
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
/** {@inheritDoc} */
@Nullable @Override public String fileName() {
for (Logger log = impl; log != null; log = log.getParent()) {
for (Appender a : log.getAppenders().values()) {
if (a instanceof FileAppender)
return ((FileAppender)a).getFileName();
if (a instanceof RollingFileAppender)
return ((RollingFileAppender)a).getFileName();
if (a instanceof RoutingAppender) {
try {
RoutingAppender routing = (RoutingAppender)a;
Field appsFiled = routing.getClass().getDeclaredField("appenders");
appsFiled.setAccessible(true);
Map<String, AppenderControl> appenders = (Map<String, AppenderControl>)appsFiled.get(routing);
for (AppenderControl control : appenders.values()) {
Appender innerApp = control.getAppender();
if (innerApp instanceof FileAppender)
return normalize(((FileAppender)innerApp).getFileName());
if (innerApp instanceof RollingFileAppender)
return normalize(((RollingFileAppender)innerApp).getFileName());
}
}
catch (IllegalAccessException | NoSuchFieldException e) {
error("Failed to get file name (was the implementation of log4j2 changed?).", e);
}
}
}
}
return null;
}
示例15: buildDailyRollingFileAppender
import org.apache.logging.log4j.core.appender.RollingFileAppender; //导入依赖的package包/类
private void buildDailyRollingFileAppender(final String appenderName) {
final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName,
RollingFileAppender.PLUGIN_NAME);
buildFileAppender(appenderName, appenderBuilder);
final String fileName = getLog4jAppenderValue(appenderName, "File");
final String datePattern = getLog4jAppenderValue(appenderName, "DatePattern", fileName + "'.'yyyy-MM-dd");
appenderBuilder.addAttribute("filePattern", fileName + "%d{" + datePattern + "}");
final ComponentBuilder<?> triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("TimeBasedTriggeringPolicy").addAttribute("modulate", true));
appenderBuilder.addComponent(triggeringPolicy);
appenderBuilder
.addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", Integer.MAX_VALUE));
builder.add(appenderBuilder);
}