The log.entryAdded event of the log module fires when a new log entry is created in the browser, from either a console API call or an unhandled JavaScript error.
Event data
The params field in the event notification is a log entry object. Based on the source of the log, the log entry object has different types: "console" or "javascript". Each type may provide additional fields specific to that source.
Common fields
All log entry objects include the following fields:
level- : A string that indicates the severity of the log entry. It has one of the following values:
"debug": A debug-level message (from console/debug_static or console/trace_static)."info": An informational message (from console/log_static, console/info_static, and other console methods that don't produce a more specific level)."warn": A warning message (from console/warn_static)."error": An error message (from console/error_static or console/assert_static).
- : A string that indicates the severity of the log entry. It has one of the following values:
source- : An object that identifies the realm where the log entry was created. It contains the following fields:
realm- : A string that contains the ID of the realm.
context- : A string that contains the ID of the context in which the log entry was created.
userContext- : A string that contains the ID of the user context in which the script-related event occurred.
- : An object that identifies the realm where the log entry was created. It contains the following fields:
stackTrace- : An object that represents the JavaScript stack at the point the log entry was created.
It contains the following field:
callFrames- : An array of zero or more stack-frame objects.
Each object contains the following fields:
columnNumber- : A non-negative integer that contains the zero-based column number of the executed code.
functionName- : A string that contains the name of the executing function.
lineNumber- : A non-negative integer that contains the zero-based line number of the executed code.
url- : A string that contains the URL of the script.
- : An array of zero or more stack-frame objects.
Each object contains the following fields:
- : An object that represents the JavaScript stack at the point the log entry was created.
It contains the following field:
text- : A string that contains the log message or
nullif not available. For console entries, it is the concatenation of all stringified arguments joined by spaces, and for JavaScript errors, it is generally the error message. The exact format is browser-dependent, so don't rely on this value for assertions in tests.
- : A string that contains the log message or
timestamp- : A non-negative integer that represents the time when the log entry was created, as milliseconds elapsed since the epoch (Date.now()).
type- : A string that identifies the source of the log entry. It has one of the following values:
"console": Indicates that the log entry was generated from a call to a console API method (for example, console/log_static, console/warn_static). Log entry objects of this type include additional fields."javascript": Indicates that the log entry was generated from an unhandled JavaScript error.
- : A string that identifies the source of the log entry. It has one of the following values:
"console" log entry fields
In addition to the common fields, log entry objects with "type": "console" also include:
args- : An array of objects that represent the arguments passed to the console method. Each object has a
typefield (such as"string","number","boolean", or"array") and optionalvalue,handle, andinternalIdfields.
- : An array of objects that represent the arguments passed to the console method. Each object has a
method- : A string that contains the name of the console method that was called (for example,
"log","error","assert","debug","trace","warn").
- : A string that contains the name of the console method that was called (for example,
Examples
Receiving an event for a console log
With a WebDriver BiDi connection and a subscription to log.entryAdded active, the browser sends a log.entryAdded event when a script evaluates console.log("hello", [1, true, "foo"]):
{
"type": "event",
"method": "log.entryAdded",
"params": {
"type": "console",
"method": "log",
"source": {
"realm": "7c37f4c0-abcd-1234-ef56-789012345678",
"context": "6B3D5B3A-6571-432B-8E96-E53B5C2ECBB5"
},
"args": [
{
"type": "string",
"value": "hello"
},
{
"type": "array",
"value": [
{ "type": "number", "value": 1 },
{ "type": "boolean", "value": true },
{ "type": "string", "value": "foo" }
]
}
],
"level": "info",
"text": "hello 1,true,foo",
"timestamp": 1712345678901,
"stackTrace": {
"callFrames": [
{
"columnNumber": 8,
"functionName": "",
"lineNumber": 1,
"url": "https://example.com/app.js"
}
]
}
}
}
Receiving an event for a console warning
With a WebDriver BiDi connection and a subscription to log.entryAdded active, the browser sends a log.entryAdded event when a script evaluates console.warn("something went wrong"):
{
"type": "event",
"method": "log.entryAdded",
"params": {
"type": "console",
"method": "warn",
"source": {
"realm": "7c37f4c0-abcd-1234-ef56-789012345678",
"context": "6B3D5B3A-6571-432B-8E96-E53B5C2ECBB5"
},
"args": [
{
"type": "string",
"value": "something went wrong"
}
],
"level": "warn",
"text": "something went wrong",
"timestamp": 1712345678950,
"stackTrace": {
"callFrames": [
{
"columnNumber": 8,
"functionName": "",
"lineNumber": 1,
"url": "https://example.com/app.js"
}
]
}
}
}
Receiving an event for an unhandled JavaScript error
With a WebDriver BiDi connection and a subscription to log.entryAdded active, the browser sends a log.entryAdded event when an unhandled JavaScript error occurs:
{
"type": "event",
"method": "log.entryAdded",
"params": {
"type": "javascript",
"level": "error",
"source": {
"realm": "7c37f4c0-abcd-1234-ef56-789012345678",
"context": "6B3D5B3A-6571-432B-8E96-E53B5C2ECBB5"
},
"text": "ReferenceError: undefinedVariable is not defined",
"timestamp": 1712345679100,
"stackTrace": {
"callFrames": [
{
"columnNumber": 27,
"functionName": "",
"lineNumber": 3,
"url": "https://example.com/app.js"
},
{
"columnNumber": 18,
"functionName": "",
"lineNumber": 3,
"url": "https://example.com/app.js"
}
]
}
}
}
Specifications
Browser compatibility
See also
session.subscribecommandconsoleAPI
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
