今天有点纠结这个,还是报错。文档在这里。我最接近的是:
constructor(region, sslEnabled = true,
logger = () => {}, errorLogger = () => {}) {
if (region === undefined || !region)
throw new Error("Initialization error: region is required");
this.log = () => {}; // these two lines might be unnecessary
this.logError = () => {}; //
this.log = logger.bind(this);
this.logError = errorLogger.bind(this);
this.region = region;
this.sslEnabled = sslEnabled;
}
在课堂上的其他地方,我发送了一个 bunyan 记录器的函数:
const tools = new Tools(
config.region,
config.ssl,
logger.debug,
logger.error
);
记录器只是使用控制台输出。如果我通过console.log
并且console.error
如果我通过 Bunyan 记录器则失败:
bunyan usage error: /usr/src/app/src/healthcheck.js:47:
attempt to log with an unbound log method: `this` is: TTools {
log: [Function: bound ],
logError: [Function: bound ],
region: 'us-west-2',
sslEnabled: false }
有一个关于这个的github问题,但它并没有明确说明如何解决它。如何将 bunyan 记录器函数传递logger.error
给另一个对象?这可能吗?