admin管理员组文章数量:1300047
I am try to make a logging service for my TypeScript / Angular 2 App. Unfortunately if i call console.log the line number is wrong. Even if i try to return console.log()
.
Here is my code:
LoggerService.ts
export class LoggerService {
log(message) {
// Server-side logging
// [...]
if (clientSideLogging) return console.log(message);
}
}
SomewhereElse.ts
this.logger.log('hello world');
-> Shows line number of LoggerService.ts instead of source
I am try to make a logging service for my TypeScript / Angular 2 App. Unfortunately if i call console.log the line number is wrong. Even if i try to return console.log()
.
Here is my code:
LoggerService.ts
export class LoggerService {
log(message) {
// Server-side logging
// [...]
if (clientSideLogging) return console.log(message);
}
}
SomewhereElse.ts
this.logger.log('hello world');
-> Shows line number of LoggerService.ts instead of source
Share edited Feb 3, 2017 at 1:12 Josh Crozier 241k56 gold badges400 silver badges313 bronze badges asked Jan 31, 2017 at 16:10 MickMick 8,92210 gold badges49 silver badges70 bronze badges 1- is it a typescript pile time error? – Aravind Commented Jan 31, 2017 at 16:12
2 Answers
Reset to default 9You could use the .bind()
method to bind window.console
to your custom log method and then return the function so that the code is executed within the original scope when it is called.
In doing so, the line number will be preserved when calling the logger service's log
method:
class LoggerService {
public log = console.log.bind(window.console);
}
// ...or annotated:
class LoggerService {
public log: (message) => void = console.log.bind(window.console);
}
Then if you want to add in your conditional statement:
class LoggerService {
public log = clientSideLogging ? console.log.bind(window.console) : () => {};
}
Here is an example with the piled TypeScript code.
Aside from the one-liner solutions mentioned above, if you want to implement additional logic inside of the log
method, then you could utilize a getter which will return and call the console.log
function that is bound to window.console
.
class LoggerService {
public get log (): Function {
// Implemnt server-side logging
return console.log.bind(window.console);
}
}
As you can tell, it is important for the console.log
function to be returned since it will not preserve the line numbers when it is called directly within another scope.
Then if you want to add in your conditional statement:
class LoggerService {
public get log (): Function {
const log = console.log.bind(window.console);
// Implemnt server-side logging
return clientSideLogging ? log : () => {};
}
}
Here is an example with the piled TypeScript code.
You could use .trace()
instead of .log()
.
this.logger.trace('hello world');
This will give you a stack trace to the original line number.
https://developer.mozilla/en-US/docs/Web/API/Console/trace
版权声明:本文标题:javascript - Keeping console.log() line numbers in a wrapper function in TypeScriptAngular 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741628316a2389208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论