lib/webdriver/logging.js

1// Copyright 2013 Selenium comitters
2// Copyright 2013 Software Freedom Conservancy
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16goog.provide('webdriver.logging');
17goog.provide('webdriver.logging.Preferences');
18
19goog.require('goog.object');
20
21
22/**
23 * Logging levels.
24 * @enum {{value: number, name: string}}
25 */
26webdriver.logging.Level = {
27 ALL: {value: Number.MIN_VALUE, name: 'ALL'},
28 DEBUG: {value: 700, name: 'DEBUG'},
29 INFO: {value: 800, name: 'INFO'},
30 WARNING: {value: 900, name: 'WARNING'},
31 SEVERE: {value: 1000, name: 'SEVERE'},
32 OFF: {value: Number.MAX_VALUE, name: 'OFF'}
33};
34
35
36/**
37 * Converts a level name or value to a {@link webdriver.logging.Level} value.
38 * If the name/value is not recognized, {@link webdriver.logging.Level.ALL}
39 * will be returned.
40 * @param {(number|string)} nameOrValue The log level name, or value, to
41 * convert .
42 * @return {!webdriver.logging.Level} The converted level.
43 */
44webdriver.logging.getLevel = function(nameOrValue) {
45 var predicate = goog.isString(nameOrValue) ?
46 function(val) { return val.name === nameOrValue; } :
47 function(val) { return val.value === nameOrValue; };
48
49 return goog.object.findValue(webdriver.logging.Level, predicate) ||
50 webdriver.logging.Level.ALL;
51};
52
53
54/**
55 * Common log types.
56 * @enum {string}
57 */
58webdriver.logging.Type = {
59 /** Logs originating from the browser. */
60 BROWSER: 'browser',
61 /** Logs from a WebDriver client. */
62 CLIENT: 'client',
63 /** Logs from a WebDriver implementation. */
64 DRIVER: 'driver',
65 /** Logs related to performance. */
66 PERFORMANCE: 'performance',
67 /** Logs from the remote server. */
68 SERVER: 'server'
69};
70
71
72/**
73 * Describes the log preferences for a WebDriver session.
74 * @constructor
75 */
76webdriver.logging.Preferences = function() {
77 /** @private {!Object.<string, webdriver.logging.Level>} */
78 this.prefs_ = {};
79};
80
81
82/**
83 * Sets the desired logging level for a particular log type.
84 * @param {(string|webdriver.logging.Type)} type The log type.
85 * @param {!webdriver.logging.Level} level The desired log level.
86 */
87webdriver.logging.Preferences.prototype.setLevel = function(type, level) {
88 this.prefs_[type] = level;
89};
90
91
92/**
93 * Converts this instance to its JSON representation.
94 * @return {!Object.<string, string>} The JSON representation of this set of
95 * preferences.
96 */
97webdriver.logging.Preferences.prototype.toJSON = function() {
98 var obj = {};
99 for (var type in this.prefs_) {
100 if (this.prefs_.hasOwnProperty(type)) {
101 obj[type] = this.prefs_[type].name;
102 }
103 }
104 return obj;
105};
106
107
108/**
109 * A single log entry.
110 * @param {(!webdriver.logging.Level|string)} level The entry level.
111 * @param {string} message The log message.
112 * @param {number=} opt_timestamp The time this entry was generated, in
113 * milliseconds since 0:00:00, January 1, 1970 UTC. If omitted, the
114 * current time will be used.
115 * @param {string=} opt_type The log type, if known.
116 * @constructor
117 */
118webdriver.logging.Entry = function(level, message, opt_timestamp, opt_type) {
119
120 /** @type {!webdriver.logging.Level} */
121 this.level =
122 goog.isString(level) ? webdriver.logging.getLevel(level) : level;
123
124 /** @type {string} */
125 this.message = message;
126
127 /** @type {number} */
128 this.timestamp = goog.isNumber(opt_timestamp) ? opt_timestamp : goog.now();
129
130 /** @type {string} */
131 this.type = opt_type || '';
132};
133
134
135/**
136 * @return {{level: string, message: string, timestamp: number,
137 * type: string}} The JSON representation of this entry.
138 */
139webdriver.logging.Entry.prototype.toJSON = function() {
140 return {
141 'level': this.level.name,
142 'message': this.message,
143 'timestamp': this.timestamp,
144 'type': this.type
145 };
146};
147
148
149/**
150 * Converts a {@link goog.debug.LogRecord} into a
151 * {@link webdriver.logging.Entry}.
152 * @param {!goog.debug.LogRecord} logRecord The record to convert.
153 * @param {string=} opt_type The log type.
154 * @return {!webdriver.logging.Entry} The converted entry.
155 */
156webdriver.logging.Entry.fromClosureLogRecord = function(logRecord, opt_type) {
157 var closureLevel = logRecord.getLevel();
158 var level = webdriver.logging.Level.SEVERE;
159
160 if (closureLevel.value <= webdriver.logging.Level.DEBUG.value) {
161 level = webdriver.logging.Level.DEBUG;
162 } else if (closureLevel.value <= webdriver.logging.Level.INFO.value) {
163 level = webdriver.logging.Level.INFO;
164 } else if (closureLevel.value <= webdriver.logging.Level.WARNING.value) {
165 level = webdriver.logging.Level.WARNING;
166 }
167
168 return new webdriver.logging.Entry(
169 level,
170 '[' + logRecord.getLoggerName() + '] ' + logRecord.getMessage(),
171 logRecord.getMillis(),
172 opt_type);
173};