- 安装依赖
npm install debug
- 编写脚本 logging.js
'use strict';
const debug = require('debug');
let loggerProvider = debugLoggerProvider;
module.exports = exports = function getLogger (name){
return loggerProvider(name);
}
exports.setLoggerProvider = function setLoggerProvider (provider){
loggerProvider = provider;
}
function debugLoggerProvider (name){
let logger = debug(name);
console.log(logger);
logger = logger.bind(logger);
return {
debug: logger,
info: logger,
warn: logger,
error: logger
};
}
- 编写测试脚本 testDebug.js
'use strict';
var logger = require('./logging')('testDebug');
console.log('hello');
logger.info('这个是测试Debug');
logger.debug('这个是测试Debug');
- 执行脚本
node .\testDebug.js
- 发现只打印了hello
- 设置debug环境
$env:DEBUG="*,-not_this"
-
再次执行,效果
image.png
网友评论