|
| 1 | +/* |
| 2 | + * Copyright 2015 Splunk, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 5 | + * not use this file except in compliance with the License. You may obtain |
| 6 | + * a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * This example shows how to batch events with the |
| 19 | + * SplunkLogger by manually calling flush. |
| 20 | + * |
| 21 | + * By default autoFlush is enabled, this means |
| 22 | + * an HTTP request is made each time send() |
| 23 | + * is called. |
| 24 | + * |
| 25 | + * By disabling autoFlush, events will be queued |
| 26 | + * until flush() is called. |
| 27 | + */ |
| 28 | + |
| 29 | +// Change to require("splunk-logging").Logger; |
| 30 | +var SplunkLogger = require("../index").Logger; |
| 31 | + |
| 32 | +/** |
| 33 | + * Only the token property is required. |
| 34 | + * |
| 35 | + * Here, autoFlush is set to false |
| 36 | + */ |
| 37 | +var config = { |
| 38 | + token: "your-token-here", // TODO: remove the token |
| 39 | + host: "localhost", |
| 40 | + path: "/services/collector/event/1.0", |
| 41 | + protocol: "https", |
| 42 | + port: 8088, |
| 43 | + level: "info", |
| 44 | + autoFlush: false |
| 45 | +}; |
| 46 | + |
| 47 | +// Create a new logger |
| 48 | +var Logger = new SplunkLogger(config); |
| 49 | + |
| 50 | +Logger.error = function(err, context) { |
| 51 | + // Handle errors here |
| 52 | + console.log("error", err, "context", context); |
| 53 | +}; |
| 54 | + |
| 55 | +// Define the payload to send to Splunk's Event Collector |
| 56 | +var payload = { |
| 57 | + // Message can be anything, doesn't have to be an object |
| 58 | + message: { |
| 59 | + temperature: "70F", |
| 60 | + chickenCount: 500 |
| 61 | + }, |
| 62 | + // Metadata is optional |
| 63 | + metadata: { |
| 64 | + source: "chicken coop", |
| 65 | + sourcetype: "httpevent", |
| 66 | + index: "main", |
| 67 | + host: "farm.local", |
| 68 | + }, |
| 69 | + // Severity is also optional |
| 70 | + severity: "info" |
| 71 | +}; |
| 72 | + |
| 73 | +console.log("Queuing payload", payload); |
| 74 | +// Don't need a callback here |
| 75 | +Logger.send(payload); |
| 76 | + |
| 77 | +var payload2 = { |
| 78 | + message: { |
| 79 | + temperature: "75F", |
| 80 | + chickenCount: 600, |
| 81 | + note: "New chickens have arrived!" |
| 82 | + }, |
| 83 | + metadata: payload.metadata |
| 84 | +}; |
| 85 | + |
| 86 | +console.log("Queuing second payload", payload2); |
| 87 | +// Don't need a callback here |
| 88 | +Logger.send(payload2); |
| 89 | + |
| 90 | +/** |
| 91 | + * Since autoFlush is disabled, call flush manually. |
| 92 | + * This will send both payloads in a single |
| 93 | + * HTTP request. |
| 94 | + * |
| 95 | + * The same callback can work for send() and flush(). |
| 96 | + */ |
| 97 | +Logger.flush(function(err, resp, body) { |
| 98 | + // If successful, body will be { text: 'Success', code: 0 } |
| 99 | + console.log("Response from Splunk", body); |
| 100 | +}); |
0 commit comments