8

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

1
  • is it a typescript compile time error? Commented Jan 31, 2017 at 16:12

2 Answers 2

9

You 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 compiled 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 compiled TypeScript code.

Sign up to request clarification or add additional context in comments.

7 Comments

what about the server side logging and other actions that should be performed on logger.log(msg)?
@JoshCrozier Is there a way to access the arguments sent to the log(). I want to send them to server.
Finally a solution that WORKS!! this should be starred!
@Granga's comment is the big deal breaker with this solution. Binding to console.log is great, but not being able to do anything else with the message input means it doesn't answer the question.
@Tawm Back when I had this problem, I found out that we can still use bind but for log to bring us back to the wanted line of code, we have to use another pair of brackets. You can see an example here. My requirement was to be able to send log parameters to server when an error was logged on client.
|
0

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.org/en-US/docs/Web/API/Console/trace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.