Skip to content

Commit 8db014a

Browse files
committed
Merge pull request #865 from stephenplusplus/spp--logging
introduce Logging support
2 parents f919245 + ef51f55 commit 8db014a

16 files changed

Lines changed: 3893 additions & 11 deletions

File tree

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This client supports the following Google Cloud Platform services:
1717
* [Google Cloud Storage](#google-cloud-storage)
1818
* [Google Compute Engine](#google-compute-engine)
1919
* [Google Prediction API](#google-prediction-api)
20+
* [Google Cloud Logging](#google-cloud-logging-beta) (Beta)
2021
* [Google Cloud Resource Manager](#google-cloud-resource-manager-beta) (Beta)
2122
* [Google Cloud Search](#google-cloud-search-alpha) (Alpha)
2223

@@ -385,12 +386,62 @@ model.query('Hello', function(err, results) {
385386
// ]
386387
}
387388
});
389+
390+
391+
## Google Cloud Logging (Beta)
392+
393+
> **This is a Beta release of Google Cloud Logging.** This API is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
394+
395+
- [API Documentation][gcloud-logging-docs]
396+
- [Official Documentation][cloud-logging-docs]
397+
398+
```js
399+
// Authenticating on a global-basis. You can also authenticate on a per-API-
400+
// basis (see Authentication section above).
401+
402+
var gcloud = require('gcloud')({
403+
projectId: 'my-project',
404+
keyFilename: '/path/to/keyfile.json'
405+
});
406+
407+
var logging = gcloud.logging();
408+
409+
// Create a sink using a Bucket as a destination.
410+
var gcs = gcloud.storage();
411+
412+
logging.createSink('my-new-sink', {
413+
destination: gcs.bucket('my-sink')
414+
}, function(err, sink) {});
415+
416+
// Write a critical entry to a log.
417+
var syslog = logging.log('syslog');
418+
419+
var resource = {
420+
type: 'gce_instance',
421+
labels: {
422+
zone: 'global',
423+
instance_id: 3
424+
}
425+
};
426+
427+
var entry = syslog.entry(resource, {
428+
delegate: process.env.user
429+
});
430+
431+
syslog.critical(entry, function(err) {});
432+
433+
// Get all entries in your project.
434+
logging.getEntries(function(err, entries) {
435+
if (!err) {
436+
// `entries` contains all of the entries from the logs in your project.
437+
}
438+
});
388439
```
389440

390441

391442
## Google Cloud Resource Manager (Beta)
392443

393-
> This is a *Beta* release of Google Cloud Resource Manager. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
444+
> **This is a Beta release of Google Cloud Resource Manager.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
394445

395446
- [API Documentation][gcloud-resource-docs]
396447
- [Official Documentation][cloud-resource-docs]
@@ -426,7 +477,7 @@ project.getMetadata(function(err, metadata) {
426477

427478
## Google Cloud Search (Alpha)
428479

429-
> This is an *Alpha* release of Google Cloud Search. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
480+
> **This is an Alpha release of Google Cloud Search.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
430481

431482
- [API Documentation][gcloud-search-docs]
432483
- [Official Documentation][cloud-search-docs]
@@ -485,6 +536,7 @@ Apache 2.0 - See [COPYING](COPYING) for more information.
485536
[gcloud-datastore-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/datastore
486537
[gcloud-dns-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/dns
487538
[gcloud-prediction-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/prediction
539+
[gcloud-logging-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/logging
488540
[gcloud-pubsub-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/pubsub
489541
[gcloud-resource-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/resource
490542
[gcloud-search-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/search
@@ -514,6 +566,8 @@ Apache 2.0 - See [COPYING](COPYING) for more information.
514566

515567
[cloud-prediction-docs]: https://cloud.google.com/prediction/docs
516568

569+
[cloud-logging-docs]: https://cloud.google.com/logging/docs
570+
517571
[cloud-pubsub-docs]: https://cloud.google.com/pubsub/docs
518572

519573
[cloud-resource-docs]: https://cloud.google.com/resource-manager

docs/json/master/logging/.gitkeep

Whitespace-only changes.

docs/site/components/docs/docs-values.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ angular.module('gcloud.docs')
135135
]
136136
},
137137

138+
logging: {
139+
title: 'Logging',
140+
_url: '{baseUrl}/logging',
141+
pages: [
142+
{
143+
title: 'Entry',
144+
url: '/entry'
145+
},
146+
{
147+
title: 'Log',
148+
url: '/log'
149+
},
150+
{
151+
title: 'Sink',
152+
url: '/sink'
153+
}
154+
]
155+
},
156+
138157
prediction: {
139158
title: 'Prediction',
140159
_url: '{baseUrl}/prediction',
@@ -275,7 +294,7 @@ angular.module('gcloud.docs')
275294
// introduce Storage#Channel.
276295
'>=0.26.0': ['storageWithChannels'],
277296

278-
// introduce prediction api.
279-
'>=0.27.0': ['prediction']
297+
// introduce prediction & logging api.
298+
'>=0.27.0': ['prediction', 'logging']
280299
}
281300
});

docs/site/components/docs/docs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h3 class="sub-heading">
4444
</article>
4545
<hr>
4646

47-
<article ng-repeat="service in ['bigquery', 'compute', 'datastore', 'dns', 'prediction', 'pubsub', 'resource', 'search', 'storage']"
47+
<article ng-repeat="service in ['bigquery', 'compute', 'datastore', 'dns', 'logging', 'prediction', 'pubsub', 'resource', 'search', 'storage']"
4848
ng-if="isActiveDoc(service)"
4949
ng-include="'site/components/docs/' + service + '-overview.html'">
5050
</article>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h3>Logging Overview</h3>
2+
<p class="notice">
3+
<strong>This is a Beta release of Google Cloud Logging.</strong> This API is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
4+
</p>
5+
<p>
6+
The <code>gcloud.logging</code> method will return a <code>logging</code> object, allowing you to create sinks, write log entries, and more.
7+
</p>
8+
<p>
9+
To learn more about Logging, see the <a href="https://cloud.google.com/logging/docs">What is Google Cloud Logging?</a>
10+
</p>
11+

lib/index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ var apis = {
135135
*/
136136
prediction: require('./prediction'),
137137

138+
/**
139+
* [Google Cloud Logging](https://cloud.google.com/logging/docs) collects and
140+
* stores logs from applications and services on the Google Cloud Platform:
141+
*
142+
* - Export your logs to Google Cloud Storage, Google BigQuery, or Google
143+
* Cloud Pub/Sub.
144+
* - Integrate third-party logs from your virtual machine instances by
145+
* installing the logging agent, `google-fluentd`.
146+
*
147+
* <p class="notice">
148+
* **This is a Beta release of Google Cloud Logging.** This API is not
149+
* covered by any SLA or deprecation policy and may be subject to backward-
150+
* incompatible changes.
151+
* </p>
152+
*
153+
* @type {module:logging}
154+
*
155+
* @return {module:logging}
156+
*
157+
* @example
158+
* var gcloud = require('gcloud');
159+
* var logging = gcloud.logging({
160+
* projectId: 'grape-spaceship-123',
161+
* keyFilename: '/path/to/keyfile.json'
162+
* });
163+
*/
164+
logging: require('./logging'),
165+
138166
/**
139167
* [Google Cloud Pub/Sub](https://developers.google.com/pubsub/overview) is a
140168
* reliable, many-to-many, asynchronous messaging service from Google Cloud
@@ -165,9 +193,9 @@ var apis = {
165193
* - Recover projects.
166194
*
167195
* <p class="notice">
168-
* **This is a *Beta* release of Cloud Resource Manager.** This feature is
169-
* not covered by any SLA or deprecation policy and may be subject to
170-
* backward-incompatible changes.
196+
* **This is a Beta release of Cloud Resource Manager.** This feature is not
197+
* covered by any SLA or deprecation policy and may be subject to backward-
198+
* incompatible changes.
171199
* </p>
172200
*
173201
* @type {module:resource}
@@ -190,9 +218,9 @@ var apis = {
190218
* maintaining a search service.
191219
*
192220
* <p class="notice">
193-
* **This is an *Alpha* release of Google Cloud Search.** This feature is
194-
* not covered by any SLA or deprecation policy and may be subject to
195-
* backward-incompatible changes.
221+
* **This is an Alpha release of Google Cloud Search.** This feature is not
222+
* covered by any SLA or deprecation policy and may be subject to backward-
223+
* incompatible changes.
196224
* </p>
197225
*
198226
* @type {module:search}

lib/logging/entry.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*!
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module logging/entry
19+
*/
20+
21+
'use strict';
22+
23+
var extend = require('extend');
24+
var is = require('is');
25+
26+
/**
27+
* Create an entry object to define new data to insert into a log.
28+
*
29+
* @resource [LogEntry JSON representation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry}
30+
*
31+
* @param {object=|string=} resource - See a
32+
* [Monitored Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource).
33+
* @param {object|string} data - The data to use as the value for this log
34+
* entry.
35+
* @return {module:logging/entry}
36+
*
37+
* @example
38+
* var gcloud = require('gcloud')({
39+
* keyFilename: '/path/to/keyfile.json',
40+
* projectId: 'grape-spaceship-123'
41+
* });
42+
*
43+
* var logging = gcloud.logging();
44+
*
45+
* var syslog = logging.log('syslog');
46+
*
47+
* var resource = {
48+
* type: 'gce_instance',
49+
* labels: {
50+
* zone: 'global',
51+
* instance_id: 3
52+
* }
53+
* };
54+
*
55+
* var entry = syslog.entry(resource, {
56+
* delegate: process.env.user
57+
* });
58+
*
59+
* syslog.alert(entry, function(err, apiResponse) {
60+
* if (!error) {
61+
* // Log entry inserted successfully.
62+
* }
63+
* });
64+
*
65+
* //-
66+
* // You will also receive `Entry` objects when using
67+
* // {module:logging#getEntries} and {module:logging/log#getEntries}.
68+
* //-
69+
* logging.getEntries(function(err, entries) {
70+
* if (!err) {
71+
* // entries[0].data = The data value from the log entry.
72+
* }
73+
* });
74+
*/
75+
function Entry(resource, data) {
76+
if (!data) {
77+
this.data = resource;
78+
return;
79+
}
80+
81+
this.resource = resource;
82+
this.data = data;
83+
}
84+
85+
/**
86+
* Create an Entry object from an API response, such as `entries:list`.
87+
*
88+
* @private
89+
*
90+
* @param {object} entry - An API representation of an entry. See a
91+
* [LogEntry](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry).
92+
* @return {module:logging/entity}
93+
*/
94+
Entry.fromApiResponse_ = function(entry) {
95+
// Only one of these are populated.
96+
var data = entry.protoPayload || entry.jsonPayload || entry.textPayload;
97+
return extend(new Entry(entry.resource, data), entry);
98+
};
99+
100+
/**
101+
* Serialize an entry to the format the API expects.
102+
*
103+
* @private
104+
*/
105+
Entry.prototype.toJSON = function() {
106+
var entry = extend(true, {}, this);
107+
108+
var whitelist = [
109+
'logName',
110+
'resource',
111+
'timestamp',
112+
'severity',
113+
'insertId',
114+
'httpRequest',
115+
'labels',
116+
'operation'
117+
];
118+
119+
for (var prop in entry) {
120+
if (whitelist.indexOf(prop) === -1) {
121+
delete entry[prop];
122+
}
123+
}
124+
125+
if (is.string(this.resource)) {
126+
entry.resource = {
127+
type: this.resource
128+
};
129+
}
130+
131+
if (is.object(this.data)) {
132+
entry.jsonPayload = this.data;
133+
} else if (is.string(this.data)) {
134+
entry.textPayload = this.data;
135+
}
136+
137+
return entry;
138+
};
139+
140+
module.exports = Entry;

0 commit comments

Comments
 (0)