use of co.cask.cdap.api.logging.AppenderContext in project cdap by caskdata.
the class LocalLogAppender method start.
@Override
public void start() {
if (!started.compareAndSet(false, true)) {
return;
}
// Load and starts all configured log processing pipelines
LogPipelineLoader pipelineLoader = new LogPipelineLoader(cConf);
Map<String, LogPipelineSpecification<AppenderContext>> specs = pipelineLoader.load(new Provider<AppenderContext>() {
@Override
public AppenderContext get() {
return new LocalAppenderContext(datasetFramework, txClient, locationFactory, metricsCollectionService);
}
});
// Use the event delay as the sync interval
long syncIntervalMillis = cConf.getLong(Constants.Logging.PIPELINE_EVENT_DELAY_MS);
for (LogPipelineSpecification<AppenderContext> spec : specs.values()) {
LogProcessorPipelineContext context = new LogProcessorPipelineContext(cConf, spec.getName(), spec.getContext(), spec.getContext().getMetricsContext(), spec.getContext().getInstanceId());
LocalLogProcessorPipeline pipeline = new LocalLogProcessorPipeline(context, syncIntervalMillis);
pipeline.startAndWait();
pipelineThreads.add(pipeline.getAppenderThread());
pipelines.add(pipeline);
}
super.start();
}
use of co.cask.cdap.api.logging.AppenderContext in project cdap by caskdata.
the class RollingLocationLogAppenderTest method testFileClose.
@Test
public void testFileClose() throws Exception {
// assume SLF4J is bound to logback in the current environment
AppenderContext appenderContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(appenderContext);
// Call context.reset() to clear any previous configuration, e.g. default
// configuration. For multi-step configuration, omit calling context.reset().
appenderContext.reset();
configurator.doConfigure(getClass().getResourceAsStream("/rolling-appender-logback-test.xml"));
StatusPrinter.printInCaseOfErrorsOrWarnings(appenderContext);
RollingLocationLogAppender rollingAppender = (RollingLocationLogAppender) appenderContext.getLogger(RollingLocationLogAppenderTest.class).getAppender("rollingAppender");
addTagsToMdc("testNs", "testApp");
Logger logger = appenderContext.getLogger(RollingLocationLogAppenderTest.class);
ingestLogs(logger, 20);
// wait for 500 ms so that file is eligible for closing
Thread.sleep(500);
// flush to make sure file is closed
rollingAppender.flush();
Assert.assertEquals(0, rollingAppender.getLocationManager().getActiveLocations().size());
}
use of co.cask.cdap.api.logging.AppenderContext in project cdap by caskdata.
the class RollingLocationLogAppenderTest method testRollingLocationLogAppender.
@Test
public void testRollingLocationLogAppender() throws Exception {
// assume SLF4J is bound to logback in the current environment
AppenderContext appenderContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(appenderContext);
// Call context.reset() to clear any previous configuration, e.g. default
// configuration. For multi-step configuration, omit calling context.reset().
appenderContext.reset();
configurator.doConfigure(getClass().getResourceAsStream("/rolling-appender-logback-test.xml"));
StatusPrinter.printInCaseOfErrorsOrWarnings(appenderContext);
RollingLocationLogAppender rollingAppender = (RollingLocationLogAppender) appenderContext.getLogger(RollingLocationLogAppenderTest.class).getAppender("rollingAppender");
addTagsToMdc("testNamespace", "testApp");
Logger logger = appenderContext.getLogger(RollingLocationLogAppenderTest.class);
ingestLogs(logger, 5);
Map<LocationIdentifier, LocationOutputStream> activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(1, activeFiles.size());
verifyFileOutput(activeFiles, 5);
// different program should go to different directory
addTagsToMdc("testNamespace", "testApp1");
ingestLogs(logger, 5);
activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(2, activeFiles.size());
verifyFileOutput(activeFiles, 5);
// different program should go to different directory because namespace is different
addTagsToMdc("testNamespace1", "testApp1");
ingestLogs(logger, 5);
activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(3, activeFiles.size());
verifyFileOutput(activeFiles, 5);
}
use of co.cask.cdap.api.logging.AppenderContext in project cdap by caskdata.
the class CDAPLogAppenderTest method testCDAPLogAppender.
@Test
public void testCDAPLogAppender() throws Exception {
int syncInterval = 1024 * 1024;
CDAPLogAppender cdapLogAppender = new CDAPLogAppender();
cdapLogAppender.setSyncIntervalBytes(syncInterval);
cdapLogAppender.setMaxFileLifetimeMs(TimeUnit.DAYS.toMillis(1));
cdapLogAppender.setMaxFileSizeInBytes(104857600);
cdapLogAppender.setDirPermissions("700");
cdapLogAppender.setFilePermissions("600");
cdapLogAppender.setFileRetentionDurationDays(1);
cdapLogAppender.setLogCleanupIntervalMins(10);
cdapLogAppender.setFileCleanupTransactionTimeout(30);
AppenderContext context = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
context.start();
cdapLogAppender.setContext(context);
cdapLogAppender.start();
FileMetaDataReader fileMetaDataReader = injector.getInstance(FileMetaDataReader.class);
LoggingEvent event = new LoggingEvent("co.cask.Test", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message", null, null);
Map<String, String> properties = new HashMap<>();
properties.put(NamespaceLoggingContext.TAG_NAMESPACE_ID, "default");
properties.put(ApplicationLoggingContext.TAG_APPLICATION_ID, "testApp");
properties.put(FlowletLoggingContext.TAG_FLOW_ID, "testFlow");
properties.put(FlowletLoggingContext.TAG_FLOWLET_ID, "testFlowlet");
event.setMDCPropertyMap(properties);
cdapLogAppender.doAppend(event);
cdapLogAppender.stop();
context.stop();
try {
List<LogLocation> files = fileMetaDataReader.listFiles(cdapLogAppender.getLoggingPath(properties), 0, Long.MAX_VALUE);
Assert.assertEquals(1, files.size());
LogLocation logLocation = files.get(0);
Assert.assertEquals(LogLocation.VERSION_1, logLocation.getFrameworkVersion());
Assert.assertTrue(logLocation.getLocation().exists());
CloseableIterator<LogEvent> logEventCloseableIterator = logLocation.readLog(Filter.EMPTY_FILTER, 0, Long.MAX_VALUE, Integer.MAX_VALUE);
int logCount = 0;
while (logEventCloseableIterator.hasNext()) {
logCount++;
LogEvent logEvent = logEventCloseableIterator.next();
Assert.assertEquals(event.getMessage(), logEvent.getLoggingEvent().getMessage());
}
logEventCloseableIterator.close();
Assert.assertEquals(1, logCount);
// checking permission
String expectedPermissions = "rw-------";
for (LogLocation file : files) {
Location location = file.getLocation();
Assert.assertEquals(expectedPermissions, location.getPermissions());
}
} catch (Exception e) {
Assert.fail();
}
}
use of co.cask.cdap.api.logging.AppenderContext in project cdap by caskdata.
the class FixedWindowRollingPolicy method start.
@Override
public void start() {
if (fileNamePatternStr != null) {
if (context instanceof AppenderContext) {
AppenderContext context = (AppenderContext) this.context;
fileNamePatternStr = fileNamePatternStr.replace("instanceId", Integer.toString(context.getInstanceId()));
} else if (!Boolean.TRUE.equals(context.getObject(Constants.Logging.PIPELINE_VALIDATION))) {
throw new IllegalStateException("Expected logger context instance of " + AppenderContext.class.getName() + " but got " + context.getClass().getName());
}
fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
} else {
LOG.error(FNP_NOT_SET);
throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET);
}
if (maxIndex < minIndex) {
LOG.warn("MaxIndex {} cannot be smaller than MinIndex {}.", maxIndex, minIndex);
maxIndex = minIndex;
}
if ((maxIndex - minIndex) > MAX_WINDOW_SIZE) {
LOG.warn("Large window sizes are not allowed.");
maxIndex = minIndex + MAX_WINDOW_SIZE;
LOG.warn("MaxIndex reduced to " + maxIndex);
}
IntegerTokenConverter itc = fileNamePattern.getIntegerTokenConverter();
if (itc == null) {
throw new IllegalStateException("FileNamePattern [" + fileNamePattern.getPattern() + "] does not contain a valid IntegerToken");
}
processedIndex = maxIndex;
super.start();
}
Aggregations