我可以在控制台中记录不同类型的消息吗?

Can i log different TYPES of message in the console?

我浏览了 console.log 的 MDN 文档,它表明没有额外的参数定义正在记录的消息类型。尽管如此,我在 Web 浏览器的控制台中看到明显为红色的消息,表明发生了非常重要的错误。

所以我想知道这是否严格来说是一种浏览器功能,用于指示明显的红色错误?这意味着通过 Javascript 写入控制台的任何内容都将始终是普通的默认外观?或者我只是缺少一些额外的功能来向控制台写入更多 "complex" 数据?

所以,这里的目标是让我能够编写默认外观的通知,例如 "service started, everything is ok",然后是 "image is missing, but it's fine" 之类的显示为黄色警告的警告,最后"could not perform action because server has burnt down" 之类的错误会显示为红色警告,就像我的浏览器对 "Uncaught DOMExceptions" 所做的一样。

是否可以编程方式(通过 Javascript)?

您可以使用以下代码向 console.log 错误添加 CSS 样式;

console.log('%c Something went wrong','color:red; font-size: 20px');

console.log('%c'+ myVariable,'color:red; font-size: 20px');

或者您可以使用日志、警告、信息和警报

console.alert('Something went wrong');
// If there are different types of messages in the console.
// for example:

console.log("my message");

console.error("my error");

console.warn("my warn");

console.info("my info");

信息:https://developer.mozilla.org/es/docs/Web/API/Console