Skip to content

Commit a2bfca1

Browse files
committed
[js] Adding logger documentation
1 parent 00120d3 commit a2bfca1

File tree

1 file changed

+80
-0
lines changed
  • website_and_docs/content/documentation/webdriver/troubleshooting

1 file changed

+80
-0
lines changed

website_and_docs/content/documentation/webdriver/troubleshooting/logging.en.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,86 @@ is causing a problem. (Unfortunately, we can't blame the driver if Selenium is s
7171
This is less useful information where we log things about the servers and the sockets, and header information, etc.
7272
Debug mode is set if either `$DEBUG` is true or `ENV['DEBUG']` has a value.
7373

74+
75+
## Javascript
76+
77+
JS has implemented logging APIs. It has also added LogManager to maintain a collection of loggers.
78+
79+
### 1. Getting a logger can be done in two ways:
80+
81+
```
82+
const logging = require('selenium-webdriver/lib/logging')
83+
```
84+
85+
First method using manager
86+
```
87+
mgr = new logging.LogManager()
88+
logger = mgr.getLogger('')
89+
```
90+
91+
Second method without manager
92+
```
93+
logger = logging.getLogger('')
94+
```
95+
96+
Loggers class uses hierarchical, dot-delimited namespaces (e.g. "" > "webdriver" > "webdriver.logging"). For example, the following code will give a hierarchy of loggers:
97+
```
98+
logger = mgr.getLogger('foo.bar.baz')
99+
```
100+
101+
"" (parent of) > "foo" (parent of) > "foo.bar" (parent of) > "foo.bar.baz" (=logger)
102+
103+
104+
### 2. Logger Level:
105+
106+
The basic levels for JS loggers are: `OFF`, `SEVERE`, `WARNING`, `INFO`, `DEBUG`, `FINE`, `FINER`, `FINEST`, `ALL`.
107+
To log messages at the given level pass the level in the `log()` method:
108+
```
109+
logger.log(logging.Level.INFO, 'This is an info message')
110+
```
111+
112+
You can also use the instance methods to target logs of a particular level:
113+
```
114+
logger.finest('this is the finest message')
115+
logger.finer('this is a finer message')
116+
logger.info('this is an info message')
117+
logger.warning('this is a warning message')
118+
logger.severe('this is a severe message')
119+
```
120+
121+
By default the logger's level is `Level.OFF`
122+
To change the level of the logger:
123+
```
124+
logger.setLevel(logging.Level.INFO)
125+
```
126+
127+
### 3. Log Output:
128+
129+
As opposed to ruby which by default sends logs to console in stdout, JS has the option to add a handler. The handler will be invoked for each message logged with this instance, or any of its descendants.
130+
```
131+
logger.addHandler(callback)
132+
```
133+
134+
JS has provided a method to add console handler to the given logger. The console handler will log all messages using the JavaScript Console API:
135+
```
136+
logging.addConsoleHandler(logger)
137+
```
138+
139+
Similarly, to add console handler to the root logger:
140+
```
141+
logging.installConsoleHandler()
142+
```
143+
144+
### 4. Logging Preference (remote logging API):
145+
146+
JS gives the ability to define log preference for a remote WebDriver session. There are 5 common log types: `BROWSER`, `CLIENT`, `DRIVER`, `PERFORMANCE`, `SERVER`
147+
148+
To set a log level for a particular log type:
149+
```
150+
let prefs = new logging.Preferences()
151+
prefs.setLevel(logging.Type.BROWSER, logging.Level.FINE)
152+
```
153+
74154
-----
75155

76156
{{< alert-content >}}

0 commit comments

Comments
 (0)