UNPKG

9.85 kBTypeScriptView Raw
1/**
2 * Defines a message level that may be used to control logging output.
3 *
4 * @final
5 */
6export class Level {
7 name_: string;
8 value_: number;
9
10 /**
11 * @param {string} name the level's name.
12 * @param {number} level the level's numeric value.
13 */
14 constructor(name: string, level: number);
15
16 /** @override */
17 toString(): string;
18
19 /** This logger's name. */
20 name: string;
21
22 /** The numeric log level. */
23 value: number;
24
25 /**
26 * Indicates no log messages should be recorded.
27 * @const
28 */
29 static OFF: Level;
30 /**
31 * Log messages with a level of `1000` or higher.
32 * @const
33 */
34 static SEVERE: Level;
35 /**
36 * Log messages with a level of `900` or higher.
37 * @const
38 */
39 static WARNING: Level;
40 /**
41 * Log messages with a level of `800` or higher.
42 * @const
43 */
44 static INFO: Level;
45 /**
46 * Log messages with a level of `700` or higher.
47 * @const
48 */
49 static DEBUG: Level;
50 /**
51 * Log messages with a level of `500` or higher.
52 * @const
53 */
54 static FINE: Level;
55 /**
56 * Log messages with a level of `400` or higher.
57 * @const
58 */
59 static FINER: Level;
60 /**
61 * Log messages with a level of `300` or higher.
62 * @const
63 */
64 static FINEST: Level;
65 /**
66 * Indicates all log messages should be recorded.
67 * @const
68 */
69 static ALL: Level;
70}
71
72/**
73 * Converts a level name or value to a {@link logging.Level} value.
74 * If the name/value is not recognized, {@link logging.Level.ALL}
75 * will be returned.
76 * @param {(number|string)} nameOrValue The log level name, or value, to
77 * convert .
78 * @return {!logging.Level} The converted level.
79 */
80export function getLevel(nameOrValue: string | number): Level;
81
82export interface IEntryJSON {
83 level: string;
84 message: string;
85 timestamp: number;
86 type: string;
87}
88
89/**
90 * A single log entry.
91 */
92export class Entry {
93 /**
94 * @param {(!logging.Level|string)} level The entry level.
95 * @param {string} message The log message.
96 * @param {number=} opt_timestamp The time this entry was generated, in
97 * milliseconds since 0:00:00, January 1, 1970 UTC. If omitted, the
98 * current time will be used.
99 * @param {string=} opt_type The log type, if known.
100 * @constructor
101 */
102 constructor(level: Level | string | number, message: string, opt_timestamp?: number, opt_type?: string | IType);
103
104 /** @type {!logging.Level} */
105 level: Level;
106
107 /** @type {string} */
108 message: string;
109
110 /** @type {number} */
111 timestamp: number;
112
113 /** @type {string} */
114 type: string;
115
116 /**
117 * @return {{level: string, message: string, timestamp: number,
118 * type: string}} The JSON representation of this entry.
119 */
120 toJSON(): IEntryJSON;
121}
122
123/**
124 * An object used to log debugging messages. Loggers use a hierarchical,
125 * dot-separated naming scheme. For instance, 'foo' is considered the parent of
126 * the 'foo.bar' and an ancestor of 'foo.bar.baz'.
127 *
128 * Each logger may be assigned a {@linkplain #setLevel log level}, which
129 * controls which level of messages will be reported to the
130 * {@linkplain #addHandler handlers} attached to this instance. If a log level
131 * is not explicitly set on a logger, it will inherit its parent.
132 *
133 * This class should never be directly instantiated. Instead, users should
134 * obtain logger references using the {@linkplain ./logging.getLogger()
135 * getLogger()} function.
136 *
137 * @final
138 */
139export class Logger {
140 /**
141 * @param {string} name the name of this logger.
142 * @param {Level=} opt_level the initial level for this logger.
143 */
144 constructor(name: string, opt_level?: Level);
145
146 /** @private {string} */
147 name_: string;
148 /** @private {Level} */
149 level_: Level;
150 /** @private {Logger} */
151 parent_: Logger;
152 /** @private {Set<function(!Entry)>} */
153 handlers_: any;
154
155 /** @return {string} the name of this logger. */
156 getName(): string;
157
158 /**
159 * @param {Level} level the new level for this logger, or `null` if the logger
160 * should inherit its level from its parent logger.
161 */
162 setLevel(level: Level): void;
163
164 /** @return {Level} the log level for this logger. */
165 getLevel(): Level;
166
167 /**
168 * @return {!Level} the effective level for this logger.
169 */
170 getEffectiveLevel(): Level;
171
172 /**
173 * @param {!Level} level the level to check.
174 * @return {boolean} whether messages recorded at the given level are loggable
175 * by this instance.
176 */
177 isLoggable(level: Level): boolean;
178
179 /**
180 * Adds a handler to this logger. The handler will be invoked for each message
181 * logged with this instance, or any of its descendants.
182 *
183 * @param {function(!Entry)} handler the handler to add.
184 */
185 addHandler(handler: any): void;
186
187 /**
188 * Removes a handler from this logger.
189 *
190 * @param {function(!Entry)} handler the handler to remove.
191 * @return {boolean} whether a handler was successfully removed.
192 */
193 removeHandler(handler: any): void;
194
195 /**
196 * Logs a message at the given level. The message may be defined as a string
197 * or as a function that will return the message. If a function is provided,
198 * it will only be invoked if this logger's
199 * {@linkplain #getEffectiveLevel() effective log level} includes the given
200 * `level`.
201 *
202 * @param {!Level} level the level at which to log the message.
203 * @param {(string|function(): string)} loggable the message to log, or a
204 * function that will return the message.
205 */
206 log(level: Level, loggable: string | Function): void;
207
208 /**
209 * Logs a message at the {@link Level.SEVERE} log level.
210 * @param {(string|function(): string)} loggable the message to log, or a
211 * function that will return the message.
212 */
213 severe(loggable: string | Function): void;
214
215 /**
216 * Logs a message at the {@link Level.WARNING} log level.
217 * @param {(string|function(): string)} loggable the message to log, or a
218 * function that will return the message.
219 */
220 warning(loggable: string | Function): void;
221
222 /**
223 * Logs a message at the {@link Level.INFO} log level.
224 * @param {(string|function(): string)} loggable the message to log, or a
225 * function that will return the message.
226 */
227 info(loggable: string | Function): void;
228
229 /**
230 * Logs a message at the {@link Level.DEBUG} log level.
231 * @param {(string|function(): string)} loggable the message to log, or a
232 * function that will return the message.
233 */
234 debug(loggable: string | Function): void;
235
236 /**
237 * Logs a message at the {@link Level.FINE} log level.
238 * @param {(string|function(): string)} loggable the message to log, or a
239 * function that will return the message.
240 */
241 fine(loggable: string | Function): void;
242
243 /**
244 * Logs a message at the {@link Level.FINER} log level.
245 * @param {(string|function(): string)} loggable the message to log, or a
246 * function that will return the message.
247 */
248 finer(loggable: string | Function): void;
249
250 /**
251 * Logs a message at the {@link Level.FINEST} log level.
252 * @param {(string|function(): string)} loggable the message to log, or a
253 * function that will return the message.
254 */
255 finest(loggable: string | Function): void;
256}
257
258/**
259 * Maintains a collection of loggers.
260 *
261 * @final
262 */
263export class LogManager {
264 /**
265 * Retrieves a named logger, creating it in the process. This function will
266 * implicitly create the requested logger, and any of its parents, if they
267 * do not yet exist.
268 *
269 * @param {string} name the logger's name.
270 * @return {!Logger} the requested logger.
271 */
272 getLogger(name?: string): Logger;
273
274 /**
275 * Creates a new logger.
276 *
277 * @param {string} name the logger's name.
278 * @param {!Logger} parent the logger's parent.
279 * @return {!Logger} the new logger.
280 * @private
281 */
282 createLogger_(name: string, parent: Logger): Logger;
283}
284
285/**
286 * Retrieves a named logger, creating it in the process. This function will
287 * implicitly create the requested logger, and any of its parents, if they
288 * do not yet exist.
289 *
290 * @param {string} name the logger's name.
291 * @return {!Logger} the requested logger.
292 */
293export function getLogger(name?: string): Logger;
294
295/**
296 * Adds the console handler to the given logger. The console handler will log
297 * all messages using the JavaScript Console API.
298 *
299 * @param {Logger=} opt_logger The logger to add the handler to; defaults
300 * to the root logger.
301 */
302export function addConsoleHandler(opt_logger?: Logger): void;
303
304/**
305 * Removes the console log handler from the given logger.
306 *
307 * @param {Logger=} opt_logger The logger to remove the handler from; defaults
308 * to the root logger.
309 * @see exports.addConsoleHandler
310 */
311export function removeConsoleHandler(opt_logger?: Logger): void;
312
313/**
314 * Installs the console log handler on the root logger.
315 */
316export function installConsoleHandler(): void;
317
318export interface IType {
319 /** Logs originating from the browser. */
320 BROWSER: string;
321 /** Logs from a WebDriver client. */
322 CLIENT: string;
323 /** Logs from a WebDriver implementation. */
324 DRIVER: string;
325 /** Logs related to performance. */
326 PERFORMANCE: string;
327 /** Logs from the remote server. */
328 SERVER: string;
329}
330
331/**
332 * Common log types.
333 * @enum {string}
334 */
335export const Type: IType;
336
337/**
338 * Describes the log preferences for a WebDriver session.
339 */
340export class Preferences {
341 prefs_: Map<string, Level>;
342
343 constructor();
344
345 /**
346 * Sets the desired logging level for a particular log type.
347 * @param {(string|Type)} type The log type.
348 * @param {(!Level|string|number)} level The desired log level.
349 * @throws {TypeError} if `type` is not a `string`.
350 */
351 setLevel(type: string | IType, level: Level): void;
352
353 /**
354 * Converts this instance to its JSON representation.
355 * @return {!Object<string, string>} The JSON representation of this set of
356 * preferences.
357 */
358 toJSON(): { [key: string]: string };
359}