From 27a9d53693fea45cc2a5ed7b388d1cc4af1a80fc Mon Sep 17 00:00:00 2001 From: heycalmdown Date: Fri, 11 Nov 2016 17:53:12 +0900 Subject: [PATCH] Add configs(cli, npm, syslog) - refactor to abstract configs --- winston/index.d.ts | 135 +++++++++++++++++++++++++++++++++------ winston/winston-tests.ts | 20 ++++++ 2 files changed, 136 insertions(+), 19 deletions(-) diff --git a/winston/index.d.ts b/winston/index.d.ts index 0b10766d10..54d5a0e713 100644 --- a/winston/index.d.ts +++ b/winston/index.d.ts @@ -17,7 +17,88 @@ declare var winston: winston.Winston; export = winston; declare namespace winston { + export interface AbstractConfigLevels { + [key: string]: number; + } + + export interface AbstractConfigColors { + [key: string]: string; + } + + export interface AbstractConfig { + levels: AbstractConfigLevels; + colors: AbstractConfigColors; + } + + export interface CliConfigLevels extends AbstractConfigLevels { + error: number; + warn: number; + help: number; + data: number; + info: number; + debug: number; + prompt: number; + verbose: number; + input: number; + silly: number; + } + + export interface CliConfigColors extends AbstractConfigColors { + error: string; + warn: string; + help: string; + data: string; + info: string; + debug: string; + prompt: string; + verbose: string; + input: string; + silly: string; + } + export interface NpmConfigLevels extends AbstractConfigLevels { + error: number; + warn: number; + info: number; + verbose: number; + debug: number; + silly: number; + } + export interface NpmConfigColors extends AbstractConfigColors { + error: string; + warn: string; + info: string; + verbose: string; + debug: string; + silly: string; + } + export interface SyslogConfigLevels extends AbstractConfigLevels { + emerg: number; + alert: number; + crit: number; + error: number; + warning: number; + notice: number; + info: number; + debug: number; + } + export interface SyslogConfigColors extends AbstractConfigColors { + emerg: string; + alert: string; + crit: string; + error: string; + warning: string; + notice: string; + info: string; + debug: string; + } + export interface Winston { + config: { + cli: {levels: CliConfigLevels, colors: CliConfigColors}, + npm: {levels: NpmConfigLevels, colors: NpmConfigColors}, + syslog: {levels: SyslogConfigLevels, colors: SyslogConfigColors} + }; + transports: winston.Transports; Transport: winston.TransportStatic; Logger: winston.LoggerStatic; @@ -48,8 +129,8 @@ declare namespace winston { remove(transport: 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: any): any; - setLevels(target: any): any; + addColors(target: AbstractConfigColors): any; + setLevels(target: AbstractConfigLevels): any; cli(): winston.LoggerInstance; close(): void; configure(options: winston.LoggerOptions): void; @@ -110,16 +191,30 @@ declare namespace winston { export interface LoggerInstance extends NodeJS.EventEmitter { rewriters: Array; filters: Array; - transports: Array; + transports: {[key: string]: TransportInstance}; extend(target: any): LoggerInstance; log: LogMethod; - debug: LeveledLogMethod; - info: LeveledLogMethod; - warn: LeveledLogMethod; + // for cli levels error: LeveledLogMethod; + warn: LeveledLogMethod; + help: LeveledLogMethod; + data: LeveledLogMethod; + info: LeveledLogMethod; + debug: LeveledLogMethod; + prompt: LeveledLogMethod; + verbose: LeveledLogMethod; + input: LeveledLogMethod; + silly: LeveledLogMethod; + + // for syslog levels only + emerg: LeveledLogMethod + alert: LeveledLogMethod; + crit: LeveledLogMethod; + warning: LeveledLogMethod; + notice: LeveledLogMethod; query(options: QueryOptions, callback?: (err: Error, results: any) => void): any; query(callback: (err: Error, results: any) => void): any; @@ -133,9 +228,9 @@ 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: any): any; + setLevels(target: AbstractConfigLevels): any; cli(): LoggerInstance; - + level: string; } @@ -144,6 +239,8 @@ declare namespace winston { rewriters?: TransportInstance[]; exceptionHandlers?: TransportInstance[]; handleExceptions?: boolean; + level?: string; + levels?: AbstractConfigLevels, /** * @type {(boolean|(err: Error) => void)} @@ -265,7 +362,7 @@ declare namespace winston { has(id: string): boolean; close(id: string): void; options: LoggerOptions; - loggers: any; + loggers: {[key: string]: LoggerInstance}; default: LoggerOptions; } @@ -308,8 +405,8 @@ declare namespace winston { host?: string; port?: number; auth?: { - username: string; - password: string; + username: string; + password: string; }; path?: string; } @@ -329,8 +426,8 @@ declare namespace winston { filename?: string; dirname?: string; options?: { - flags?: string; - highWaterMark?: number; + flags?: string; + highWaterMark?: number; }; stream?: NodeJS.WritableStream; } @@ -347,8 +444,8 @@ declare namespace winston { filename?: string; dirname?: string; options?: { - flags?: string; - highWaterMark?: number; + flags?: string; + highWaterMark?: number; }; stream?: NodeJS.WritableStream; } @@ -363,9 +460,9 @@ declare namespace winston { export interface WebhookTransportOptions extends GenericTransportOptions, GenericNetworkTransportOptions { method?: string; ssl?: { - key?: any; - cert?: any; - ca: any; + key?: any; + cert?: any; + ca: any; }; } @@ -404,4 +501,4 @@ declare namespace winston { interface LogCallback { (error?: any, level?: string, msg?: string, meta?: any): void; } -} \ No newline at end of file +} diff --git a/winston/winston-tests.ts b/winston/winston-tests.ts index dae649d1f9..589a0fc226 100644 --- a/winston/winston-tests.ts +++ b/winston/winston-tests.ts @@ -267,3 +267,23 @@ var logger: winston.LoggerInstance = new (winston.Logger)({ logger.configure({ level: 'silly' }); winston.default.warn("Don't export reserved words in JavaScript!"); + +winston.default.setLevels(winston.config.syslog.levels); +winston.addColors(winston.config.syslog.colors); +winston.default.emerg('syslog!'); + +// module augment for custom level log +// https://github.com/winstonjs/winston#using-custom-logging-levels +// https://github.com/Microsoft/TypeScript/issues/7545 +declare module 'winston' { + interface LoggerInstance { + yay: winston.LeveledLogMethod; + } +} +const customLevelLogger = new winston.Logger({ + levels: {yay: 0, haha: 1, hoho: 2}, + transports: [ + new (winston.transports.Console)() + ] +}); +customLevelLogger.yay('yay!');