Add typings for graylog2 (#38225)

This commit is contained in:
Andrey 2019-09-24 20:20:07 +03:00 committed by Andrew Branch
parent b8d2701d9b
commit 76f9a5bdb5
4 changed files with 208 additions and 0 deletions

View File

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

86
types/graylog2/index.d.ts vendored Normal file
View File

@ -0,0 +1,86 @@
// Type definitions for graylog2 0.2
// Project: http://github.com/Wizcorp/node-graylog2
// Definitions by: Andrey Kun <https://github.com/scalder27>
// 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<Readonly<{ host: string; port: number }>>;
}
export class graylog extends EventEmitter {
constructor(config: Readonly<GraylogConfig>);
log(message: string | Error | Record<string, any>): void;
log(message: string, fullMessage: string, additionalFields?: Record<string, any>, timestamp?: number): void;
log(message: string | Error, additionalFields?: Record<string, any>, _?: undefined, timestamp?: number): void;
log(
message: string | Error | Record<string, any>,
_: undefined,
additionalFields: Record<string, any> | 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;
}

View File

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

View File

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