Search in sources :

Example 16 with LoggingEvent

use of ch.qos.logback.classic.spi.LoggingEvent in project ha-bridge by bwssytems.

the class SystemControl method setupServer.

//	This function sets up the sparkjava rest calls for the hue api
public void setupServer() {
    log.info("System control service started....");
    before(SYSTEM_CONTEXT + "/*", (request, response) -> {
        if (bridgeSettings.getBridgeSecurity().isSecure()) {
            String pathInfo = request.pathInfo();
            if (pathInfo == null || (!pathInfo.equals(SYSTEM_CONTEXT + "/login") && !pathInfo.equals(SYSTEM_CONTEXT + "/habridge/version"))) {
                User authUser = bridgeSettings.getBridgeSecurity().getAuthenticatedUser(request);
                if (authUser == null) {
                    halt(401, "{\"message\":\"User not authenticated\"}");
                }
            }
        }
    });
    // http://ip_address:port/system/habridge/version gets the version of this bridge instance
    get(SYSTEM_CONTEXT + "/habridge/version", (request, response) -> {
        log.debug("Get HA Bridge version: v" + version.getVersion());
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return "{\"version\":\"" + version.getVersion() + "\",\"isSecure\":" + bridgeSettings.getBridgeSecurity().isSecure() + "}";
    });
    // http://ip_address:port/system/logmsgs gets the log messages for the bridge
    get(SYSTEM_CONTEXT + "/logmsgs", (request, response) -> {
        log.debug("Get logmsgs.");
        String logMsgs;
        int count = -1;
        if (cyclicBufferAppender == null)
            reacquireCBA();
        if (cyclicBufferAppender != null) {
            count = cyclicBufferAppender.getLength();
        }
        logMsgs = "[";
        if (count == -1) {
            logMsgs = logMsgs + "{\"message\":\"Failed to locate CyclicBuffer\"}";
        } else if (count == 0) {
            logMsgs = logMsgs + "{\"message\":\"No logging events to display\"}";
        } else {
            LoggingEvent le;
            for (int i = 0; i < count; i++) {
                le = (LoggingEvent) cyclicBufferAppender.get(i);
                logMsgs = logMsgs + (i > 0 ? ",{" : "{") + "\"time\":\"" + dateFormat.format(le.getTimeStamp()) + "\",\"level\":\"" + le.getLevel().levelStr + "\",\"component\":\"" + le.getLoggerName() + "\",\"message\":\"" + TextStringFormatter.forJSON(le.getFormattedMessage()) + "\"}";
            }
        }
        logMsgs = logMsgs + "]";
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return logMsgs;
    });
    // http://ip_address:port/system/logmgmt/loggers gets the logger info for the bridge
    get(SYSTEM_CONTEXT + "/logmgmt/loggers/:all", (request, response) -> {
        log.debug("Get loggers info with showAll argument: " + request.params(":all"));
        Boolean showAll = false;
        if (request.params(":all").equals("true"))
            showAll = true;
        theLogServiceMgr.setShowAll(showAll);
        theLogServiceMgr.init();
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return theLogServiceMgr.getConfiguredLoggers();
    }, new JsonTransformer());
    //      http://ip_address:port/system/setpassword CORS request
    options(SYSTEM_CONTEXT + "/setpassword", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/setpassword which sets a password for a given user
    post(SYSTEM_CONTEXT + "/setpassword", (request, response) -> {
        log.debug("setpassword....");
        String theDecodedPayload = new String(Base64.getDecoder().decode(request.body()));
        User theUser = new Gson().fromJson(theDecodedPayload, User.class);
        String errorMessage = bridgeSettings.getBridgeSecurity().setPassword(theUser);
        if (errorMessage != null) {
            response.status(HttpStatus.SC_BAD_REQUEST);
            errorMessage = "{\"message\":\"" + errorMessage + "\"}";
        } else {
            response.status(HttpStatus.SC_OK);
            bridgeSettings.save(bridgeSettings.getBridgeSettingsDescriptor());
        }
        if (errorMessage == null)
            errorMessage = "{}";
        response.type("application/json");
        return errorMessage;
    });
    //      http://ip_address:port/system/adduser CORS request
    options(SYSTEM_CONTEXT + "/adduser", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/adduser which adds a new user
    put(SYSTEM_CONTEXT + "/adduser", (request, response) -> {
        log.debug("adduser....");
        String theDecodedPayload = new String(Base64.getDecoder().decode(request.body()));
        User theUser = new Gson().fromJson(theDecodedPayload, User.class);
        String errorMessage = theUser.validate();
        if (errorMessage != null) {
            response.status(HttpStatus.SC_BAD_REQUEST);
            errorMessage = "{\"message\":\"" + errorMessage + "\"}";
        } else {
            errorMessage = bridgeSettings.getBridgeSecurity().addUser(theUser);
            if (errorMessage == null) {
                response.status(HttpStatus.SC_OK);
                bridgeSettings.save(bridgeSettings.getBridgeSettingsDescriptor());
            } else {
                response.status(HttpStatus.SC_BAD_REQUEST);
                errorMessage = "{\"message\":\"" + errorMessage + "\"}";
            }
        }
        if (errorMessage == null)
            errorMessage = "{}";
        response.type("application/json");
        return errorMessage;
    });
    //      http://ip_address:port/system/deluser CORS request
    options(SYSTEM_CONTEXT + "/deluser", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/deluser which dels a user
    put(SYSTEM_CONTEXT + "/deluser", (request, response) -> {
        log.debug("deluser....");
        String theDecodedPayload = new String(Base64.getDecoder().decode(request.body()));
        User theUser = new Gson().fromJson(theDecodedPayload, User.class);
        String errorMessage = bridgeSettings.getBridgeSecurity().delUser(theUser);
        if (errorMessage != null) {
            response.status(HttpStatus.SC_BAD_REQUEST);
            errorMessage = "{\"message\":\"" + errorMessage + "\"}";
        } else {
            response.status(HttpStatus.SC_OK);
            bridgeSettings.save(bridgeSettings.getBridgeSettingsDescriptor());
        }
        if (errorMessage == null)
            errorMessage = "{}";
        response.type("application/json");
        return errorMessage;
    });
    //      http://ip_address:port/system/login CORS request
    options(SYSTEM_CONTEXT + "/login", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/login validates the login
    post(SYSTEM_CONTEXT + "/login", (request, response) -> {
        log.debug("login....");
        String theDecodedPayload = new String(Base64.getDecoder().decode(request.body()));
        User theUser = new Gson().fromJson(theDecodedPayload, User.class);
        LoginResult result = bridgeSettings.getBridgeSecurity().validatePassword(theUser);
        if (result.getUser() != null)
            bridgeSettings.getBridgeSecurity().addAuthenticatedUser(request, theUser);
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return result;
    }, new JsonTransformer());
    //      http://ip_address:port/system/presslinkbutton CORS request
    options(SYSTEM_CONTEXT + "/presslinkbutton", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/presslinkbutton which sets the link button for device registration
    put(SYSTEM_CONTEXT + "/presslinkbutton", (request, response) -> {
        log.info("Link button pressed....");
        bridgeSettings.getBridgeControl().setLinkButton(true);
        Timer theTimer = new Timer();
        theTimer.schedule(new LinkButtonPressed(bridgeSettings.getBridgeControl(), theTimer), 30000);
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return "";
    }, new JsonTransformer());
    // http://ip_address:port/system/securityinfo gets the security info for the bridge
    get(SYSTEM_CONTEXT + "/securityinfo", (request, response) -> {
        log.debug("Get security info");
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBridgeSecurity().getSecurityInfo();
    }, new JsonTransformer());
    //      http://ip_address:port/system/changesecurityinfo CORS request
    options(SYSTEM_CONTEXT + "/changesecurityinfo", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/changesecurityinfo which sets the security settings other than passwords and users
    post(SYSTEM_CONTEXT + "/changesecurityinfo", (request, response) -> {
        log.debug("changesecurityinfo....");
        SecurityInfo theInfo = new Gson().fromJson(request.body(), SecurityInfo.class);
        bridgeSettings.getBridgeSecurity().setUseLinkButton(theInfo.isUseLinkButton());
        bridgeSettings.getBridgeSecurity().setSecureHueApi(theInfo.isSecureHueApi());
        bridgeSettings.save(bridgeSettings.getBridgeSettingsDescriptor());
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBridgeSecurity().getSecurityInfo();
    }, new JsonTransformer());
    //      http://ip_address:port/system/logmgmt/update CORS request
    options(SYSTEM_CONTEXT + "/logmgmt/update", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/logmgmt/update which changes logging parameters for the process
    put(SYSTEM_CONTEXT + "/logmgmt/update", (request, response) -> {
        log.debug("update loggers: " + request.body());
        LoggerInfo[] updateLoggers;
        updateLoggers = new Gson().fromJson(request.body(), LoggerInfo[].class);
        LoggingForm theModel = theLogServiceMgr.getModel();
        theModel.setUpdatedLoggers(Arrays.asList(updateLoggers));
        theLogServiceMgr.updateLogLevels();
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return theLogServiceMgr.getConfiguredLoggers();
    }, new JsonTransformer());
    //      http://ip_address:port/system/settings which returns the bridge configuration settings
    get(SYSTEM_CONTEXT + "/settings", (request, response) -> {
        log.debug("bridge settings requested from " + request.ip());
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBridgeSettingsDescriptor();
    }, new JsonTransformer());
    //      http://ip_address:port/system/settings CORS request
    options(SYSTEM_CONTEXT + "/settings", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    //      http://ip_address:port/system/settings which returns the bridge configuration settings
    put(SYSTEM_CONTEXT + "/settings", (request, response) -> {
        log.debug("save bridge settings requested from " + request.ip() + " with body: " + request.body());
        BridgeSettingsDescriptor newBridgeSettings = new Gson().fromJson(request.body(), BridgeSettingsDescriptor.class);
        bridgeSettings.save(newBridgeSettings);
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBridgeSettingsDescriptor();
    }, new JsonTransformer());
    // http://ip_address:port/system/control/reinit CORS request
    options(SYSTEM_CONTEXT + "/control/reinit", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    // http://ip_address:port/system/control/reinit sets the parameter reinit the server
    put(SYSTEM_CONTEXT + "/control/reinit", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return reinit();
    });
    // http://ip_address:port/system/control/stop CORS request
    options(SYSTEM_CONTEXT + "/control/stop", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    // http://ip_address:port/system/control/stop sets the parameter stop the server
    put(SYSTEM_CONTEXT + "/control/stop", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return stop();
    });
    // http://ip_address:port/system/backup/available returns a list of config backup filenames
    get(SYSTEM_CONTEXT + "/backup/available", (request, response) -> {
        log.debug("Get backup filenames");
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBackups();
    }, new JsonTransformer());
    // http://ip_address:port/system/backup/create CORS request
    options(SYSTEM_CONTEXT + "/backup/create", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "PUT");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    put(SYSTEM_CONTEXT + "/backup/create", (request, response) -> {
        log.debug("Create backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        BackupFilename returnFilename = new BackupFilename();
        returnFilename.setFilename(bridgeSettings.backup(aFilename.getFilename()));
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return returnFilename;
    }, new JsonTransformer());
    // http://ip_address:port/system/backup/delete CORS request
    options(SYSTEM_CONTEXT + "/backup/delete", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "POST");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(SYSTEM_CONTEXT + "/backup/delete", (request, response) -> {
        log.debug("Delete backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        if (aFilename != null)
            bridgeSettings.deleteBackup(aFilename.getFilename());
        else
            log.warn("No filename given for delete backup.");
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return "";
    }, new JsonTransformer());
    // http://ip_address:port/system/backup/restore CORS request
    options(SYSTEM_CONTEXT + "/backup/restore", (request, response) -> {
        response.status(HttpStatus.SC_OK);
        response.header("Access-Control-Allow-Origin", request.headers("Origin"));
        response.header("Access-Control-Allow-Methods", "POST");
        response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
        response.header("Content-Type", "text/html; charset=utf-8");
        return "";
    });
    post(SYSTEM_CONTEXT + "/backup/restore", (request, response) -> {
        log.debug("Restore backup: " + request.body());
        BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
        if (aFilename != null) {
            bridgeSettings.restoreBackup(aFilename.getFilename());
            bridgeSettings.loadConfig();
        } else
            log.warn("No filename given for restore backup.");
        response.status(HttpStatus.SC_OK);
        response.type("application/json");
        return bridgeSettings.getBridgeSettingsDescriptor();
    }, new JsonTransformer());
}
Also used : BackupFilename(com.bwssystems.HABridge.dao.BackupFilename) Gson(com.google.gson.Gson) LoggerInfo(com.bwssystems.logservices.LoggerInfo) LoggingForm(com.bwssystems.logservices.LoggingForm) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) Timer(java.util.Timer) JsonTransformer(com.bwssystems.HABridge.util.JsonTransformer)

Example 17 with LoggingEvent

use of ch.qos.logback.classic.spi.LoggingEvent in project cdap-ingest by caskdata.

the class FileTailerMetricsProcessor method writeMetricsHeader.

/**
   * Writes header to metrics file.
   *
   * @param logger the logger
   * @param appender the file appender
   */
private void writeMetricsHeader(ch.qos.logback.classic.Logger logger, RollingFileAppender appender) {
    LOG.debug("Start writing header to file ..");
    String header = new StringBuilder("Current Date").append(",").append("Flow Name").append(",").append("Directory Name").append(",").append("Total Events Read Per File").append(",").append("Total Events Ingested Per File").append(",").append("Min Event Size Per File").append(",").append("Average Event Size Per File").append(",").append("Max Event Size Per File").append(",").append("Min Write Latency Per Stream").append(",").append("Average Write Latency Per Stream").append(",").append("Max Write Latency Per Stream").append("\n").toString();
    appender.doAppend(new LoggingEvent(loggerClass, logger, null, header, null, null));
    LOG.debug("Successfully wrote header");
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent)

Example 18 with LoggingEvent

use of ch.qos.logback.classic.spi.LoggingEvent in project cdap by caskdata.

the class CDAPLogAppenderTest method testCDAPLogAppenderSizeBasedRotation.

@Test
public void testCDAPLogAppenderSizeBasedRotation() throws Exception {
    int syncInterval = 1024 * 1024;
    FileMetaDataReader fileMetaDataReader = injector.getInstance(FileMetaDataReader.class);
    CDAPLogAppender cdapLogAppender = new CDAPLogAppender();
    AppenderContext context = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
    context.start();
    cdapLogAppender.setSyncIntervalBytes(syncInterval);
    cdapLogAppender.setMaxFileLifetimeMs(TimeUnit.DAYS.toMillis(1));
    cdapLogAppender.setMaxFileSizeInBytes(500);
    cdapLogAppender.setDirPermissions("750");
    cdapLogAppender.setFilePermissions("640");
    cdapLogAppender.setFileRetentionDurationDays(1);
    cdapLogAppender.setLogCleanupIntervalMins(10);
    cdapLogAppender.setFileCleanupTransactionTimeout(30);
    cdapLogAppender.setContext(context);
    cdapLogAppender.start();
    Map<String, String> properties = new HashMap<>();
    properties.put(NamespaceLoggingContext.TAG_NAMESPACE_ID, "testSizeRotation");
    properties.put(ApplicationLoggingContext.TAG_APPLICATION_ID, "testApp");
    properties.put(FlowletLoggingContext.TAG_FLOW_ID, "testFlow");
    properties.put(FlowletLoggingContext.TAG_FLOWLET_ID, "testFlowlet");
    long currentTimeMillisEvent1 = System.currentTimeMillis();
    LoggingEvent event1 = getLoggingEvent("co.cask.Test1", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 1", properties);
    event1.setTimeStamp(currentTimeMillisEvent1);
    cdapLogAppender.doAppend(event1);
    // sync updates the file size
    cdapLogAppender.sync();
    long currentTimeMillisEvent2 = System.currentTimeMillis();
    LoggingEvent event2 = getLoggingEvent("co.cask.Test2", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 2", properties);
    event2.setTimeStamp(currentTimeMillisEvent2);
    // one new append, we will rotate to new file as the file size limit is very low and last append exceeded that.
    cdapLogAppender.doAppend(event2);
    cdapLogAppender.stop();
    context.stop();
    try {
        List<LogLocation> files = fileMetaDataReader.listFiles(cdapLogAppender.getLoggingPath(properties), 0, Long.MAX_VALUE);
        Assert.assertEquals(2, files.size());
        assertLogEventDetails(event1, files.get(0));
        assertLogEventDetails(event2, files.get(1));
        Assert.assertEquals(currentTimeMillisEvent1, files.get(0).getEventTimeMs());
        Assert.assertEquals(currentTimeMillisEvent2, files.get(1).getEventTimeMs());
        Assert.assertTrue(files.get(0).getFileCreationTimeMs() >= currentTimeMillisEvent1);
        Assert.assertTrue(files.get(1).getFileCreationTimeMs() >= currentTimeMillisEvent2);
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : HashMap(java.util.HashMap) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) LogLocation(co.cask.cdap.logging.write.LogLocation) FileMetaDataReader(co.cask.cdap.logging.meta.FileMetaDataReader) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) AppenderContext(co.cask.cdap.api.logging.AppenderContext) Test(org.junit.Test)

Example 19 with LoggingEvent

use of ch.qos.logback.classic.spi.LoggingEvent in project cdap by caskdata.

the class CDAPLogAppenderTest method testCDAPLogAppenderRotation.

@Test
public void testCDAPLogAppenderRotation() throws Exception {
    int syncInterval = 1024 * 1024;
    FileMetaDataReader fileMetaDataReader = injector.getInstance(FileMetaDataReader.class);
    CDAPLogAppender cdapLogAppender = new CDAPLogAppender();
    AppenderContext context = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
    context.start();
    cdapLogAppender.setSyncIntervalBytes(syncInterval);
    cdapLogAppender.setMaxFileLifetimeMs(500);
    cdapLogAppender.setMaxFileSizeInBytes(104857600);
    cdapLogAppender.setDirPermissions("750");
    cdapLogAppender.setFilePermissions("640");
    cdapLogAppender.setFileRetentionDurationDays(1);
    cdapLogAppender.setLogCleanupIntervalMins(10);
    cdapLogAppender.setFileCleanupTransactionTimeout(30);
    cdapLogAppender.setContext(context);
    cdapLogAppender.start();
    Map<String, String> properties = new HashMap<>();
    properties.put(NamespaceLoggingContext.TAG_NAMESPACE_ID, "testTimeRotation");
    properties.put(ApplicationLoggingContext.TAG_APPLICATION_ID, "testApp");
    properties.put(FlowletLoggingContext.TAG_FLOW_ID, "testFlow");
    properties.put(FlowletLoggingContext.TAG_FLOWLET_ID, "testFlowlet");
    long currentTimeMillisEvent1 = System.currentTimeMillis();
    LoggingEvent event1 = getLoggingEvent("co.cask.Test1", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 1", properties);
    event1.setTimeStamp(currentTimeMillisEvent1);
    cdapLogAppender.doAppend(event1);
    // Pause pass the max file lifetime ms
    TimeUnit.MILLISECONDS.sleep(500);
    long currentTimeMillisEvent2 = System.currentTimeMillis();
    LoggingEvent event2 = getLoggingEvent("co.cask.Test2", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 2", properties);
    event2.setTimeStamp(currentTimeMillisEvent1 + 1000);
    cdapLogAppender.doAppend(event2);
    cdapLogAppender.stop();
    context.stop();
    try {
        List<LogLocation> files = fileMetaDataReader.listFiles(cdapLogAppender.getLoggingPath(properties), 0, Long.MAX_VALUE);
        Assert.assertEquals(2, files.size());
        assertLogEventDetails(event1, files.get(0));
        assertLogEventDetails(event2, files.get(1));
        Assert.assertEquals(currentTimeMillisEvent1, files.get(0).getEventTimeMs());
        Assert.assertEquals(currentTimeMillisEvent1 + 1000, files.get(1).getEventTimeMs());
        Assert.assertTrue(files.get(0).getFileCreationTimeMs() >= currentTimeMillisEvent1);
        Assert.assertTrue(files.get(1).getFileCreationTimeMs() >= currentTimeMillisEvent2);
        // checking permission
        String expectedPermissions = "rw-r-----";
        for (LogLocation file : files) {
            Location location = file.getLocation();
            Assert.assertEquals(expectedPermissions, location.getPermissions());
        }
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : HashMap(java.util.HashMap) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) LogLocation(co.cask.cdap.logging.write.LogLocation) FileMetaDataReader(co.cask.cdap.logging.meta.FileMetaDataReader) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) AppenderContext(co.cask.cdap.api.logging.AppenderContext) Location(org.apache.twill.filesystem.Location) LogLocation(co.cask.cdap.logging.write.LogLocation) Test(org.junit.Test)

Example 20 with LoggingEvent

use of ch.qos.logback.classic.spi.LoggingEvent in project cdap by caskdata.

the class CDAPLogAppenderTest method getLoggingEvent.

private LoggingEvent getLoggingEvent(String fqcn, Logger logger, Level level, String message, Map<String, String> mdcMap) {
    LoggingEvent event = new LoggingEvent(fqcn, logger, level, message, null, null);
    event.setMDCPropertyMap(mdcMap);
    return event;
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent)

Aggregations

LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)20 Test (org.junit.Test)8 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)5 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)4 Timestamp (com.google.cloud.Timestamp)4 LogEntry (com.google.cloud.logging.LogEntry)4 TransactionSystemClient (org.apache.tephra.TransactionSystemClient)4 LocationFactory (org.apache.twill.filesystem.LocationFactory)4 AppenderContext (co.cask.cdap.api.logging.AppenderContext)3 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)3 LocalAppenderContext (co.cask.cdap.logging.framework.LocalAppenderContext)3 FileMetaDataReader (co.cask.cdap.logging.meta.FileMetaDataReader)3 LogEvent (co.cask.cdap.logging.read.LogEvent)3 LogLocation (co.cask.cdap.logging.write.LogLocation)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LogOffset (co.cask.cdap.logging.read.LogOffset)2 Location (org.apache.twill.filesystem.Location)2 ThresholdFilter (ch.qos.logback.classic.filter.ThresholdFilter)1