[loglevel] Refactored and added log.levels property (#19742)

* Refactored loglevel

* Fixed linting errors

* Moved tests to dedicated test directory
This commit is contained in:
Christian Rackerseder
2017-09-26 20:45:56 +02:00
committed by Mohamed Hegazy
parent 2aa66e9396
commit bdad2b46d6
5 changed files with 113 additions and 83 deletions

View File

@@ -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 <https://github.com/Pro>, Florian Wagner <https://github.com/flqw>, Gabor Szmetanko <https://github.com/szmeti>
// Definitions by: Stefan Profanter <https://github.com/Pro>
// Florian Wagner <https://github.com/flqw>
// Gabor Szmetanko <https://github.com/szmeti>
// Christian Rackerseder <https://github.com/screendriver>
// 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 cant 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;
}

View File

@@ -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 ----------

View File

@@ -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[]) {
};
};
log.methodFactory = (methodName: string, level: number, loggerName: string) => {
return (...messages: any[]) => {};
};

View File

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

View File

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