-
Notifications
You must be signed in to change notification settings - Fork 16
/
Logging.java
108 lines (96 loc) · 4.06 KB
/
Logging.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.examples.rackspace;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.rest.RestContext;
import java.io.Closeable;
import java.util.Set;
/**
* This example shows you how to log what jclouds is doing. This is extremely useful for debugging, submitting bug
* reports, getting help, and figuring exactly what the HTTP requests and resonses look like.
*
* In this example we use the Simple Logging Facade for Java (SLF4J). The implementation of SLF4J that we'll use is
* Logback so you'll need to download the Logback JARs from http://logback.qos.ch/download.html and put them on your
* classpath.
*
* The last key ingredient is the file /jclouds-examples/rackspace/src/main/resources/logback.xml which configures
* the logging. As it is configured right now it will log wire input/output and headers to standard out (STDOUT).
* This means that you will be able to see on your console everything that is sent in the request (marked by ">>")
* and everything received in the response (marked by "<<").
*
* @author Everett Toews
*/
public class Logging implements Closeable {
private final ComputeService compute;
private final RestContext<NovaApi, NovaAsyncApi> nova;
/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) {
Logging logging = new Logging(args[0], args[1]);
try {
logging.getConfiguredZones();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
logging.close();
}
}
public Logging(String username, String apiKey) {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudservers-uk"
String provider = System.getProperty("provider.cs", "rackspace-cloudservers-us");
// This module is responsible for enabling logging
Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule());
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules) // don't forget to add the modules to your context!
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();
}
private void getConfiguredZones() {
// Calling getConfiguredZones() talks to the cloud which gets logged
Set<String> zones = nova.getApi().getConfiguredZones();
System.out.format("Zones%n");
for (String zone : zones) {
System.out.format(" %s%n", zone);
}
}
/**
* Always close your service when you're done with it.
*/
public void close() {
if (compute != null) {
compute.getContext();
}
}
}