Skip to content

Commit fef484d

Browse files
callmehiphopstephenplusplus
authored andcommitted
logging: add promise support (#1707)
1 parent 8a58b32 commit fef484d

9 files changed

Lines changed: 351 additions & 62 deletions

File tree

handwritten/logging/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ logging.getEntries(function(err, entries) {
4949
// `entries` contains all of the entries from the logs in your project.
5050
}
5151
});
52+
53+
// Promises are also supported by omitting callbacks.
54+
logging.getEntries().then(function(data) {
55+
var entries = data[0];
56+
});
57+
58+
// It's also possible to integrate with third-party Promise libraries.
59+
var logging = require('@google-cloud/logging')({
60+
promise: require('bluebird')
61+
});
5262
```
5363

5464

@@ -104,4 +114,4 @@ var logging = require('@google-cloud/logging')({
104114
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using
105115
[dev-console]: https://console.developers.google.com/project
106116
[gcloud-logging-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging
107-
[cloud-logging-docs]: https://cloud.google.com/logging/docs
117+
[cloud-logging-docs]: https://cloud.google.com/logging/docs

handwritten/logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"stackdriver"
5353
],
5454
"dependencies": {
55-
"@google-cloud/common": "^0.6.0",
55+
"@google-cloud/common": "^0.7.0",
5656
"arrify": "^1.0.0",
5757
"extend": "^3.0.0",
5858
"google-gax": "^0.7.0",

handwritten/logging/src/index.js

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ util.inherits(Logging, common.GrpcService);
130130
* }
131131
*
132132
* logging.createSink('new-sink-name', config, callback);
133+
*
134+
* //-
135+
* // If the callback is omitted, we'll return a Promise.
136+
* //-
137+
* logging.createSink('new-sink-name', config).then(function(data) {
138+
* var sink = data[0];
139+
* var apiResponse = data[1];
140+
* });
133141
*/
134142
Logging.prototype.createSink = function(name, config, callback) {
135143
// jscs:enable maximumLineLength
@@ -273,26 +281,11 @@ Logging.prototype.entry = function(resource, data) {
273281
* }, callback);
274282
*
275283
* //-
276-
* // Get the entries from your project as a readable object stream.
284+
* // If the callback is omitted, we'll return a Promise.
277285
* //-
278-
* logging.getEntries()
279-
* .on('error', console.error)
280-
* .on('data', function(entry) {
281-
* // `entry` is a Stackdriver Logging entry object.
282-
* // See the `data` property to read the data from the entry.
283-
* })
284-
* .on('end', function() {
285-
* // All entries retrieved.
286-
* });
287-
*
288-
* //-
289-
* // If you anticipate many results, you can end a stream early to prevent
290-
* // unnecessary processing and API requests.
291-
* //-
292-
* logging.getEntries()
293-
* .on('data', function(entry) {
294-
* this.end();
295-
* });
286+
* logging.getEntries().then(function(data) {
287+
* var entries = data[0];
288+
* });
296289
*/
297290
Logging.prototype.getEntries = function(options, callback) {
298291
if (is.fn(options)) {
@@ -331,6 +324,36 @@ Logging.prototype.getEntries = function(options, callback) {
331324
});
332325
};
333326

327+
/**
328+
* List the {module:logging/entry} objects in your logs as a readable object
329+
* stream.
330+
*
331+
* @param {object=} options - Configuration object. See
332+
* {module:logging#getEntries} for a complete list of options.
333+
* @return {stream}
334+
*
335+
* @example
336+
* logging.getEntriesStream()
337+
* .on('error', console.error)
338+
* .on('data', function(entry) {
339+
* // `entry` is a Stackdriver Logging entry object.
340+
* // See the `data` property to read the data from the entry.
341+
* })
342+
* .on('end', function() {
343+
* // All entries retrieved.
344+
* });
345+
*
346+
* //-
347+
* // If you anticipate many results, you can end a stream early to prevent
348+
* // unnecessary processing and API requests.
349+
* //-
350+
* logging.getEntriesStream()
351+
* .on('data', function(entry) {
352+
* this.end();
353+
* });
354+
*/
355+
Logging.prototype.getEntriesStream = common.paginator.streamify('getEntries');
356+
334357
/**
335358
* Get the sinks associated with this project.
336359
*
@@ -352,25 +375,11 @@ Logging.prototype.getEntries = function(options, callback) {
352375
* });
353376
*
354377
* //-
355-
* // Get the sinks from your project as a readable object stream.
378+
* // If the callback is omitted, we'll return a Promise.
356379
* //-
357-
* logging.getSinks()
358-
* .on('error', console.error)
359-
* .on('data', function(sink) {
360-
* // `sink` is a Sink object.
361-
* })
362-
* .on('end', function() {
363-
* // All sinks retrieved.
364-
* });
365-
*
366-
* //-
367-
* // If you anticipate many results, you can end a stream early to prevent
368-
* // unnecessary processing and API requests.
369-
* //-
370-
* logging.getSinks()
371-
* .on('data', function(sink) {
372-
* this.end();
373-
* });
380+
* logging.getSinks().then(function(data) {
381+
* var sinks = data[0];
382+
* });
374383
*/
375384
Logging.prototype.getSinks = function(options, callback) {
376385
var self = this;
@@ -413,6 +422,35 @@ Logging.prototype.getSinks = function(options, callback) {
413422
});
414423
};
415424

425+
/**
426+
* Get the {module:logging/sink} objects associated with this project as a
427+
* readable object stream.
428+
*
429+
* @param {object=} options - Configuration object. See
430+
* {module:logging#getSinks} for a complete list of options.
431+
* @return {stream}
432+
*
433+
* @example
434+
* logging.getSinksStream()
435+
* .on('error', console.error)
436+
* .on('data', function(sink) {
437+
* // `sink` is a Sink object.
438+
* })
439+
* .on('end', function() {
440+
* // All sinks retrieved.
441+
* });
442+
*
443+
* //-
444+
* // If you anticipate many results, you can end a stream early to prevent
445+
* // unnecessary processing and API requests.
446+
* //-
447+
* logging.getSinksStream()
448+
* .on('data', function(sink) {
449+
* this.end();
450+
* });
451+
*/
452+
Logging.prototype.getSinksStream = common.paginator.streamify('getSinks');
453+
416454
/**
417455
* Get a reference to a Stackdriver Logging log.
418456
*
@@ -557,10 +595,18 @@ Logging.prototype.setAclForTopic_ = function(name, config, callback) {
557595

558596
/*! Developer Documentation
559597
*
560-
* These methods can be used with either a callback or as a readable object
561-
* stream. `streamRouter` is used to add this dual behavior.
598+
* These methods can be auto-paginated.
599+
*/
600+
common.paginator.extend(Logging, ['getEntries', 'getSinks']);
601+
602+
/*! Developer Documentation
603+
*
604+
* All async methods (except for streams) will return a Promise in the event
605+
* that a callback is omitted.
562606
*/
563-
common.streamRouter.extend(Logging, ['getEntries', 'getSinks']);
607+
common.util.promisifyAll(Logging, {
608+
exclude: ['entry', 'log', 'sink']
609+
});
564610

565611
Logging.Entry = Entry;
566612
Logging.Log = Log;

0 commit comments

Comments
 (0)