ITPub博客

首页 > Linux操作系统 > Linux操作系统 > jclouds日志工具介绍

jclouds日志工具介绍

原创 Linux操作系统 作者:jieforest 时间:2012-08-20 11:28:02 0 删除 编辑
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 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博客 ” ,链接:https://blog.itpub.net/301743/viewspace-741380/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Griffon 1.0.2发布
请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    441
  • 访问量
    602348