From bdad2b46d6aa725ae9eb340fdbd8e4330274e132 Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Tue, 26 Sep 2017 20:45:56 +0200 Subject: [PATCH] [loglevel] Refactored and added log.levels property (#19742) * Refactored loglevel * Fixed linting errors * Moved tests to dedicated test directory --- types/loglevel/index.d.ts | 126 ++++++++---------- types/loglevel/test/loglevel-tests.ts | 43 ++++++ .../loglevel-umd-tests.ts} | 19 ++- types/loglevel/tsconfig.json | 7 +- types/loglevel/tslint.json | 1 + 5 files changed, 113 insertions(+), 83 deletions(-) create mode 100644 types/loglevel/test/loglevel-tests.ts rename types/loglevel/{loglevel-tests.ts => test/loglevel-umd-tests.ts} (59%) create mode 100644 types/loglevel/tslint.json diff --git a/types/loglevel/index.d.ts b/types/loglevel/index.d.ts index e7c9015e0c..74bfdfb3d3 100644 --- a/types/loglevel/index.d.ts +++ b/types/loglevel/index.d.ts @@ -1,41 +1,50 @@ -// Type definitions for loglevel 1.4.0 +// Type definitions for loglevel 1.5 // Project: https://github.com/pimterry/loglevel -// Definitions by: Stefan Profanter , Florian Wagner , Gabor Szmetanko +// Definitions by: Stefan Profanter +// Florian Wagner +// Gabor Szmetanko +// Christian Rackerseder // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +declare var log: Logger; +export as namespace log; +export = log; /** * Log levels */ -declare const enum LogLevel { - TRACE = 0, - DEBUG = 1, - INFO = 2, - WARN = 3, - ERROR = 4, - SILENT = 5 +interface LogLevel { + TRACE: 0; + DEBUG: 1; + INFO: 2; + WARN: 3; + ERROR: 4; + SILENT: 5; } -interface LoggingMethod { +/** + * Possible log level numbers. + */ +type LogLevelNumbers = LogLevel[keyof LogLevel]; - (...message : any[]):void; +type LoggingMethod = (...message: any[]) => void; -} +type MethodFactory = (methodName: string, level: LogLevelNumbers, loggerName: string) => LoggingMethod; -interface MethodFactory { - - (methodName : string, level : LogLevel, loggerName : string):LoggingMethod; - -} - -interface Log { +interface Logger { + /** + * Available log levels. + */ + readonly levels: LogLevel; /** - * Plugin API entry point. This will be called for each enabled method each time the level is set - * (including initially), and should return a MethodFactory to be used for the given log method, at the given level, - * for a logger with the given name. If you'd like to retain all the reliability and features of loglevel, it's + * Plugin API entry point. This will be called for each enabled method each time the level is set + * (including initially), and should return a MethodFactory to be used for the given log method, at the given level, + * for a logger with the given name. If you'd like to retain all the reliability and features of loglevel, it's * recommended that this wraps the initially provided value of log.methodFactory */ - methodFactory:MethodFactory; + methodFactory: MethodFactory; /** * Output trace message to console. @@ -43,71 +52,57 @@ interface Log { * * @param msg any data to log to the console */ - trace(...msg : any[]):void; + trace(...msg: any[]): void; /** * Output debug message to console including appropriate icons * * @param msg any data to log to the console */ - debug(...msg : any[]):void; + debug(...msg: any[]): void; /** * Output info message to console including appropriate icons * * @param msg any data to log to the console */ - info(...msg : any[]):void; + info(...msg: any[]): void; /** * Output warn message to console including appropriate icons * * @param msg any data to log to the console */ - warn(...msg : any[]):void; + warn(...msg: any[]): void; /** * Output error message to console including appropriate icons * * @param msg any data to log to the console */ - error(...msg : any[]):void; - + error(...msg: any[]): void; /** * This disables all logging below the given level, so that after a log.setLevel("warn") call log.warn("something") * or log.error("something") will output messages, but log.info("something") will not. * - * @param level 0=trace to 5=silent + * @param level as a string, like 'error' (case-insensitive) or as a number from 0 to 5 (or as log.levels. values) * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass * false as the optional 'persist' second argument, persistence will be skipped. */ - setLevel(level : LogLevel, persist? : boolean):void; - - - /** - * This disables all logging below the given level, so that after a log.setLevel("warn") call log.warn("something") - * or log.error("something") will output messages, but log.info("something") will not. - * - * @param level as a string, like 'error' (case-insensitive) - * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling - * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass - * false as the optional 'persist' second argument, persistence will be skipped. - */ - setLevel(level : string, persist? : boolean):void; - - - /** - * This disables all logging below the given level, so that after a log.setLevel("warn") call log.warn("something") - * or log.error("something") will output messages, but log.info("something") will not. - * - * @param level as the value from the enum - * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling - * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass - * false as the optional 'persist' second argument, persistence will be skipped. - */ - setLevel(level : LogLevel, persist? : boolean):void; + setLevel( + level: + LogLevelNumbers + | 'trace' + | 'debug' + | 'info' + | 'warn' + | 'error' + | 'silent' + | keyof LogLevel, + persist?: boolean + ): void; /** * If you're using another JavaScript library that exposes a 'log' global, you can run into conflicts with loglevel. @@ -115,16 +110,16 @@ interface Log { * onto the page. This resets to 'log' global to its value before loglevel was loaded (typically undefined), and * returns the loglevel object, which you can then bind to another name yourself. */ - noConflict():any; + noConflict(): any; /** - * Returns the current logging level, as a value from the enum. + * Returns the current logging level, as a value from LogLevel. * It's very unlikely you'll need to use this for normal application logging; it's provided partly to help plugin * development, and partly to let you optimize logging code as below, where debug data is only generated if the * level is set such that it'll actually be logged. This probably doesn't affect you, unless you've run profiling * on your code and you have hard numbers telling you that your log data generation is a real performance problem. */ - getLevel():LogLevel; + getLevel(): LogLevel[keyof LogLevel]; /** * This sets the current log level only if one has not been persisted and can’t be loaded. This is useful when @@ -139,7 +134,7 @@ interface Log { * * @param level as the value from the enum */ - setDefaultLevel(level : LogLevel):void; + setDefaultLevel(level: LogLevel): void; /** * This gets you a new logger object that works exactly like the root log object, but can have its level and @@ -153,7 +148,7 @@ interface Log { * circumstances. * @param name The name of the produced logger */ - getLogger(name : String):Log; + getLogger(name: string): Logger; /** * This enables all log messages, and is equivalent to log.setLevel("trace"). @@ -162,7 +157,7 @@ interface Log { * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass * false as the optional 'persist' second argument, persistence will be skipped. */ - enableAll(persist? : boolean):void; + enableAll(persist?: boolean): void; /** * This disables all log messages, and is equivalent to log.setLevel("silent"). @@ -171,12 +166,5 @@ interface Log { * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass * false as the optional 'persist' second argument, persistence will be skipped. */ - disableAll(persist? : boolean):void; - -} - -declare var log : Log; - -declare module "loglevel" { - export = log; + disableAll(persist?: boolean): void; } diff --git a/types/loglevel/test/loglevel-tests.ts b/types/loglevel/test/loglevel-tests.ts new file mode 100644 index 0000000000..351de4cf67 --- /dev/null +++ b/types/loglevel/test/loglevel-tests.ts @@ -0,0 +1,43 @@ +import * as moduleLog from 'loglevel'; + +moduleLog.trace('Trace message'); +moduleLog.debug('Debug message'); +moduleLog.info('Info message'); +moduleLog.warn('Warn message'); +moduleLog.error('Error message'); +moduleLog.debug(['Hello', 'world', 42]); + +moduleLog.setLevel(0); +moduleLog.setLevel(0, false); + +moduleLog.setLevel('error'); +moduleLog.setLevel('ERROR'); +moduleLog.setLevel('error', false); +moduleLog.setLevel('ERROR', false); + +moduleLog.setLevel(moduleLog.levels.WARN); +moduleLog.setLevel(moduleLog.levels.WARN, false); + +moduleLog.enableAll(false); +moduleLog.enableAll(); +moduleLog.disableAll(true); +moduleLog.disableAll(); + +const logLevel = moduleLog.getLevel(); + +const testLogger = moduleLog.getLogger('TestLogger'); + +testLogger.setLevel(logLevel); +testLogger.warn('logging test'); + +const logging = moduleLog.noConflict(); + +logging.error("still pretty easy"); + +// TODO: only works in a global environment +// moduleLog.methodFactory = (methodName: string, level: number, loggerName :string) => { +// return function(...messages: any[]) { +// }; +// }; + +// ----------- global tests ---------- diff --git a/types/loglevel/loglevel-tests.ts b/types/loglevel/test/loglevel-umd-tests.ts similarity index 59% rename from types/loglevel/loglevel-tests.ts rename to types/loglevel/test/loglevel-umd-tests.ts index c5bd30b59c..a052f7cc53 100644 --- a/types/loglevel/loglevel-tests.ts +++ b/types/loglevel/test/loglevel-umd-tests.ts @@ -1,5 +1,3 @@ - - log.trace("Trace message"); log.debug("Debug message"); log.info("Info message"); @@ -13,26 +11,25 @@ log.setLevel(0, false); log.setLevel("error"); log.setLevel("error", false); -log.setLevel(LogLevel.WARN); -log.setLevel(LogLevel.WARN, false); +log.setLevel(log.levels.WARN); +log.setLevel(log.levels.WARN, false); log.enableAll(false); log.enableAll(); log.disableAll(true); log.disableAll(); -var logLevel = log.getLevel(); +const logLevel = log.getLevel(); -var testLogger = log.getLogger("TestLogger"); +const testLogger = log.getLogger("TestLogger"); testLogger.setLevel(logLevel); testLogger.warn("logging test"); -var logging = log.noConflict(); +const logging = log.noConflict(); logging.error("still pretty easy"); -log.methodFactory = function(methodName: string, level: LogLevel, loggerName :string) { - return function(...messages: any[]) { - }; -}; \ No newline at end of file +log.methodFactory = (methodName: string, level: number, loggerName: string) => { + return (...messages: any[]) => {}; +}; diff --git a/types/loglevel/tsconfig.json b/types/loglevel/tsconfig.json index 858fabbb22..2533113a54 100644 --- a/types/loglevel/tsconfig.json +++ b/types/loglevel/tsconfig.json @@ -1,4 +1,4 @@ -{ +{ "compilerOptions": { "module": "commonjs", "lib": [ @@ -17,6 +17,7 @@ }, "files": [ "index.d.ts", - "loglevel-tests.ts" + "test/loglevel-tests.ts", + "test/loglevel-umd-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/loglevel/tslint.json b/types/loglevel/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/loglevel/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }