public static void main()

in src/main/java/com/aliyun/openservices/log/sample/LoggingSample.java [25:71]


    public static void main(String[] args) throws Exception {

        Client client = new Client(ENDPOINT, ACCESS_KEY, ACCESS_KEY_SECRET);

        // step 1. Get all projects in this region

        ListProjectResponse response = client.ListProject();
        String[] logTypes = new String[]{
                "operation_log",
                "metering",
                "consumergroup_log",
                "logtail_alarm",
                "logtail_profile",
                "logtail_status",
        };
        // step 2. Enable logging for each project
        for (Project project : response.getProjects()) {

            System.out.println(project.getProjectName());

            final String projectName = project.getProjectName();
            List<LoggingDetail> logListToEnable = new ArrayList<LoggingDetail>();
            for (String logType : logTypes) {
                String logstoreToSaveLog;
                if (logType.equals("operation_log")) {
                    // operation-log save to a separated logstore
                    logstoreToSaveLog = "internal-operation_log";
                } else {
                    // the other logs save to a free logstore
                    logstoreToSaveLog = "internal-diagnostic_log";
                }
                logListToEnable.add(new LoggingDetail(logType, logstoreToSaveLog));
            }
            Logging logging = new Logging(PROJECT_TO_STORE_LOG, logListToEnable);
            try {
                // try to update logging configuration first
                client.updateLogging(new UpdateLoggingRequest(projectName, logging));
            } catch (LogException ex) {
                if (ex.GetErrorCode().equals("LoggingNotExist")) {
                    // enable logging if not enabled previously.
                    client.createLogging(new CreateLoggingRequest(projectName, logging));
                } else {
                    throw ex;
                }
            }
        }
    }