本文整理匯總了Java中ch.qos.logback.core.rolling.TimeBasedRollingPolicy.setContext方法的典型用法代碼示例。如果您正苦於以下問題:Java TimeBasedRollingPolicy.setContext方法的具體用法?Java TimeBasedRollingPolicy.setContext怎麽用?Java TimeBasedRollingPolicy.setContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ch.qos.logback.core.rolling.TimeBasedRollingPolicy
的用法示例。
在下文中一共展示了TimeBasedRollingPolicy.setContext方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
protected Appender buildAppender(String name, String fileName, String pattern) {
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setName(name);
appender.setAppend(true);
appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
rolling.setContext(loggerContext);
rolling.setParent(appender);
rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd}");
rolling.start();
appender.setRollingPolicy(rolling);
PatternLayoutEncoder layout = new PatternLayoutEncoder();
layout.setContext(loggerContext);
layout.setPattern(pattern);
layout.setCharset(Charset.forName(getEncoding()));
layout.start();
appender.setEncoder(layout);
// 啟動
appender.start();
return appender;
}
示例2: buildDailyMaxRollingAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
protected Appender buildDailyMaxRollingAppender(String name, String fileName, String pattern, int maxBackupIndex) {
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setName(name);
appender.setAppend(true);
appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
rolling.setContext(loggerContext);
rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}");
rolling.setMaxHistory(maxBackupIndex);
rolling.setParent(appender);
rolling.start();
appender.setRollingPolicy(rolling);
PatternLayoutEncoder layout = new PatternLayoutEncoder();
layout.setContext(loggerContext);
layout.setPattern(pattern);
layout.setCharset(Charset.forName(getEncoding()));
layout.start();
appender.setEncoder(layout);
// 啟動
appender.start();
return appender;
}
示例3: buildAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
protected Appender buildAppender(String name, String fileName, String pattern) {
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setName(name);
appender.setAppend(true);
appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
rolling.setParent(appender);
rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd}");
rolling.setContext(loggerContext);
rolling.start();
appender.setRollingPolicy(rolling);
PatternLayout layout = new PatternLayout();
layout.setPattern(pattern);
layout.setContext(loggerContext);
layout.start();
appender.setLayout(layout);
// 啟動
appender.start();
return appender;
}
示例4: buildDailyMaxRollingAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
protected Appender buildDailyMaxRollingAppender(String name, String fileName, String pattern, int maxBackupIndex) {
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setName(name);
appender.setAppend(true);
appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
rolling.setContext(loggerContext);
rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}");
rolling.setMaxHistory(maxBackupIndex);
rolling.setParent(appender);
rolling.start();
appender.setRollingPolicy(rolling);
PatternLayout layout = new PatternLayout();
layout.setContext(loggerContext);
layout.setPattern(pattern);
layout.start();
appender.setLayout(layout);
// 啟動
appender.start();
return appender;
}
示例5: 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();
}
示例6: addFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
public static void addFileAppender(String name, String logPattern, String filePath, int maxHistory) {
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setFile(filePath);
fileAppender.setEncoder(getPatternLayoutEncoder(logPattern));
fileAppender.setContext(loggerContext);
fileAppender.setName(name);
fileAppender.setAppend(true);
// time based rolling policy
TimeBasedRollingPolicy<ILoggingEvent> logFilePolicy = new TimeBasedRollingPolicy<>();
logFilePolicy.setContext(loggerContext);
logFilePolicy.setParent(fileAppender);
logFilePolicy.setFileNamePattern(getDateBasedFileName(filePath));
logFilePolicy.setMaxHistory(50);
logFilePolicy.start();
fileAppender.setRollingPolicy(logFilePolicy);
fileAppender.start();
registeredAppenders.put(name, fileAppender);
}
示例7: doInitializer
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
/**
* Initializer logback
*
* @param file
* @param level
* @param maxHistory
*/
private void doInitializer(String file, String level, int maxHistory) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
// appender
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(loggerContext);
fileAppender.setName("application");
fileAppender.setFile(file);
fileAppender.setAppend(true);
// policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
policy.setContext(loggerContext);
policy.setMaxHistory(maxHistory);
policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
policy.setParent(fileAppender);
policy.start();
fileAppender.setRollingPolicy(policy);
// encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
rootLogger.addAppender(fileAppender);
rootLogger.setLevel(Level.toLevel(level));
rootLogger.setAdditive(false);
}
示例8: setupLogger
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Override
public void setupLogger() {
String file = checkFile();
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger newLogger =loggerContext.getLogger("ROOT");
//Remove all previously added appenders from this logger instance.
newLogger.detachAndStopAllAppenders();
//define appender
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
//policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
policy.setContext(loggerContext);
policy.setMaxHistory(day);
policy.setFileNamePattern(formateLogFile(file));
policy.setParent(appender);
policy.start();
//encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern(formatePattern);
encoder.start();
//start appender
appender.setRollingPolicy(policy);
appender.setContext(loggerContext);
appender.setEncoder(encoder);
appender.setPrudent(true); //support that multiple JVMs can safely write to the same file.
appender.start();
newLogger.addAppender(appender);
//setup level
setLevel(newLogger);
//remove the appenders that inherited 'ROOT'.
newLogger.setAdditive(false);
}
示例9: doInitializer
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
/**
* Initializer logback
*
* @param file log file
* @param level log level
* @param maxHistory max history
*/
private void doInitializer(String file, String level, int maxHistory) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
// appender
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setContext(loggerContext);
fileAppender.setName("application");
fileAppender.setFile(file);
fileAppender.setAppend(true);
// policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
policy.setContext(loggerContext);
policy.setMaxHistory(maxHistory);
policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
policy.setParent(fileAppender);
policy.start();
fileAppender.setRollingPolicy(policy);
// encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
rootLogger.addAppender(fileAppender);
rootLogger.setLevel(Level.toLevel(level));
rootLogger.setAdditive(false);
}
示例10: createFlusher
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
public static Flusher createFlusher(String name, String dir, String file) {
Preconditions.checkArgument(new File(dir).exists(), String.format("dir[%s] not exist", dir));
// LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
LoggerContext context = new LoggerContext();
Logger logger = context.getLogger(name);
logger.setAdditive(false);
RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.setContext(context);
rollingFileAppender.setName(name + "_appender");
rollingFileAppender.setFile(dir + File.separator + file + ".dat");
rollingFileAppender.setAppend(true);
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setFileNamePattern(dir + File.separator + file + ".%d{yyyy-MM-dd_HH}.dat");
rollingPolicy.setParent(rollingFileAppender);
rollingPolicy.setContext(context);
rollingPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(context);
encoder.setPattern("%msg%n");
encoder.start();
rollingFileAppender.setRollingPolicy(rollingPolicy);
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.start();
logger.addAppender(rollingFileAppender);
return new Flusher(logger);
}
示例11: buildLoggerContext
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
static LoggerContext buildLoggerContext(String stamp, String filename,
boolean safetyMode) {
LoggerContext loggerContext = new LoggerContext();
RollingFileAppender<ILoggingEvent> rfa = new RollingFileAppender<ILoggingEvent>();
PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
patternLayout.setPattern(stamp + " %5p - %-50m%n");
patternLayout.setContext(loggerContext);
patternLayout.start();
rfa.setEncoder(patternLayout);
rfa.setAppend(true);
rfa.setPrudent(safetyMode);
rfa.setContext(loggerContext);
TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
tbrp.setContext(loggerContext);
tbrp.setFileNamePattern(filename+"-%d{"+DATE_PATTERN+"}.log");
tbrp.setParent(rfa);
tbrp.start();
rfa.setRollingPolicy(tbrp);
rfa.start();
ch.qos.logback.classic.Logger root = loggerContext
.getLogger(Logger.ROOT_LOGGER_NAME);
root.addAppender(rfa);
return loggerContext;
}
示例12: 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;
}
示例13: instantiatePolicy
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; //導入方法依賴的package包/類
@Override
protected TimeBasedRollingPolicy<ILoggingEvent> instantiatePolicy(LoggerContext context) {
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
setupBasePolicySettings(policy);
policy.setContext(context);
return policy;
}
示例14: 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));
}
示例15: 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));
}