13

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 ?

Jeanluca Scaljeri
  • 22,579
  • 44
  • 171
  • 286

4 Answers4

8

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.

Matthew Campbell
  • 1,108
  • 14
  • 25
5

To keep your logging statements clean, you could define the following function and simply use console.print to log without filename/line numbers:

// console.print: console.log without filename/line number
console.print = function (...args) {
    queueMicrotask (console.log.bind (console, ...args));
}

console.print takes the same arguments as console.log for example:

console.print("Text with no filename info.")

console.print('%c Custom CSS and no line numbers! ', 'background: #555555; color: #00aaff');

tapeboy7
  • 51
  • 1
  • 4
2

Another option is to give your source script files shorter names using sourceURL

//# sourceURL=new_file_name.js 
Darshan
  • 1,044
  • 7
  • 15
0

Use queueMicrotask with console.log.bind to loose the source file info when logging:

queueMicrotask (console.log.bind (console, "Look! No source file info..."));

Queuing microtasks will maintain order of the logging if there are multiple calls, while setTimeout is not guaranteed to execute tasks in order.

Terje Norderhaug
  • 3,559
  • 20
  • 25