How to omit file/line number with console.log
Asked Answered
A

5

18

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 ?

Argilliferous answered 15/3, 2016 at 10:47 Comment(0)
I
9

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.

Ilona answered 26/2, 2018 at 17:53 Comment(0)
O
12

To keep your logging statements clean, you can define console.print() as a function and simply use console.print() the same as you would use console.log() 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');

Outer answered 20/10, 2020 at 11:20 Comment(3)
Seems it displays logs out of order when nested inside console.group().Consanguinity
strange, i don't see console.printLunn
You have to declare the console.print function yourself (using code from code block above) anywhere in a .js file. Once declared, you can use console.print() the same as you would console.log() and it won't show line numbers.Outer
I
9

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.

Ilona answered 26/2, 2018 at 17:53 Comment(0)
P
3

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

//# sourceURL=new_file_name.js 
Perfumer answered 15/3, 2016 at 12:53 Comment(0)
K
1

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.

Knowhow answered 2/8, 2020 at 21:15 Comment(1)
FYI setTimeout does guarantee order. It just doesn't guarantee it will be called at the exact time.Excusatory
R
0

Ok, sound strange, but none of the above solutions worked for me, and I was in codepen. but I added these line on top my script plus console.print from answers, and it worked.

//# sourceMappingURL-=
//# sourceURL-=
Rife answered 29/3 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.