diff --git a/types/splunk-bunyan-logger/index.d.ts b/types/splunk-bunyan-logger/index.d.ts new file mode 100644 index 0000000000..5a1c8bf7d5 --- /dev/null +++ b/types/splunk-bunyan-logger/index.d.ts @@ -0,0 +1,28 @@ +// Type definitions for splunk-bunyan-logger 0.9 +// Project: http://dev.splunk.com +// Definitions by: Alex Brick +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import { Stream as BunyanStream } from 'bunyan'; +import { + Config, + EventFormatter, + Logger as SplunkLogger, + SendContext +} from 'splunk-logging'; + +export interface SplunkStream extends NodeJS.WritableStream { + logger: SplunkLogger; +} + +export interface SplunkBunyanStream extends BunyanStream { + flush(callback?: (error: Error, req: any, res: any) => void): void; + on(event: string, callback: (err: Error, context: SendContext) => void): void; + setEventFormatter(eventFormatter: EventFormatter): void; + stream: SplunkStream; +} + +export function createStream(config: Config): SplunkBunyanStream; diff --git a/types/splunk-bunyan-logger/splunk-bunyan-logger-tests.ts b/types/splunk-bunyan-logger/splunk-bunyan-logger-tests.ts new file mode 100644 index 0000000000..78c9ed44eb --- /dev/null +++ b/types/splunk-bunyan-logger/splunk-bunyan-logger-tests.ts @@ -0,0 +1,62 @@ +import { createStream } from 'splunk-bunyan-logger'; +import { Config } from 'splunk-logging'; +import { createLogger } from 'bunyan'; + +const config = { + token: "your-token-here", + url: "https://splunk.local:8088" +}; + +const splunkStream = createStream(config); +// Enable SSL certificate validation +splunkStream.stream.logger.requestOptions.strictSSL = true; + +splunkStream.on("error", (err, context) => { + // Handle errors here + console.log("Error", err, "Context", context); +}); + +splunkStream.flush((err, resp, body) => { + // If successful, body will be { text: 'Success', code: 0 } + console.log("Response from Splunk", body); +}); + +splunkStream.setEventFormatter((message, severity) => { + let event = `[${severity}]`; + + if (typeof message === "object") { + for (const key in message) { + event += `${key}=${message[key]} `; + } + } else { + event += "message=" + message; + } + + return event; +}); + +// Note: splunkStream must be set to an element in the streams array +const Logger = createLogger({ + name: "my logger", + streams: [ + splunkStream + ] +}); + +// Fully-specified config + +const fullConfig: Config = { + token: 'token', + name: 'loggerName', + host: 'splunkHost', + maxRetries: 2, + path: 'splunkPath', + protocol: 'https', + port: 234, + level: 'info', + batchInterval: 200, + maxBatchSize: 300, + maxBatchCount: 400, +}; + +createStream(fullConfig); diff --git a/types/splunk-bunyan-logger/tsconfig.json b/types/splunk-bunyan-logger/tsconfig.json new file mode 100644 index 0000000000..6197901424 --- /dev/null +++ b/types/splunk-bunyan-logger/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "splunk-bunyan-logger-tests.ts" + ] +} diff --git a/types/splunk-bunyan-logger/tslint.json b/types/splunk-bunyan-logger/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/splunk-bunyan-logger/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/splunk-logging/index.d.ts b/types/splunk-logging/index.d.ts index 296fab277f..ecbc38f32c 100644 --- a/types/splunk-logging/index.d.ts +++ b/types/splunk-logging/index.d.ts @@ -35,10 +35,11 @@ export interface SendContext { } export type Callback = (error: Error | undefined, req: any, res: any) => void; +export type EventFormatter = (message: any, severity: string) => any; export class Logger { error: (error: Error, context: SendContext) => void; - eventFormatter: (message: any, severity: string) => any; + eventFormatter: EventFormatter; requestOptions: RequestOptions; readonly serializedEventQueue: any[];