Skip to content

Commit 8c33f2d

Browse files
committed
Slight refactor of logging samples.
1 parent 241d28f commit 8c33f2d

File tree

5 files changed

+329
-366
lines changed

5 files changed

+329
-366
lines changed

logging/logs.js

+108-84
Original file line numberDiff line numberDiff line change
@@ -13,114 +13,136 @@
1313

1414
'use strict';
1515

16-
// [START all]
1716
// [START setup]
1817
// By default, the client will authenticate using the service account file
1918
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
2019
// the project specified by the GCLOUD_PROJECT environment variable. See
21-
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
20+
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/latest/guides/authentication
2221
var Logging = require('@google-cloud/logging');
23-
24-
// Instantiate a logging client
25-
var logging = Logging();
2622
// [END setup]
2723

28-
// [START list_log_entries]
29-
/**
30-
* List log entires in the authenticated project.
31-
*
32-
* @param {object} [options] Optional. Configuration options.
33-
* @param {string} [options.filter] Optional. An advanced logs filter. An empty filter matches all log entries.
34-
* @param {number} [options.limit] Optional. Maximum number of logs to return.
35-
* @param {string} [options.sort] Optional. Default: "timestamp desc".
36-
* @param {function} callback The callback function.
37-
*/
38-
function listLogEntries (options, callback) {
39-
// Configuration to pass to Logging#getEntries
40-
var config = {};
41-
42-
if (options.filter) {
43-
config.filter = options.filter;
44-
}
45-
if (options.limit) {
46-
config.pageSize = options.limit;
47-
}
48-
if (options.sort) {
49-
config.orderBy = options.sort;
50-
}
24+
function writeLogEntry (logName, callback) {
25+
var logging = Logging();
26+
var log = logging.log(logName);
27+
28+
// Modify this resource to match a resource in your project
29+
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
30+
var resource = {
31+
// This example targets the "global" resource for simplicity
32+
type: 'global'
33+
};
34+
35+
// A text log entry
36+
var entry = log.entry(resource, 'Hello, world!');
37+
38+
// A structured log entry
39+
var secondEntry = log.entry(resource, {
40+
name: 'King Arthur',
41+
quest: 'Find the Holy Grail',
42+
favorite_color: 'Blue'
43+
});
5144

52-
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging/latest/logging
53-
logging.getEntries(config, function (err, entries) {
45+
// Save the two log entries. You can write entries one at a time, but it is
46+
// best to write multiple entires together in a batch.
47+
log.write([
48+
entry,
49+
secondEntry
50+
], function (err, apiResponse) {
5451
if (err) {
5552
return callback(err);
5653
}
5754

58-
console.log('Found %d entries!', entries.length);
59-
return callback(null, entries);
55+
console.log('Wrote to %s', logName);
56+
return callback(null, apiResponse);
6057
});
6158
}
62-
// [END list_log_entries]
63-
64-
// [START write_log_entry]
65-
/**
66-
* Write a log entry.
67-
*
68-
* @param {object} options Configuration options.
69-
* @param {string} options.name The name of the log to write to.
70-
* @param {object} options.resource The resource to associate with the log entry.
71-
* @param {string|object} options.entry The body of the log entry.
72-
* @param {function} callback The callback function.
73-
*/
74-
function writeLogEntry (options, callback) {
75-
// Get a reference to an existing log
76-
var log = logging.log(options.name);
59+
60+
function writeLogEntryAdvanced (logName, options, callback) {
61+
var logging = Logging();
62+
var log = logging.log(logName);
7763

7864
// Prepare the entry
7965
var entry = log.entry(options.resource, options.entry);
8066

81-
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging/latest/logging/log
82-
log.write(entry, function (err) {
67+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
68+
log.write(entry, function (err, apiResponse) {
69+
if (err) {
70+
return callback(err);
71+
}
72+
73+
console.log('Wrote entry to log: %s', logName);
74+
return callback(null, apiResponse);
75+
});
76+
}
77+
78+
function listLogEntries (logName, callback) {
79+
var logging = Logging();
80+
var log = logging.log(logName);
81+
82+
// List the most recent entries for a given log
83+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
84+
log.getEntries(function (err, entries) {
85+
if (err) {
86+
return callback(err);
87+
}
88+
89+
console.log('Found %d entries!', entries.length);
90+
return callback(null, entries);
91+
});
92+
}
93+
94+
function listLogEntriesAdvanced (filter, limit, sort, callback) {
95+
var logging = Logging();
96+
var options = {};
97+
98+
if (filter) {
99+
// See https://cloud.google.com/logging/docs/view/advanced_filters for more filter information.
100+
options.filter = filter;
101+
}
102+
if (limit) {
103+
options.pageSize = limit;
104+
}
105+
if (sort) {
106+
options.orderBy = sort;
107+
}
108+
109+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
110+
logging.getEntries(options, function (err, entries) {
83111
if (err) {
84112
return callback(err);
85113
}
86114

87-
console.log('Wrote entry to log: %s', options.name);
88-
callback(null);
115+
console.log('Found %d entries!', entries.length);
116+
return callback(null, entries);
89117
});
90118
}
91-
// [END write_log_entry]
92-
93-
// [START delete_log]
94-
/**
95-
* Delete a log.
96-
*
97-
* @param {string} name The name of the log to delete.
98-
* @param {function} callback The callback function.
99-
*/
100-
function deleteLog (name, callback) {
101-
// Get a reference to the Log to be deleted
102-
var log = logging.log(name);
103-
104-
// Delete the log
105-
log.delete(function (err) {
119+
120+
function deleteLog (logName, callback) {
121+
var logging = Logging();
122+
var log = logging.log(logName);
123+
124+
// Deletes a logger and all its entries.
125+
// Note that a deletion can take several minutes to take effect.
126+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=delete
127+
log.delete(function (err, apiResponse) {
106128
if (err) {
107129
return callback(err);
108130
}
109131

110-
console.log('Deleted log: %s', name);
111-
callback(null);
132+
console.log('Deleted log: %s', logName);
133+
return callback(null, apiResponse);
112134
});
113135
}
114-
// [END delete_log]
115-
// [END all]
116136

117137
// The command-line program
118138
var cli = require('yargs');
119139
var utils = require('../utils');
120140

121141
var program = module.exports = {
122-
listLogEntries: listLogEntries,
123142
writeLogEntry: writeLogEntry,
143+
writeLogEntryAdvanced: writeLogEntryAdvanced,
144+
listLogEntries: listLogEntries,
145+
listLogEntriesAdvanced: listLogEntriesAdvanced,
124146
deleteLog: deleteLog,
125147
main: function (args) {
126148
// Run the command-line program
@@ -130,7 +152,7 @@ var program = module.exports = {
130152

131153
cli
132154
.demand(1)
133-
.command('list', 'List log entries.', {
155+
.command('list', 'Lists log entries, optionally filtering, limiting, and sorting results.', {
134156
filter: {
135157
alias: 'f',
136158
type: 'string',
@@ -150,29 +172,31 @@ cli
150172
description: 'Sort results.'
151173
}
152174
}, function (options) {
153-
program.listLogEntries(utils.pick(options, ['filter', 'limit', 'sort']), utils.makeHandler());
175+
program.listLogEntriesAdvanced(options.filter, options.limit, options.sort, utils.makeHandler());
154176
})
155-
.command('write <name> <resource> <entry>', 'Write a log entry.', {}, function (options) {
177+
.command('write <logName> <resource> <entry>', 'Writes a log entry to the specified log.', {}, function (options) {
156178
try {
157179
options.resource = JSON.parse(options.resource);
158180
} catch (err) {
159-
console.error('"resource" must be a valid JSON string!');
181+
return console.error('"resource" must be a valid JSON string!');
160182
}
183+
161184
try {
162185
options.entry = JSON.parse(options.entry);
163-
} catch (err) {
164-
return console.error('"entry" must be a valid JSON string!');
165-
}
166-
program.writeLogEntry(utils.pick(options, ['name', 'resource', 'entry']), utils.makeHandler());
186+
} catch (err) {}
187+
188+
program.writeLogEntryAdvanced(options.logName, utils.pick(options, ['resource', 'entry']), utils.makeHandler(false));
167189
})
168-
.command('delete <name>', 'Delete a Log.', {}, function (options) {
169-
program.deleteLog(options.name, utils.makeHandler(false));
190+
.command('delete <logName>', 'Deletes the specified Log.', {}, function (options) {
191+
program.deleteLog(options.logName, utils.makeHandler(false));
170192
})
171193
.example('node $0 list', 'List all log entries.')
172-
.example('node $0 list -f "severity = ERROR" -s "timestamp" -l 2', 'List up to 2 error entries, sorted by timestamp ascending.')
173-
.example('node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'{"message":"Hello World!"}\'', 'Write a log entry.')
194+
.example('node $0 list -f "severity=ERROR" -s "timestamp" -l 2', 'List up to 2 error entries, sorted by timestamp ascending.')
195+
.example('node $0 list -f \'logName="my-log"\' -l 2', 'List up to 2 log entries from the "my-log" log.')
196+
.example('node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'"Hello World!"\'', 'Write a string log entry.')
197+
.example('node $0 write my-log \'{"type":"global"}\' \'{"message":"Hello World!"}\'', 'Write a JSON log entry.')
174198
.example('node $0 delete my-log', 'Delete "my-log".')
175-
.wrap(100)
199+
.wrap(120)
176200
.recommendCommands()
177201
.epilogue('For more information, see https://cloud.google.com/logging/docs');
178202

logging/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
},
1111
"dependencies": {
1212
"@google-cloud/logging": "^0.1.1",
13+
"@google-cloud/storage": "^0.1.1",
1314
"express": "^4.13.4",
1415
"fluent-logger": "^2.0.1",
15-
"google-cloud": "^0.38.3",
1616
"yargs": "^5.0.0"
1717
},
1818
"devDependencies": {

0 commit comments

Comments
 (0)