Skip to content

Commit 503fe2f

Browse files
committed
Add Logging examples.
1 parent 2ebd0b4 commit 503fe2f

File tree

16 files changed

+427
-21
lines changed

16 files changed

+427
-21
lines changed

computeengine/sendgrid/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"license": "Apache Version 2.0",
77
"engines": {
8-
"node": "~4.2"
8+
"node": ">=0.10.x"
99
},
1010
"dependencies": {
1111
"sendgrid": "^2.0.0"

datastore/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Datastore Samples
22

3+
These samples require two environment variables to be set:
4+
5+
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
6+
download one from your Google project's "permissions" page.
7+
- `TEST_PROJECT_ID` - Id of your Google project.
8+
39
## Run a sample
410

511
Install dependencies:

datastore/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
"version": "0.0.1",
55
"private": true,
66
"license": "Apache Version 2.0",
7+
"engines": {
8+
"node": ">=0.10.x"
9+
},
710
"scripts": {
811
"tasks": "node tasks.js"
912
},
1013
"dependencies": {
11-
"async": "^1.5.0",
12-
"gcloud": "^0.25.0"
14+
"async": "^1.5.2",
15+
"gcloud": "^0.27.0"
1316
}
1417
}

logging/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Logging Samples
2+
3+
These samples require two environment variables to be set:
4+
5+
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
6+
download one from your Google project's "permissions" page.
7+
- `TEST_PROJECT_ID` - Id of your Google project.
8+
9+
## Run a sample
10+
11+
Install dependencies:
12+
13+
npm install
14+
15+
To print available commands:
16+
17+
npm run
18+
19+
Execute a sample:
20+
21+
npm run <sample>
22+
23+
Example:
24+
25+
npm run write

logging/export.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 setup]
17+
var projectId = process.env.TEST_PROJECT_ID;
18+
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;
19+
20+
projectId = projectId || '<your-project-id>';
21+
keyFilename = keyFilename || '/path/to/keyfile.json';
22+
23+
var gcloud = require('gcloud')({
24+
projectId: projectId,
25+
keyFilename: keyFilename
26+
});
27+
28+
var logging = gcloud.logging();
29+
// [END setup]
30+
31+
// [START listSinks]
32+
function listSinks(callback) {
33+
// list all sinks in the project
34+
logging.getSinks(callback);
35+
}
36+
// [END listSinks]
37+
38+
// [START createSink]
39+
function createSink(callback) {
40+
var gcs = gcloud.storage();
41+
42+
// create a new sink
43+
//
44+
// This method only works if you are authenticated as yourself, e.g. using the
45+
// gcloud SDK.
46+
logging.createSink('mySink', {
47+
destination: gcs.bucket('logging-bucket')
48+
}, callback);
49+
}
50+
// [END createSink]
51+
52+
// [START updateSink]
53+
function updateSink(callback) {
54+
var gcs = gcloud.storage();
55+
var sink = logging.sink('mySink');
56+
57+
// update a sink
58+
//
59+
// This method only works if you are authenticated as yourself, e.g. using the
60+
// gcloud SDK.
61+
sink.setMetadata({
62+
// change destination to something else
63+
destination: gcs.bucket('other-logging-bucket')
64+
}, callback);
65+
}
66+
// [END updateSink]
67+
68+
// [START deleteSink]
69+
function deleteSink(callback) {
70+
var sink = logging.sink('mySink');
71+
72+
// delete a sink
73+
//
74+
// This method only works if you are authenticated as yourself, e.g. using the
75+
// gcloud SDK.
76+
sink.delete(callback);
77+
}
78+
// [END deleteSink]
79+
80+
exports.listSinks = listSinks;
81+
exports.createSink = createSink;
82+
exports.updateSink = updateSink;
83+
exports.deleteSink = deleteSink;
84+
85+
if (module === require.main) {
86+
listSinks(function (err, sinks, apiResponse) {
87+
console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse);
88+
if (err) {
89+
return;
90+
}
91+
});
92+
}

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, apiResponse) {
46+
console.log(err, 'apiResponse:', apiResponse);
47+
});
48+
}

logging/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
"engines": {
8+
"node": ">=0.10.x"
9+
},
10+
"scripts": {
11+
"list": "node list.js",
12+
"write": "node write.js",
13+
"export": "node export.js"
14+
},
15+
"dependencies": {
16+
"gcloud": "^0.27.0"
17+
}
18+
}

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:', apiResponse);
79+
if (err) {
80+
return;
81+
}
82+
deleteLog(function (err, apiResponse) {
83+
console.log(err, 'apiResponse:', apiResponse);
84+
});
85+
});
86+
}

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
"deps_datastore": "cd datastore && npm i && cd ../..",
2828
"deps_storage": "cd storage && npm i && cd ../..",
2929
"deps_prediction": "cd prediction && npm i && cd ../..",
30+
"deps_logging": "cd logging && npm i && cd ../..",
3031
"deps_express": "cd appengine/express && npm i && cd ../..",
3132
"deps_sendgrid": "cd appengine/sendgrid && npm i && cd ../.. && cd computeengine/sendgrid && npm i && cd ../..",
3233
"deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..",
3334
"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 ../..;",
34-
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_prediction && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
35+
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_prediction && npm run deps_logging && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
3536
"test": "npm run jshint && npm run cover"
3637
},
3738
"devDependencies": {
38-
"async": "^1.5.0",
39-
"coveralls": "^2.11.4",
40-
"googleapis": "^2.1.6",
41-
"istanbul": "^0.4.0",
39+
"async": "^1.5.2",
40+
"coveralls": "^2.11.6",
41+
"googleapis": "^2.1.7",
42+
"istanbul": "^0.4.2",
4243
"jshint": "~2.8.0",
4344
"mocha": "^2.2.5",
4445
"proxyquire": "^1.7.3",

prediction/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Prediction API Samples
2+
3+
These samples require an environment variable to be set:
4+
5+
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
6+
download one from your Google project's "permissions" page.
7+
8+
## Run a sample
9+
10+
Install dependencies:
11+
12+
npm install
13+
14+
To print available commands:
15+
16+
npm run
17+
18+
Execute a sample:
19+
20+
npm run <sample>
21+
22+
Example:
23+
24+
npm run hostedmodels

0 commit comments

Comments
 (0)