Skip to content

Commit 45ae851

Browse files
committed
Add Logging examples.
1 parent dade986 commit 45ae851

File tree

6 files changed

+220
-1
lines changed

6 files changed

+220
-1
lines changed

logging/list.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START list]
17+
// [START auth]
18+
var projectId = process.env.TEST_PROJECT_ID;
19+
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;
20+
21+
projectId = projectId || '<your-project-id>';
22+
keyFilename = keyFilename || '/path/to/keyfile.json';
23+
24+
// [START require]
25+
var gcloud = require('gcloud')({
26+
projectId: projectId,
27+
keyFilename: keyFilename
28+
});
29+
// [END require]
30+
// [END auth]
31+
32+
var logging = gcloud.logging();
33+
34+
function list(callback) {
35+
// Retrieve 3 log entries.
36+
logging.getEntries({
37+
pageSize: 3
38+
}, callback);
39+
}
40+
// [END list]
41+
42+
exports.list = list;
43+
44+
if (module === require.main) {
45+
list(function (err, result) {
46+
console.log(err, result);
47+
});
48+
}

logging/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "nodejs-docs-samples-logging",
3+
"description": "Node.js samples for Google Cloud Logging.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"scripts": {
8+
"list": "node list.js",
9+
"write": "node write.js"
10+
},
11+
"dependencies": {
12+
"gcloud": "^0.27.0"
13+
}
14+
}

logging/write.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
/* jshint camelcase:false */
15+
'use strict';
16+
17+
// [START write]
18+
// [START setup]
19+
var projectId = process.env.TEST_PROJECT_ID;
20+
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;
21+
22+
projectId = projectId || '<your-project-id>';
23+
keyFilename = keyFilename || '/path/to/keyfile.json';
24+
25+
var gcloud = require('gcloud')({
26+
projectId: projectId,
27+
keyFilename: keyFilename
28+
});
29+
30+
var logging = gcloud.logging();
31+
// [END setup]
32+
33+
function write(callback) {
34+
var log = logging.log('myLog');
35+
36+
// Modify this resource type to match a resource in your project
37+
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
38+
// /v2beta1/monitoredResourceDescriptors/list
39+
var resource = {
40+
type: 'gae_app',
41+
labels: {
42+
module_id: 'default',
43+
version_id: 'express'
44+
}
45+
};
46+
47+
var entry = log.entry(resource, {
48+
foo: 'bar'
49+
});
50+
51+
var secondEntry = log.entry(resource, {
52+
beep: 'boop'
53+
});
54+
55+
// You can log multiple entries one at a a time, but it is best to write
56+
// multiple entires together in a batch.
57+
log.write([
58+
entry,
59+
secondEntry
60+
], callback);
61+
}
62+
// [END write]
63+
64+
// [START deleteLog]
65+
function deleteLog(callback) {
66+
var log = logging.log('myLog');
67+
68+
// Delete the logs
69+
log.delete(callback);
70+
}
71+
// [END deleteLog]
72+
73+
exports.write = write;
74+
exports.deleteLog = deleteLog;
75+
76+
if (module === require.main) {
77+
write(function (err, apiResponse) {
78+
console.log(err, apiResponse);
79+
if (err) {
80+
return;
81+
}
82+
deleteLog(function (err, apiResponse) {
83+
console.log(err, apiResponse);
84+
});
85+
});
86+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
2727
"deps_datastore": "cd datastore && npm i && cd ../..",
2828
"deps_storage": "cd storage && npm i && cd ../..",
29+
"deps_logging": "cd logging && npm i && cd ../..",
2930
"deps_express": "cd appengine/express && npm i && cd ../..",
3031
"deps_sendgrid": "cd appengine/sendgrid && npm i && cd ../.. && cd computeengine/sendgrid && npm i && cd ../..",
3132
"deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..",
3233
"pretest_geddy": "cd appengine/geddy && npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;",
33-
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
34+
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_logging && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
3435
"test": "npm run jshint && npm run cover"
3536
},
3637
"devDependencies": {

test/logging/list.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
18+
var logging = require('../../logging/list');
19+
20+
describe('logging/list', function () {
21+
it('should list entries', function (done) {
22+
logging.list(function (err, entries) {
23+
if (err) {
24+
return done(err);
25+
}
26+
assert.ok(Array.isArray(entries), 'should have got an array');
27+
assert.equal(entries.length, 3, 'should have three entries');
28+
done();
29+
});
30+
});
31+
});

test/logging/write.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
18+
var logging = require('../../logging/write');
19+
20+
describe('logging/write', function () {
21+
it('should write entries', function (done) {
22+
logging.write(function (err, apiResponse) {
23+
if (err) {
24+
return done(err);
25+
}
26+
assert.deepEqual(apiResponse, {}, 'should have correct response');
27+
done();
28+
});
29+
});
30+
it('should delete entries', function (done) {
31+
logging.deleteLog(function (err, apiResponse) {
32+
if (err) {
33+
return done(err);
34+
}
35+
assert.deepEqual(apiResponse, {}, 'should have correct response');
36+
done();
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)