jclouds日誌工具介紹

jieforest發表於2012-08-20
TL;DR For basic header and wire logging in jclouds:

1. Download logback
2. Put the JARs on your classpath
3. Put this logback.xml on your classpath
4. Use the SLF4JLoggingModule()
5. Run

I’m still learning jclouds and one thing that helps me out a lot is if I can get some insight into what’s going on under the hood. Specifically, since what we’re really doing is talking to an HTTP API, I like to be able to see what’s going over the wire. Fortunately jclouds allows for some pretty fine grained logging out-of-the-box and it’s relatively easy to configure too…once you know where everything goes.

jclouds itself uses SLF4J Logging in its examples so let’s try that. I’m building off of my previous post Getting started with jclouds so the directory structure is the same as that. Here’s the Java app I’m using to try this out. You can see the use of SLF4J Logging from line 43 where I add the SLF4JLoggingModule to a list of modules to be injected into the ComputeServiceContext.

CODE:

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 modules = ImmutableSet. 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();
        }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-741380/,如需轉載,請註明出處,否則將追究法律責任。

相關文章