|
| 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 | +} |
0 commit comments