Search code examples
javascriptbrowsergoogle-chrome-devtoolsconsole.log

How to omit file/line number with console.log


In the console of Chrome you can write pretty nice stuff these days. Checkout this link. I've also made a screenshot:

enter image description here

As you can see in the screenshot too, the file name / line number (VM298:4) is written at the right. Is it possible the remove that, because in my case this is a very long name and more or less breaks the effect I'm trying to make in my console ?


Solution

  • This is pretty simple to do. You will need to use setTimeout with a console.log.bind:

    setTimeout (console.log.bind (console, "This is a sentence."));
    

    And if you want to apply CSS or additional text to it just add add %c or any other % variable:

    setTimeout (console.log.bind (console, "%cThis is a sentence.", "font-weight: bold;"));
    var css = "text-decoration: underline;";
    setTimeout (console.log.bind (console, "%cThis is a sentence.", css));
    

    Note that this method will always be placed at the end of the log list. For example:

    console.log ("Log 1");
    setTimeout (console.log.bind (console, "This is a sentence."));
    console.log ("Log 2");
    

    will appear as

    Log 1
    Log 2
    This is a sentence.
    

    instead of

    Log 1
    This is a sentence.
    Log 2
    

    I hope this answers your question.