diff --git a/types/graylog2/graylog2-tests.ts b/types/graylog2/graylog2-tests.ts new file mode 100644 index 0000000000..9db0a0c6ad --- /dev/null +++ b/types/graylog2/graylog2-tests.ts @@ -0,0 +1,96 @@ +import graylog2, { graylog, GraylogConfig, GraylogLogMethod } from 'graylog2'; + +const fullGraylogConfig: GraylogConfig = { + bufferSize: 1000, + deflate: 'always', + hostname: 'LocalMachine', + facility: 'Developers, Inc.', + servers: [{ host: 'graylog-host', port: 12201 }], +}; + +const shortGraylogConfig: GraylogConfig = { + servers: [{ host: 'graylog-host', port: 12201 }], +}; + +// $ExpectType graylog +new graylog2.graylog(fullGraylogConfig); + +// $ExpectType graylog +const logger = new graylog(shortGraylogConfig); + +// graylog.log method +logger.log("What we've got here is...failure to communicate"); +logger.log( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", +); +logger.log('short message', { cool: 'beans' }); +logger.log( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", + { cool: 'beans' }, +); +logger.log( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", + { cool: 'beans' }, + Date.now(), +); +logger.log('short message', undefined, { + type: 'meta with properties', +}); +logger.log('short message', undefined, { cool: 'beans' }, Date.now()); +logger.log('short message', undefined, undefined, Date.now()); +logger.log(new Error('NotFoundError')); +logger.log(new Error('NotFoundError'), undefined, { cool: 'beans' }, Date.now()); +logger.log(new Error('NotFoundError'), undefined, undefined, Date.now()); + +const graylogLogMethods: GraylogLogMethod[] = [ + 'emergency', + 'alert', + 'critical', + 'error', + 'warning', + 'warn', + 'notice', + 'info', + 'debug', +]; + +for (const method of graylogLogMethods) { + logger[method]("What we've got here is...failure to communicate"); + logger[method]( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", + ); + logger[method]('short message', { cool: 'beans' }); + logger[method]( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", + { + type: 'meta with properties', + }, + ); + logger[method]( + "What we've got here is...failure to communicate", + "Some men you just can't reach. So you get what we had here last week, which is the way he wants it... well, he gets it. I don't like it any more than you men.", + { cool: 'beans' }, + Date.now(), + ); + logger[method]('short message', undefined, { cool: 'beans' }); + logger[method]('short message', undefined, { cool: 'beans' }, Date.now()); + logger[method]('short message', undefined, undefined, Date.now()); + logger[method](new Error('NotFoundError')); + logger[method](new Error('NotFoundError'), undefined, { cool: 'beans' }, Date.now()); + logger[method](new Error('NotFoundError'), undefined, undefined, Date.now()); +} + +logger.on('error', error => { + throw new Error(`Error while trying to write to graylog2: ${error.message}`); +}); + +logger.close(error => { + if (error != null) throw error; +}); + +logger.close(); diff --git a/types/graylog2/index.d.ts b/types/graylog2/index.d.ts new file mode 100644 index 0000000000..e67e2bd589 --- /dev/null +++ b/types/graylog2/index.d.ts @@ -0,0 +1,86 @@ +// Type definitions for graylog2 0.2 +// Project: http://github.com/Wizcorp/node-graylog2 +// Definitions by: Andrey Kun +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import { EventEmitter } from 'events'; + +export type GraylogDeflate = 'optimal' | 'always' | 'never'; + +export type GraylogLogMethod = + | 'log' + | 'emergency' + | 'alert' + | 'critical' + | 'error' + | 'warning' + | 'warn' + | 'notice' + | 'info' + | 'debug'; + +export interface GraylogConfig { + /** + * The name of a host. + * The default value is "os.hostname()" + */ + hostname?: string; + + /** + * The facility - log's field type in Graylog. + * The default value is "Node.js" + */ + facility?: string; + + /** + * The strategy for a message compression: + * "always" - every message will be compressed with zlib.deflate + * "never" - no compression + * "optimal" - messages bigger than GraylogConfig.bufferSize will be compressed + * + * The default value is "optimal" + */ + deflate?: GraylogDeflate; + + /** + * The max UDP packet size. Should never exceed the MTU of your system. + * The default value is 1400 + */ + bufferSize?: number; + + /** + * The list of graylog servers + */ + servers: ReadonlyArray>; +} + +export class graylog extends EventEmitter { + constructor(config: Readonly); + + log(message: string | Error | Record): void; + log(message: string, fullMessage: string, additionalFields?: Record, timestamp?: number): void; + log(message: string | Error, additionalFields?: Record, _?: undefined, timestamp?: number): void; + log( + message: string | Error | Record, + _: undefined, + additionalFields: Record | undefined, + timestamp?: number, + ): void; + + emergency: graylog['log']; + alert: graylog['log']; + critical: graylog['log']; + error: graylog['log']; + warning: graylog['log']; + warn: graylog['log']; + notice: graylog['log']; + info: graylog['log']; + debug: graylog['log']; + + close(callback?: (err: Error | undefined) => void): void; +} + +export namespace graylog { + const graylog: graylog; +} diff --git a/types/graylog2/tsconfig.json b/types/graylog2/tsconfig.json new file mode 100644 index 0000000000..c95ec8131b --- /dev/null +++ b/types/graylog2/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true + }, + "files": [ + "index.d.ts", + "graylog2-tests.ts" + ] +} diff --git a/types/graylog2/tslint.json b/types/graylog2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/graylog2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }