-
Notifications
You must be signed in to change notification settings - Fork 2
/
log-behavior.html
160 lines (129 loc) · 3.97 KB
/
log-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="./debug-element.html">
<script type="text/javascript">
(function () {
'use strict';
/**
* Available log levels
*/
var LOG_LEVELS = [
'info',
'debug',
'trace',
'warn',
'error'
];
/**
* Get log function for name and level
*/
function _getLogFn(name, level) {
var logFn = null;
if (typeof debug === 'function' && typeof level === 'string') {
var namespace = name + ':' + level;
logFn = debug(namespace);
logFn.log = console[level].bind(console);
}
return logFn;
}
/**
* Get log instance for element
*/
function _getLogInstance(element) {
var logInstance = {};
LOG_LEVELS.forEach(function (level) {
var logFn = _getLogFn(element.is, level);
if (typeof logFn === 'function') {
logInstance[level] = function () {
if (element.verbose) {
var args = [].slice.call(arguments);
logFn.apply(element, args);
}
}
}
});
return logInstance;
}
/**
* `PolymerElements.LogBehavior` wraps the [visionmedia/debug](https://github.com/visionmedia/debug#browser-support) library to
* enable filterable logging per element and level.
*
* To enable logs `visionmedia/debug` requires localeStorage.debug to be set to your desired scope,
* e.g. `my-element:error`.
* For your convenience this can be done using an instance of the bundled <locale-storage> element, instead of
* setting the scope directly via your browser console.
*
* It is meant to be used in development as well as in production.
*
* Example usage:
*
* <dom-module id="my-element">
* <template>
* <locale-storage debug="my-element:*"></locale-storage>
* </template>
* <script>
* Polymer({
*
* is: 'my-element',
*
* behaviors: [
* PolymerElements.LogBehavior
* ],
*
* ready: function() {
* this._log.info('this is an info message');
* this._log.debug('this is a debug message');
* this._log.trace('this is a trace message');
* this._log.warn('this is a warn message');
* this._log.error('this is a error message');
* },
* });
* </script>
* </dom-module>
*
*
* @demo demo/index.html
* @polymerBehavior PolymerElements.LogBehavior
*
*/
var LogBehaviorImpl = {
'properties': {
/**
* Enable / disable logging for element.
* Logs for element are only shown if set to `true`.
*/
'verbose': {
'type': Boolean,
'notify': false,
'readOnly': false,
'reflectToAttribute': true,
'value': false
},
/**
* Private log instance for element
*
* For example:
*
* this._log.info('this is an info message');<br>
* this._log.debug('this is a debug message');<br>
* this._log.trace('this is a debug message');<br>
* this._log.warn('this is a warn message');<br>
* this._log.error('this is an error message');<br>
*/
'_log': {
'type': Object,
'notify': false,
'readOnly': true,
'value': function () {
var element = this;
var logInstance = _getLogInstance(element);
return logInstance;
}
}
}
};
// define namespace
window.PolymerElements = window.PolymerElements || {};
// register behavior
window.PolymerElements.LogBehavior = window.PolymerElements.LogBehavior || LogBehaviorImpl;
})();
</script>