-
Notifications
You must be signed in to change notification settings - Fork 0
/
oo-logger-mixin.html
182 lines (144 loc) · 4.76 KB
/
oo-logger-mixin.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="oo-logger-base.html">
<script>
// Declare in local function scope to avoid bleeding variables into global `window`.
(function () {
// Declare or reuse global namespace.
window.oo = window.oo || {}
/**
* A custom element class mixin that provides a collection of utility methods.
*
* @polymerMixin
* @memberof oo
*/
oo.LoggerMixin = Polymer.dedupingMixin(function (superClass) {
/**
* @polymerMixinClass
*/
return class LoggerMixin extends superClass {
constructor() {
super()
this._logger = oo.LoggerFactory.getLogger(this.localName)
}
_trace(...messages) {
this._logger.trace(...messages)
}
_debug(...messages) {
this._logger.debug(...messages)
}
_info(...messages) {
this._logger.info(...messages)
}
_warn(...messages) {
this._logger.warn(...messages)
}
_error(...messages) {
this._logger.error(...messages)
}
}
})
oo.LoggerFactory = class LoggerFactory {
static getLogger(contextName = ROOT_CONTEXT_NAME) {
_checkNotNil(contextName, () => "illegal argument: contextName is missing")
// return root logger.
if (contextName === ROOT_CONTEXT_NAME) {
return ROOT_LOGGER
}
// return registered logger.
const logger = LOGGER_CONTEXTS[contextName]
if (logger) {
return logger
}
// return newly registered logger.
const newLogger = new oo.Logger(contextName, ROOT_LOGGER)
LOGGER_CONTEXTS[contextName] = newLogger
return newLogger
}
static getLoggerContexts() {
return LOGGER_CONTEXTS
}
static getLogLevels() {
return LOG_LEVELS
}
}
oo.Logger = class Logger {
constructor(contextName, parentLogger = null) {
_checkNotNil(contextName, () => "illegal argument: contextName is missing")
this._contextName = contextName
this._parentLogger = parentLogger
this._logLevel = null
}
error() { this._log(LOG_LEVELS.ERROR, ...arguments) }
warn() { this._log(LOG_LEVELS.WARN, ...arguments) }
info() { this._log(LOG_LEVELS.INFO, ...arguments) }
debug() { this._log(LOG_LEVELS.DEBUG, ...arguments) }
trace() { this._log(LOG_LEVELS.TRACE, ...arguments) }
get contextName() {
return this._contextName
}
get logLevel() {
if (this._logLevel) {
return this._logLevel
}
if (this._parentLogger) {
return this._parentLogger.logLevel
}
return DEFAULT_LOG_LEVEL
}
set logLevel(logLevel) {
this._logLevel = logLevel
}
set logLevelByName(logLevelName) {
_checkNotNil(logLevelName, () => "illegal argument: logLevelName")
_checkNotNil(LOG_LEVELS[logLevelName], () => "illegal argument: logLevelName")
this._logLevel = LOG_LEVELS[logLevelName]
}
_log(logLevel, ...messages) {
_checkNotNil(logLevel, () => "illegal argument: logLevel")
_checkNotNil(logLevel.handler, () => "illegal argument: logLevel.handler")
if (this.logLevel.level >= logLevel.level) {
const handler = logLevel.handler
const timestamp = new Date()
const contextName = this._contextName
const levelName = logLevel.name
const logMessage = oo.buildLog(
contextName, levelName, timestamp, ...messages
)
handler(...logMessage)
}
}
}
const LOG_LEVELS = {
ERROR: { level: 1, name: "ERROR", handler: console.error },
WARN: { level: 2, name: "WARN", handler: console.warn },
INFO: { level: 3, name: "INFO", handler: console.log },
DEBUG: { level: 4, name: "DEBUG", handler: console.log },
TRACE: { level: 5, name: "TRACE", handler: console.log },
NONE: { level: 0 },
ALL: { level: 99 }
}
const DEFAULT_LOG_LEVEL = LOG_LEVELS.INFO
const ROOT_CONTEXT_NAME = "ROOT"
const ROOT_LOGGER = new oo.Logger(ROOT_CONTEXT_NAME, null)
const LOGGER_CONTEXTS = {}
function _check(condition, lazyMessage = null) {
if (!condition) {
if (lazyMessage) {
throw new Error(lazyMessage())
}
throw new Error("illegal argument")
}
}
function _checkNotNil(value, lazyMessage = null) {
if (_isNil(value)) {
if (lazyMessage) {
throw new Error(lazyMessage())
}
throw new Error("illegal argument")
}
}
function _isNil(value) {
return value === null && value === undefined
}
}())
</script>