[splunk-bunyan-logger] Adding new type definition (#27041)

This commit is contained in:
Alex Brick 2018-07-05 19:05:49 +02:00 committed by Mohamed Hegazy
parent 6c7e7b5a33
commit 6361591c22
5 changed files with 116 additions and 1 deletions

28
types/splunk-bunyan-logger/index.d.ts vendored Normal file
View File

@ -0,0 +1,28 @@
// Type definitions for splunk-bunyan-logger 0.9
// Project: http://dev.splunk.com
// Definitions by: Alex Brick <https://github.com/bricka>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node" />
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;

View File

@ -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);

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -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[];