Winston package: Typescript linting; minor cleanups/refactoring; add some missing exports (#15609)

* Typescript linting; minor cleanups/refactoring; add some missing exports

* Modify tslint.json to extend dtslint/dt.json rather than ../tslint.json
This commit is contained in:
DABH
2017-04-19 14:15:10 -07:00
committed by Andy
parent 68d74285e6
commit d74f4d8ace
3 changed files with 129 additions and 129 deletions

View File

@@ -1,6 +1,6 @@
// Type definitions for winston 2.3
// Project: https://github.com/flatiron/winston
// Definitions by: bonnici <https://github.com/bonnici>, Peter Harris <https://github.com/codeanimal>
// Definitions by: bonnici <https://github.com/bonnici>, Peter Harris <https://github.com/codeanimal>, DABH <https://github.com/DABH>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Imported from: https://github.com/soywiz/typescript-node-definitions/winston.d.ts
@@ -9,24 +9,21 @@
import {Agent} from 'http';
declare var winston: winston.Winston;
export = winston;
declare namespace winston {
export interface AbstractConfigLevels {
interface AbstractConfigSetLevels {
[key: string]: number;
}
export interface AbstractConfigColors {
interface AbstractConfigSetColors {
[key: string]: string;
}
export interface AbstractConfig {
levels: AbstractConfigLevels;
colors: AbstractConfigColors;
interface AbstractConfigSet {
levels: AbstractConfigSetLevels;
colors: AbstractConfigSetColors;
}
export interface CliConfigLevels extends AbstractConfigLevels {
interface CliConfigSetLevels extends AbstractConfigSetLevels {
error: number;
warn: number;
help: number;
@@ -39,7 +36,7 @@ declare namespace winston {
silly: number;
}
export interface CliConfigColors extends AbstractConfigColors {
interface CliConfigSetColors extends AbstractConfigSetColors {
error: string;
warn: string;
help: string;
@@ -51,7 +48,7 @@ declare namespace winston {
input: string;
silly: string;
}
export interface NpmConfigLevels extends AbstractConfigLevels {
interface NpmConfigSetLevels extends AbstractConfigSetLevels {
error: number;
warn: number;
info: number;
@@ -59,7 +56,7 @@ declare namespace winston {
debug: number;
silly: number;
}
export interface NpmConfigColors extends AbstractConfigColors {
interface NpmConfigSetColors extends AbstractConfigSetColors {
error: string;
warn: string;
info: string;
@@ -67,7 +64,7 @@ declare namespace winston {
debug: string;
silly: string;
}
export interface SyslogConfigLevels extends AbstractConfigLevels {
interface SyslogConfigSetLevels extends AbstractConfigSetLevels {
emerg: number;
alert: number;
crit: number;
@@ -77,7 +74,7 @@ declare namespace winston {
info: number;
debug: number;
}
export interface SyslogConfigColors extends AbstractConfigColors {
interface SyslogConfigSetColors extends AbstractConfigSetColors {
emerg: string;
alert: string;
crit: string;
@@ -88,13 +85,8 @@ declare namespace winston {
debug: string;
}
export interface Winston {
config: {
cli: {levels: CliConfigLevels, colors: CliConfigColors},
npm: {levels: NpmConfigLevels, colors: NpmConfigColors},
syslog: {levels: SyslogConfigLevels, colors: SyslogConfigColors}
};
interface Winston {
config: winston.Config;
transports: winston.Transports;
Transport: winston.TransportStatic;
Logger: winston.LoggerStatic;
@@ -126,18 +118,28 @@ declare namespace winston {
remove(transport: string | winston.TransportInstance): winston.LoggerInstance;
startTimer(): winston.ProfileHandler;
profile(id: string, msg?: string, meta?: any, callback?: (err: Error, level: string, msg: string, meta: any) => void): winston.LoggerInstance;
addColors(target: AbstractConfigColors): any;
setLevels(target: AbstractConfigLevels): any;
addColors(target: AbstractConfigSetColors): any;
setLevels(target: AbstractConfigSetLevels): any;
cli(): winston.LoggerInstance;
close(): void;
configure(options: winston.LoggerOptions): void;
}
export type CLILoggingLevel = 'error' | 'warn' | 'help' | 'data' | 'info' | 'debug' | 'prompt' | 'verbose' | 'input' | 'silly';
export type NPMLoggingLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly';
export type SyslogLoggingLevel = 'emerg' | 'alert' | 'crit' | 'error' | 'warning' | 'notice' | 'info' | 'debug';
type CLILoggingLevel = 'error' | 'warn' | 'help' | 'data' | 'info' | 'debug' | 'prompt' | 'verbose' | 'input' | 'silly';
type NPMLoggingLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly';
type SyslogLoggingLevel = 'emerg' | 'alert' | 'crit' | 'error' | 'warning' | 'notice' | 'info' | 'debug';
export interface ExceptionProcessInfo {
interface Config {
allColors: AbstractConfigSetColors;
cli: {levels: CliConfigSetLevels, colors: CliConfigSetColors};
npm: {levels: NpmConfigSetLevels, colors: NpmConfigSetColors};
syslog: {levels: SyslogConfigSetLevels, colors: SyslogConfigSetColors};
addColors(colors: AbstractConfigSetColors): void;
colorize(level: number, message?: string): string;
}
interface ExceptionProcessInfo {
pid: number;
uid?: number;
gid?: number;
@@ -148,12 +150,12 @@ declare namespace winston {
memoryUsage: NodeJS.MemoryUsage;
}
export interface ExceptionOsInfo {
interface ExceptionOsInfo {
loadavg: [number, number, number];
uptime: number;
}
export interface ExceptionTrace {
interface ExceptionTrace {
column: number;
file: string;
"function": string;
@@ -162,36 +164,32 @@ declare namespace winston {
native: boolean;
}
export interface ExceptionAllInfo {
interface ExceptionAllInfo {
date: Date;
process: ExceptionProcessInfo;
os: ExceptionOsInfo;
trace: Array<ExceptionTrace>;
stack: Array<string>;
trace: ExceptionTrace[];
stack: string[];
}
export interface Exception {
interface Exception {
getAllInfo(err: Error): ExceptionAllInfo;
getProcessInfo(): ExceptionProcessInfo;
getOsInfo(): ExceptionOsInfo;
getTrace(err: Error): Array<ExceptionTrace>;
getTrace(err: Error): ExceptionTrace[];
}
export interface MetadataRewriter {
(level: string, msg: string, meta: any): any;
}
type MetadataRewriter = (level: string, msg: string, meta: any) => any;
export interface MetadataFilter {
(level: string, msg: string, meta: any): string | { msg: any; meta: any; };
}
type MetadataFilter = (level: string, msg: string, meta: any) => string | { msg: any; meta: any; };
export interface LoggerStatic {
interface LoggerStatic {
new (options?: LoggerOptions): LoggerInstance;
}
export interface LoggerInstance extends NodeJS.EventEmitter {
rewriters: Array<MetadataRewriter>;
filters: Array<MetadataFilter>;
interface LoggerInstance extends NodeJS.EventEmitter {
rewriters: MetadataRewriter[];
filters: MetadataFilter[];
transports: {[key: string]: TransportInstance};
extend(target: any): LoggerInstance;
@@ -229,20 +227,20 @@ declare namespace winston {
startTimer(): ProfileHandler;
profile(id: string, msg?: string, meta?: any, callback?: (err: Error, level: string, msg: string, meta: any) => void): LoggerInstance;
configure(options: LoggerOptions): void;
setLevels(target: AbstractConfigLevels): any;
setLevels(target: AbstractConfigSetLevels): any;
cli(): LoggerInstance;
level: string;
}
export interface LoggerOptions {
interface LoggerOptions {
transports?: TransportInstance[];
rewriters?: MetadataRewriter[];
filters?: MetadataFilter[];
exceptionHandlers?: TransportInstance[];
handleExceptions?: boolean;
level?: string;
levels?: AbstractConfigLevels;
levels?: AbstractConfigSetLevels;
/**
* @type {(boolean|(err: Error) => void)}
@@ -255,27 +253,27 @@ declare namespace winston {
[optionName: string]: any;
}
export interface TransportStatic {
new (options?: TransportOptions): TransportInstance;
interface TransportStatic {
new(options?: TransportOptions): TransportInstance;
}
export interface TransportInstance extends TransportStatic, NodeJS.EventEmitter {
interface TransportInstance extends TransportStatic, NodeJS.EventEmitter {
silent: boolean;
raw: boolean;
name: string;
formatter?: Function;
level?: string;
handleExceptions: boolean;
exceptionsLevel: string;
humanReadableUnhandledException: boolean;
formatQuery(query: (string | Object)): (string | Object);
formatter?(options?: any): string;
normalizeQuery(options: QueryOptions): QueryOptions;
formatResults(results: (Object | Array<any>), options?: Object): (Object | Array<any>);
formatResults(results: (Object | any[]), options?: Object): (Object | any[]);
logException(msg: string, meta: Object, callback: () => void): void;
}
export interface ConsoleTransportInstance extends TransportInstance {
interface ConsoleTransportInstance extends TransportInstance {
json: boolean;
colorize: boolean;
prettyPrint: boolean;
@@ -285,18 +283,18 @@ declare namespace winston {
logstash: boolean;
depth: string|null;
align: boolean;
stderrLevels: { [key: string]: LeveledLogMethod; }
stderrLevels: { [key: string]: LeveledLogMethod; };
eol: string;
stringify?: (obj: Object) => string;
new (options?: ConsoleTransportOptions): ConsoleTransportInstance;
new(options?: ConsoleTransportOptions): ConsoleTransportInstance;
stringify?(obj: Object): string;
}
export interface DailyRotateFileTransportInstance extends TransportInstance {
new (options?: DailyRotateFileTransportOptions): DailyRotateFileTransportInstance;
interface DailyRotateFileTransportInstance extends TransportInstance {
new(options?: DailyRotateFileTransportOptions): DailyRotateFileTransportInstance;
}
export interface FileTransportInstance extends TransportInstance {
interface FileTransportInstance extends TransportInstance {
json: boolean;
logstash: boolean;
colorize: boolean;
@@ -312,13 +310,13 @@ declare namespace winston {
depth: string|null;
showLevel: boolean;
maxRetries: number;
stringify?: (obj: Object) => string;
new (options?: FileTransportOptions): FileTransportInstance;
close(): void;
new(options?: FileTransportOptions): FileTransportInstance;
stringify?(obj: Object): string;
}
export interface HttpTransportInstance extends TransportInstance {
interface HttpTransportInstance extends TransportInstance {
name: string;
ssl: boolean;
host: string;
@@ -327,10 +325,10 @@ declare namespace winston {
path: string;
agent?: Agent|null;
new (options?: HttpTransportOptions): HttpTransportInstance;
new(options?: HttpTransportOptions): HttpTransportInstance;
}
export interface MemoryTransportInstance extends TransportInstance {
interface MemoryTransportInstance extends TransportInstance {
errorOutput: GenericTextTransportOptions[];
writeOutput: GenericTextTransportOptions[];
@@ -341,24 +339,24 @@ declare namespace winston {
showLevel: boolean;
label: string|null;
depth: string|null;
stringify?: (obj: Object) => string;
new (options?: MemoryTransportOptions): MemoryTransportInstance;
new(options?: MemoryTransportOptions): MemoryTransportInstance;
stringify?(obj: Object): string;
}
export interface WebhookTransportInstance extends TransportInstance {
new (options?: WebhookTransportOptions): WebhookTransportInstance;
interface WebhookTransportInstance extends TransportInstance {
new(options?: WebhookTransportOptions): WebhookTransportInstance;
}
export interface WinstonModuleTrasportInstance extends TransportInstance {
new (options?: WinstonModuleTransportOptions): WinstonModuleTrasportInstance;
interface WinstonModuleTrasportInstance extends TransportInstance {
new(options?: WinstonModuleTransportOptions): WinstonModuleTrasportInstance;
}
export interface ContainerStatic {
new (options: LoggerOptions): ContainerInstance;
interface ContainerStatic {
new(options: LoggerOptions): ContainerInstance;
}
export interface ContainerInstance extends ContainerStatic {
interface ContainerInstance extends ContainerStatic {
get(id: string, options?: LoggerOptions): LoggerInstance;
add(id: string, options: LoggerOptions): LoggerInstance;
has(id: string): boolean;
@@ -368,7 +366,7 @@ declare namespace winston {
default: LoggerOptions;
}
export interface Transports {
interface Transports {
File: FileTransportInstance;
Console: ConsoleTransportInstance;
Loggly: WinstonModuleTrasportInstance;
@@ -378,32 +376,35 @@ declare namespace winston {
Webhook: WebhookTransportInstance;
}
export type TransportOptions = ConsoleTransportOptions | DailyRotateFileTransportOptions | FileTransportOptions | HttpTransportOptions | MemoryTransportOptions | WebhookTransportOptions | WinstonModuleTransportOptions;
type TransportOptions = ConsoleTransportOptions | DailyRotateFileTransportOptions | FileTransportOptions
| HttpTransportOptions | MemoryTransportOptions | WebhookTransportOptions | WinstonModuleTransportOptions;
export interface GenericTransportOptions {
interface GenericTransportOptions {
level?: string;
silent?: boolean;
raw?: boolean;
name?: string;
formatter?: Function;
handleExceptions?: boolean;
exceptionsLevel?: string;
humanReadableUnhandledException?: boolean;
formatter?(options?: any): string;
}
export interface GenericTextTransportOptions {
interface GenericTextTransportOptions {
json?: boolean;
colorize?: boolean;
colors?: any;
prettyPrint?: boolean;
timestamp?: (Function | boolean);
showLevel?: boolean;
label?: string;
depth?: number;
stringify?: Function;
timestamp?(): any | boolean;
stringify?(obj: any): string;
}
export interface GenericNetworkTransportOptions {
interface GenericNetworkTransportOptions {
host?: string;
port?: number;
auth?: {
@@ -413,12 +414,12 @@ declare namespace winston {
path?: string;
}
export interface ConsoleTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
interface ConsoleTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
logstash?: boolean;
debugStdout?: boolean;
}
export interface DailyRotateFileTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
interface DailyRotateFileTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
logstash?: boolean;
maxsize?: number;
maxFiles?: number;
@@ -434,7 +435,7 @@ declare namespace winston {
stream?: NodeJS.WritableStream;
}
export interface FileTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
interface FileTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
logstash?: boolean;
maxsize?: number;
rotationFormat?: boolean;
@@ -452,14 +453,14 @@ declare namespace winston {
stream?: NodeJS.WritableStream;
}
export interface HttpTransportOptions extends GenericTransportOptions, GenericNetworkTransportOptions {
interface HttpTransportOptions extends GenericTransportOptions, GenericNetworkTransportOptions {
ssl?: boolean;
}
export interface MemoryTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
interface MemoryTransportOptions extends GenericTransportOptions, GenericTextTransportOptions {
}
export interface WebhookTransportOptions extends GenericTransportOptions, GenericNetworkTransportOptions {
interface WebhookTransportOptions extends GenericTransportOptions, GenericNetworkTransportOptions {
method?: string;
ssl?: {
key?: any;
@@ -468,11 +469,11 @@ declare namespace winston {
};
}
export interface WinstonModuleTransportOptions extends GenericTransportOptions {
interface WinstonModuleTransportOptions extends GenericTransportOptions {
[optionName: string]: any;
}
export interface QueryOptions {
interface QueryOptions {
rows?: number;
limit?: number;
start?: number;
@@ -482,10 +483,11 @@ declare namespace winston {
fields: any;
}
export interface ProfileHandler {
interface ProfileHandler {
logger: LoggerInstance;
start: Date;
done: (msg: string) => LoggerInstance;
done(msg: string): LoggerInstance;
}
interface LogMethod {
@@ -500,7 +502,8 @@ declare namespace winston {
(msg: string, ...meta: any[]): LoggerInstance;
}
interface LogCallback {
(error?: any, level?: string, msg?: string, meta?: any): void;
}
type LogCallback = (error?: any, level?: string, msg?: string, meta?: any) => void;
}
declare const winston: winston.Winston;
export = winston;

View File

@@ -0,0 +1,7 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": false,
"no-misused-new": false
}
}

View File

@@ -1,17 +1,16 @@
import winston = require('winston');
var str: string;
var bool: boolean;
var num: number;
var metadata: any;
var obj: any = {};
let str: string;
let bool: boolean;
let num: number;
let metadata: any;
let obj: any = {};
winston.level = 'debug';
var queryOptions: winston.QueryOptions;
var transportOptions: winston.TransportOptions;
var loggerOptions: winston.LoggerOptions = {
let queryOptions: winston.QueryOptions;
let transportOptions: winston.TransportOptions;
let loggerOptions: winston.LoggerOptions = {
transports: [new (winston.Transport)()],
rewriters: [
(level: string, msg: string, meta: any): any => {
@@ -22,23 +21,21 @@ var loggerOptions: winston.LoggerOptions = {
handleExceptions: false
};
var options: any;
var value: any;
var transport: winston.TransportInstance;
var logger: winston.LoggerInstance;
var profiler: winston.ProfileHandler;
let options: any;
let value: any;
let transport: winston.TransportInstance;
let logger: winston.LoggerInstance;
let profiler: winston.ProfileHandler;
var writeableStream: NodeJS.WritableStream;
var readableStream: NodeJS.ReadableStream;
let writeableStream: NodeJS.WritableStream;
let readableStream: NodeJS.ReadableStream;
let transportStatic: winston.TransportStatic = winston.Transport;
var transportStatic: winston.TransportStatic = winston.Transport;
var transportInstance: winston.TransportInstance = new (winston.Transport)(transportOptions);
let transportInstance: winston.TransportInstance = new (winston.Transport)(transportOptions);
transportInstance = new (winston.Transport)();
var containerInstance: winston.ContainerInstance = new (winston.Container)(loggerOptions);
let containerInstance: winston.ContainerInstance = new (winston.Container)(loggerOptions);
winston.loggers.options.transports = [
new (winston.Transport)()
];
@@ -66,7 +63,7 @@ transport = winston.transports.DailyRotateFile;
transport = winston.transports.File;
transport = winston.transports.Http;
transport = winston.transports.Loggly;
transport = winston.transports.Memory
transport = winston.transports.Memory;
transport = winston.transports.Webhook;
value = transport.formatQuery({});
@@ -76,7 +73,6 @@ transport.logException(str, metadata, () => { });
winston.exitOnError = bool;
winston.log(str, str);
winston.log(str, str, metadata);
winston.log(str, str, metadata, metadata, metadata);
@@ -100,10 +96,8 @@ winston.error(str, metadata);
winston.error(str, metadata, metadata, metadata);
winston.query(queryOptions, (err: Error, results: any): void => {
});
winston.query((err: Error, results: any): void => {
});
logger = winston.add(transport, transportOptions);
@@ -112,7 +106,6 @@ logger = winston.add(transport, {filename: 'path/to/file.log'});
winston.clear();
logger = winston.profile(str, str, metadata, (err: Error, level: string, msg: string, meta: any): void => {
});
logger = winston.profile(str);
profiler = winston.startTimer();
@@ -128,8 +121,6 @@ readableStream.on('log', (log: any): void => {
console.log(log);
});
logger = logger.extend(obj);
logger.log(str, str);
logger.log(str, str, metadata);
@@ -148,10 +139,8 @@ logger.error(str, metadata);
logger.error(str, metadata, metadata, metadata);
logger.query(queryOptions, (err: Error, results: any): void => {
});
logger.query((err: Error, results: any): void => {
});
readableStream = winston.stream(options);
@@ -166,17 +155,15 @@ logger.clear();
logger = logger.remove(transport);
profiler = logger.startTimer();
logger = logger.profile(str, str, metadata, (err: Error, level: string, msg: string, meta: any): void => {
});
value = logger.setLevels(value);
logger = logger.cli();
logger = profiler.done(str);
logger = profiler.logger;
profiler.start = new Date();
let testRewriter : winston.MetadataRewriter;
let testRewriter: winston.MetadataRewriter;
testRewriter = (level: string, msg: string, meta: any): any => {
return meta;
};
@@ -186,7 +173,7 @@ logger.rewriters.push(testRewriter);
* New Logger instances with transports tests:
*/
var logger: winston.LoggerInstance = new (winston.Logger)({
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
level: str,
@@ -282,6 +269,9 @@ winston.default.setLevels(winston.config.syslog.levels);
winston.addColors(winston.config.syslog.colors);
winston.default.emerg('syslog!');
winston.config.addColors(winston.config.syslog.colors);
winston.config.colorize(winston.config.syslog.levels['info'], "Hello world!");
// module augment for custom level log
// https://github.com/winstonjs/winston#using-custom-logging-levels
// https://github.com/Microsoft/TypeScript/issues/7545