本文整理匯總了Java中ch.qos.logback.core.rolling.TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy方法的典型用法代碼示例。如果您正苦於以下問題:Java TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy方法的具體用法?Java TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy怎麽用?Java TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ch.qos.logback.core.rolling.TimeBasedRollingPolicy
的用法示例。
在下文中一共展示了TimeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setUp
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final RollingFileAppender<LoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setFile("application.log");
final TimeBasedRollingPolicy<LoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
_context = new LoggerContext();
_triggeringPolicy = new SizeAndRandomizedTimeBasedFNATP<>(_wrappedPolicy);
rollingPolicy.setContext(_context);
rollingPolicy.setFileNamePattern("application-%d{yyyy-MM-dd_HH}.log");
rollingPolicy.setParent(fileAppender);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(_triggeringPolicy);
rollingPolicy.start();
}
示例2: createDailyLogAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
/**
* Create an appender that will create a new log each day
*
* @param context
* @param encoder
* @return An appender that matches the set up of the logger builder
*/
private RollingFileAppender<ILoggingEvent> createDailyLogAppender(LoggerContext context, Encoder<ILoggingEvent> encoder){
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
appender.setEncoder(encoder);
appender.setFile(file);
TimeBasedRollingPolicy<ILoggingEvent> rolling = new TimeBasedRollingPolicy<>();
rolling.setContext(context);
rolling.setParent(appender);
rolling.setFileNamePattern(getFileWithPattern("%d"));
//Set the maximum number of logs, either to the user specified setting or default to 1
if (maxNumberLogs.isPresent() && maxNumberLogs.get() >= 0) {
rolling.setMaxHistory(maxNumberLogs.get());
} else {
rolling.setMaxHistory(1);
}
//Do we need to also split files by size?
if (divideBasedOnSize()) {
SizeAndTimeBasedFNATP<ILoggingEvent> sizeBased = new SizeAndTimeBasedFNATP<>();
sizeBased.setContext(context);
sizeBased.setMaxFileSize(getMaxFileSize());
sizeBased.setTimeBasedRollingPolicy(rolling);
rolling.setTimeBasedFileNamingAndTriggeringPolicy(sizeBased);
}
rolling.start();
if(rolling.getTimeBasedFileNamingAndTriggeringPolicy() != null){
rolling.getTimeBasedFileNamingAndTriggeringPolicy().start();
}
appender.setRollingPolicy(rolling);
return appender;
}
示例3: testComputeNextCheck
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Test
public void testComputeNextCheck() throws Exception {
final ZonedDateTime dateTime = ZonedDateTime.parse("2014-05-05T00:00:00Z");
final RollingFileAppender<LoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setFile("application.log");
final TimeBasedRollingPolicy<LoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
final RandomizedTimeBasedFNATP<LoggingEvent> triggeringPolicy = new RandomizedTimeBasedFNATP<>();
rollingPolicy.setContext(new LoggerContext());
rollingPolicy.setFileNamePattern("application-%d{yyyy-MM-dd_HH}.log");
rollingPolicy.setParent(fileAppender);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
triggeringPolicy.setCurrentTime(dateTime.toInstant().toEpochMilli());
rollingPolicy.start();
// This should set the nextCheck to 2014-0505T01:00:00Z + random offset
triggeringPolicy.computeNextCheck();
Assert.assertThat(
triggeringPolicy.getNextCheck(),
Matchers.greaterThanOrEqualTo(dateTime.toInstant().toEpochMilli()));
Assert.assertThat(
triggeringPolicy.getNextCheck(),
Matchers.lessThanOrEqualTo(dateTime.toInstant().toEpochMilli() + triggeringPolicy.getMaxOffsetInMillis()));
final LoggingEvent event = new LoggingEvent();
triggeringPolicy.setCurrentTime(ZonedDateTime.parse("2014-05-05T00:59:59Z").toInstant().toEpochMilli());
Assert.assertFalse(triggeringPolicy.isTriggeringEvent(new File("application.log"), event));
triggeringPolicy.setCurrentTime(ZonedDateTime.parse("2014-05-05T02:00:00Z").toInstant().toEpochMilli());
Assert.assertTrue(triggeringPolicy.isTriggeringEvent(new File("application.log"), event));
}
示例4: testSetMaxOffset
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Test
public void testSetMaxOffset() throws Exception {
final ZonedDateTime dateTime = ZonedDateTime.parse("2014-05-05T00:00:00Z");
final RollingFileAppender<LoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setFile("application.log");
final SecureRandom secureRandom = Mockito.mock(SecureRandom.class);
Mockito.doReturn(Double.valueOf(0.1)).when(secureRandom).nextDouble();
final SecureRandomProvider secureRandomProvider = Mockito.mock(SecureRandomProvider.class);
Mockito.when(secureRandomProvider.get(Mockito.any(byte[].class))).thenReturn(secureRandom);
final TimeBasedRollingPolicy<LoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
final RandomizedTimeBasedFNATP<LoggingEvent> triggeringPolicy = new RandomizedTimeBasedFNATP<>(
secureRandomProvider,
HostProvider.DEFAULT);
rollingPolicy.setContext(new LoggerContext());
rollingPolicy.setFileNamePattern("application-%d{yyyy-MM-dd_HH}.log");
rollingPolicy.setParent(fileAppender);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
triggeringPolicy.setCurrentTime(dateTime.toInstant().toEpochMilli());
triggeringPolicy.setMaxOffsetInMillis(30000);
Assert.assertEquals(30000, triggeringPolicy.getMaxOffsetInMillis());
rollingPolicy.start();
Mockito.verify(secureRandomProvider).get(Mockito.any(byte[].class));
Mockito.verify(secureRandom).nextDouble();
// This should set the nextCheck to 2014-0505T01:00:00Z + random offset
triggeringPolicy.computeNextCheck();
final LoggingEvent event = new LoggingEvent();
triggeringPolicy.setCurrentTime(ZonedDateTime.parse("2014-05-05T01:00:02Z").toInstant().toEpochMilli());
Assert.assertFalse(triggeringPolicy.isTriggeringEvent(new File("application.log"), event));
triggeringPolicy.setCurrentTime(ZonedDateTime.parse("2014-05-05T01:00:03Z").toInstant().toEpochMilli());
Assert.assertTrue(triggeringPolicy.isTriggeringEvent(new File("application.log"), event));
}
示例5: testUnknownHost
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Test
public void testUnknownHost() throws Exception {
final ZonedDateTime dateTime = ZonedDateTime.parse("2014-05-05T00:00:00Z");
final RollingFileAppender<LoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setFile("application.log");
final SecureRandom secureRandom = Mockito.mock(SecureRandom.class);
Mockito.doReturn(Double.valueOf(0.1)).when(secureRandom).nextDouble();
final SecureRandomProvider secureRandomProvider = Mockito.mock(SecureRandomProvider.class);
Mockito.when(secureRandomProvider.get()).thenReturn(secureRandom);
final HostProvider hostProvider = Mockito.mock(HostProvider.class);
Mockito.when(hostProvider.get()).thenThrow(new UnknownHostException());
final RandomizedTimeBasedFNATP<LoggingEvent> triggeringPolicy = new RandomizedTimeBasedFNATP<>(
secureRandomProvider,
hostProvider);
triggeringPolicy.setMaxOffsetInMillis(30000);
Assert.assertEquals(30000, triggeringPolicy.getMaxOffsetInMillis());
final TimeBasedRollingPolicy<LoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setContext(new LoggerContext());
rollingPolicy.setFileNamePattern("application-%d{yyyy-MM-dd_HH}.log");
rollingPolicy.setParent(fileAppender);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
triggeringPolicy.setCurrentTime(dateTime.toInstant().toEpochMilli());
rollingPolicy.start();
Mockito.verify(secureRandomProvider).get();
Mockito.verify(secureRandom).nextDouble();
}
示例6: PhialLogger
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
/**
* Creates Logger that will write logs in html file using slf4j and logback.
* The logs will be included in Phial Attachment
*
* @param context application context
*/
public PhialLogger(Context context) {
logDir = createLogDir(context);
clearOldLogs(logDir);
final String logDirectory = logDir.getAbsolutePath();
// reset the default context (which may already have been initialized)
// since we want to reconfigure it
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
rollingFileAppender.setContext(loggerContext);
rollingFileAppender.setAppend(true);
rollingFileAppender.setFile(logDirectory + "/" + LOG_PREFIX + "-latest.html");
SizeAndTimeBasedFNATP<ILoggingEvent> fileNamingPolicy = new SizeAndTimeBasedFNATP<>();
fileNamingPolicy.setContext(loggerContext);
fileNamingPolicy.setMaxFileSize(MAX_FILE_SIZE);
TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setFileNamePattern(logDirectory + "/" + LOG_PREFIX + HISTORY_FILE_NAME_PATTERN);
rollingPolicy.setMaxHistory(5);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(fileNamingPolicy);
rollingPolicy.setParent(rollingFileAppender); // parent and context required!
rollingPolicy.start();
HTMLLayout htmlLayout = new HTMLLayout();
htmlLayout.setContext(loggerContext);
htmlLayout.setPattern(PATTERN);
htmlLayout.start();
LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<>();
encoder.setContext(loggerContext);
encoder.setLayout(htmlLayout);
encoder.start();
rollingFileAppender.setRollingPolicy(rollingPolicy);
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.start();
// add the newly created appenders to the root logger;
// qualify Logger to disambiguate from org.slf4j.Logger
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.DEBUG);
root.addAppender(rollingFileAppender);
// print any status messages (warnings, etc) encountered in logback config
StatusPrinter.print(loggerContext);
}
示例7: buildFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
public static FileAppender<ILoggingEvent> buildFileAppender(LoggingConfiguration.FileConfiguration file,
LoggerContext context,
String logFormat) {
final LogFormatter formatter = new LogFormatter(context, file.getTimeZone());
if (logFormat != null) {
formatter.setPattern(logFormat);
}
formatter.start();
final FileAppender<ILoggingEvent> appender =
file.isArchive() ? new RollingFileAppender<ILoggingEvent>() :
new FileAppender<ILoggingEvent>();
appender.setAppend(true);
appender.setContext(context);
appender.setLayout(formatter);
appender.setFile(file.getCurrentLogFilename());
appender.setPrudent(false);
addThresholdFilter(appender, file.getThreshold());
if (file.isArchive()) {
final DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggeringPolicy =
new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
triggeringPolicy.setContext(context);
final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
rollingPolicy.setContext(context);
rollingPolicy.setFileNamePattern(file.getArchivedLogFilenamePattern());
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
rollingPolicy.setMaxHistory(file.getArchivedFileCount());
((RollingFileAppender<ILoggingEvent>) appender).setRollingPolicy(rollingPolicy);
((RollingFileAppender<ILoggingEvent>) appender).setTriggeringPolicy(triggeringPolicy);
rollingPolicy.setParent(appender);
rollingPolicy.start();
}
appender.stop();
appender.start();
return appender;
}
示例8: getFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
private FileAppender<ILoggingEvent> getFileAppender(final LoggingSettings.LogFile settings,
final LoggerContext context) {
final PatternLayout formatter = getPatternLayout(context);
formatter.setPattern(settings.getLogFormat());
formatter.start();
final FileAppender<ILoggingEvent> appender =
settings.isArchive() ? new RollingFileAppender<ILoggingEvent>() :
new FileAppender<ILoggingEvent>();
appender.setAppend(true);
appender.setContext(context);
appender.setLayout(formatter);
appender.setFile(settings.getCurrentLogFilename());
appender.setPrudent(false);
addThresholdFilter(appender, settings.getThreshold());
if (settings.isArchive()) {
final DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggeringPolicy =
new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
triggeringPolicy.setContext(context);
triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
rollingPolicy.setContext(context);
rollingPolicy.setFileNamePattern(settings.getArchivedLogFilenamePattern());
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
rollingPolicy.setMaxHistory(settings.getArchivedFileCount());
((RollingFileAppender<ILoggingEvent>)appender).setRollingPolicy(rollingPolicy);
((RollingFileAppender<ILoggingEvent>)appender).setTriggeringPolicy(triggeringPolicy);
rollingPolicy.setParent(appender);
rollingPolicy.start();
}
appender.stop();
appender.start();
return appender;
}
示例9: dynamic
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
private static Logger dynamic(String name, LogLevel level) {
// ( LoggerContext ) LoggerFactory.getILoggerFactory();
/*
* String thName = Thread.currentThread().getName(); if( thName != null
* && !thName.trim().isEmpty() ) name = name + "_" +
* thName.trim().replaceAll( " ", "_" );
*/
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread][%-5level][%logger{36}] - %msg%n");
encoder.setContext(context);
encoder.setCharset(Charset.forName("UTF-8"));
DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
timeBasedTriggeringPolicy.setContext(context);
TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
timeBasedRollingPolicy.setContext(context);
String workspace = System.getenv("WORKSPACE");
if (workspace == null)
workspace = "";
if (!workspace.isEmpty() && !workspace.endsWith("/"))
workspace += "/";
timeBasedRollingPolicy.setFileNamePattern(workspace + "logs/" + name + "-%d{yyyy-MM-dd}.log");
timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);
timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setAppend(false);
rollingFileAppender.setContext(context);
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.setFile("logs/" + name + ".log");
rollingFileAppender.setName(name + "Appender");
rollingFileAppender.setPrudent(true); // set to true to release the hold
// of log files.
rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);
timeBasedRollingPolicy.setParent(rollingFileAppender);
encoder.start();
timeBasedRollingPolicy.start();
rollingFileAppender.stop();
rollingFileAppender.start();
Logger logger = context.getLogger(name);
logger.setLevel(level.convert());
logger.setAdditive(false);
logger.addAppender(rollingFileAppender);
return logger;
}
示例10: initLogs
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
private static void initLogs() {
Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
LoggerContext lc = logbackLogger.getLoggerContext();
Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
rollingPolicy.setMaxHistory(3);
SizeAndTimeBasedFNATP<ILoggingEvent> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<ILoggingEvent>();
sizeAndTimeBasedFNATP.setMaxFileSize("2MB");
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP);
rollingPolicy.setFileNamePattern(context.getFilesDir().getPath() + "/logs/old/flickruploader.%d{yyyy-MM-dd}.%i.log");
rollingPolicy.setContext(lc);
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(lc);
fileAppender.setFile(getLogFilePath());
fileAppender.setRollingPolicy(rollingPolicy);
fileAppender.setTriggeringPolicy(rollingPolicy);
rollingPolicy.setParent(fileAppender);
PatternLayoutEncoder pl = new PatternLayoutEncoder();
pl.setContext(lc);
pl.setCharset(Charset.defaultCharset());
pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %class{0}.%method:%L > %msg%n");
pl.setImmediateFlush(false);
pl.start();
fileAppender.setEncoder(pl);
fileAppender.setName("file");
rollingPolicy.start();
fileAppender.start();
if (Config.isDebug()) {
final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder();
logcatTagPattern.setContext(lc);
logcatTagPattern.setPattern("%class{0}");
logcatTagPattern.start();
final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder();
logcatPattern.setContext(lc);
logcatPattern.setPattern("[%thread] %method:%L > %msg%n");
logcatPattern.start();
final LogcatAppender logcatAppender = new LogcatAppender();
logcatAppender.setContext(lc);
logcatAppender.setTagEncoder(logcatTagPattern);
logcatAppender.setEncoder(logcatPattern);
logcatAppender.start();
rootLogger.addAppender(logcatAppender);
}
rootLogger.addAppender(fileAppender);
}
示例11: buildFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
public static FileAppender<ILoggingEvent> buildFileAppender(FileLoggingConfiguration file,
LoggerContext context) {
final LogFormatter formatter = new LogFormatter(context, file.getTimeZone());
for (String format : file.getLogFormat().asSet()) {
formatter.setPattern(format);
}
formatter.start();
final FileAppender<ILoggingEvent> appender =
file.isArchive() ? new RollingFileAppender<ILoggingEvent>() :
new FileAppender<ILoggingEvent>();
appender.setAppend(true);
appender.setContext(context);
appender.setLayout(formatter);
appender.setFile(file.getCurrentLogFilename());
appender.setPrudent(false);
addThresholdFilter(appender, file.getThreshold());
if (file.isArchive()) {
final DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggeringPolicy =
new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
triggeringPolicy.setContext(context);
final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
rollingPolicy.setContext(context);
rollingPolicy.setFileNamePattern(file.getArchivedLogFilenamePattern());
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(
triggeringPolicy);
triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
rollingPolicy.setMaxHistory(file.getArchivedFileCount());
((RollingFileAppender<ILoggingEvent>) appender).setRollingPolicy(rollingPolicy);
((RollingFileAppender<ILoggingEvent>) appender).setTriggeringPolicy(triggeringPolicy);
rollingPolicy.setParent(appender);
rollingPolicy.start();
}
appender.stop();
appender.start();
return appender;
}