jclouds日誌工具介紹
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.
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();
}
}
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
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 告警日誌介紹
- 日誌篇:模組日誌總體介紹
- 日誌收集之filebeat使用介紹
- SQL Server事務日誌介紹SQLServer
- 清除SQL Server日誌的方法介紹SQLServer
- MS SQL Server 事務日誌介紹SQLServer
- 介紹一個.Net遠端日誌元件元件
- mysql二進位制日誌格式介紹MySql
- MySQL中的幾類日誌檔案介紹MySql
- mysql二進位制日誌的引數介紹MySql
- 簡單介紹MySQL列印死鎖日誌的方法MySql
- 日誌工具logback的簡介與配置
- 『學了就忘』Linux日誌管理 — 90、Linux中日誌介紹Linux
- 重新整理 mysql 基礎篇————— 介紹mysql日誌[二]MySql
- spring boot 日誌介紹 以及 logback配置示例Spring Boot
- 【轉】Unix系統日誌介紹和集中採集思路
- PostgreSQL DBA(3) - 日誌分析工具pgbadger簡介SQL
- 『無為則無心』Python日誌 — 64、Python日誌模組logging介紹Python
- cpio工具介紹
- 分散式日誌傳輸系統Databus(一)--系統介紹分散式
- Archived Redo Logs歸檔重做日誌介紹及其優點Hive
- SQL Server資料庫事務日誌序列號(LSN)介紹SQLServer資料庫
- 日誌收集系統PLG(Promtail+Loki+Grafana)介紹及部署AILokiGrafana
- Unity——日誌列印工具Unity
- oracle 日誌收集工具Oracle
- Log 工具列印日誌
- ElasticSearch實戰系列九: ELK日誌系統介紹和安裝Elasticsearch
- 第一部分:介紹 Spdlog 日誌庫
- 關於Linux作業系統日誌的介紹(轉載)Linux作業系統
- uml建模工具介紹
- Oracle BBED 工具介紹Oracle
- 【MySQL】mydumper工具介紹MySql
- Oracle DBV 工具 介紹Oracle
- 【Oracle】Opatch 工具介紹Oracle
- etcdctl工具介紹
- mongodb 日誌分析工具mtoolsMongoDB
- MongoDB 日誌分析工具 mtoolsMongoDB
- 日誌分析工具 Log Parser