Skip to content

Instantly share code, notes, and snippets.

@etoews
Created August 18, 2012 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etoews/3388897 to your computer and use it in GitHub Desktop.
Save etoews/3388897 to your computer and use it in GitHub Desktop.
Logging with jclouds
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Set;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
public class JCloudsLoggingExample {
private ComputeService compute;
public static void main(String[] args) {
JCloudsLoggingExample jCloudsLoggingExample = new JCloudsLoggingExample();
jCloudsLoggingExample.init();
jCloudsLoggingExample.listServers();
jCloudsLoggingExample.close();
}
private void listServers() {
System.out.println("Calling listNodes...");
Set<? extends ComputeMetadata> nodes = compute.listNodes();
System.out.println("Total Number of Nodes = " + nodes.size());
for (ComputeMetadata node: nodes) {
System.out.println("\t" + node);
}
}
private void init() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
if (compute != null) close();
e.printStackTrace();
System.exit(1);
}
});
Iterable<Module> modules = ImmutableSet.<Module> of(
new SLF4JLoggingModule());
String provider = "rackspace-cloudservers-us";
String identity = "my-username";
String apiKey = "my-api-key";
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(identity, apiKey)
.modules(modules)
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
}
private void close() {
compute.getContext().close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment