user2490003
user2490003

Reputation: 11890

Logging errors and warnings in the browser console with vanilla JavaScript

Is there a way to log WARN and ERROR messages to the console using Vanilla JavaScript?

An example would be something that looks like:

enter image description here

I could just print it with console.log() but it prints without color as just a plain message. I'd like to print it as an error or warning so it's colored output and background.

Most online guides suggest using some logger library such as log4javascript. I'm hoping to do this without pulling in any dependency.

Thanks!

Upvotes: 3

Views: 2510

Answers (2)

Luis Barajas
Luis Barajas

Reputation: 486

You can use console.warn and console.error functions. First for warnings and second for errors.

https://developer.mozilla.org/es/docs/Web/API/Console/error https://developer.mozilla.org/es/docs/Web/API/Console/warn

Upvotes: 3

wdm
wdm

Reputation: 7189

console.warn('warning message');

Outputs a warning message to the Web Console.

https://developer.mozilla.org/en-US/docs/Web/API/Console/warn

console.error('error message');

Outputs an error message to the Web Console.

https://developer.mozilla.org/en-US/docs/Web/API/Console/error

Upvotes: 1

Related Questions