Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript abstract console logging

I want to make a function, like this.

For example:

function Logger() {
    this.log = function(msg) {
        console.log(msg);
    }
}

And I want to use it in functions/modules etc, and that all works fine. But the default console in my browser normally give the fileName + lineNumber.

Now when I abstract this functionality, the fileName and lineNumber is not where I put my instance.log(). Because it will say from where the console.log is being called, not the function itself.

So my question:

How can I get the correct information from where I want to use my logger? Or give me, please, any tips to improve this functionality.

like image 601
Barry Avatar asked Jul 22 '11 12:07

Barry


People also ask

What is the use of console log function in JavaScript?

The console.log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.log(A); Parameters: It accepts a parameter which can be an array, an object or any message. Return value: It returns the value of the parameter given.

How to implement abstraction in JavaScript?

Abstraction in JavaScript can be implemented with the help of Abstract Classes and Abstract Methods. In JavaScript, an Abstract class must contain at least one or more declared Abstract methods, whereas its definition can be added in its sub-classes.

What is console log method in Salesforce?

Definition and Usage. The console.log () method writes a message to the console. The console is useful for testing purposes. Tip: When testing this method, be sure to have the console view visible (press F12 to view the console).

What is the use of console in JavaScript?

The console.log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.log(A);


1 Answers

function Logger() {
    this.log = console.log.bind(console);
}

I asked about this some time ago: Create shortcut to console.log() in Chrome.

like image 128
pimvdb Avatar answered Oct 02 '22 13:10

pimvdb