Skip to content

Commit 646f916

Browse files
committed
- fix Logger
- add e2e tests for Logging Service
1 parent 609a2e2 commit 646f916

File tree

2 files changed

+104
-64
lines changed

2 files changed

+104
-64
lines changed

src/logging/collector.js

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,102 @@ import Logger from './logger'
77

88
let lastFlushListeners
99

10+
function flush(asyncHandler) {
11+
if (LoggingCollector.pool.length) {
12+
if (LoggingCollector.flushInterval) {
13+
clearTimeout(LoggingCollector.flushInterval)
14+
}
15+
16+
let listeners
17+
18+
const cb = method => function() {
19+
listeners.forEach(callbacks => {
20+
callbacks[method].apply(null, arguments)
21+
})
22+
23+
if (listeners === lastFlushListeners) {
24+
lastFlushListeners = null
25+
}
26+
}
27+
28+
if (asyncHandler) {
29+
listeners = lastFlushListeners = lastFlushListeners ? lastFlushListeners.splice(0) : []
30+
listeners.push(asyncHandler)
31+
}
32+
33+
console.log('LoggingCollector.pool', LoggingCollector.pool)
34+
35+
Request.put({
36+
isAsync : !!asyncHandler,
37+
asyncHandler: asyncHandler && new Async(cb('success'), cb('fault')),
38+
url : Urls.logging(),
39+
data : LoggingCollector.pool
40+
})
41+
42+
LoggingCollector.pool = []
43+
44+
} else if (asyncHandler) {
45+
if (lastFlushListeners) {
46+
lastFlushListeners.push(asyncHandler)
47+
} else {
48+
setTimeout(asyncHandler.success, 0)
49+
}
50+
}
51+
}
52+
1053
const LoggingCollector = {
11-
loggers : {},
12-
pool : [],
13-
numOfMessages: 10,
14-
timeFrequency: 1,
1554

1655
reset() {
17-
this.loggers = {}
18-
this.pool = []
19-
this.numOfMessages = 10
20-
this.timeFrequency = 1
56+
LoggingCollector.loggers = {}
57+
LoggingCollector.pool = []
58+
LoggingCollector.numOfMessages = 10
59+
LoggingCollector.timeFrequency = 1
2160
},
2261

2362
getLogger(loggerName) {
2463
if (!Utils.isString(loggerName)) {
2564
throw new Error("Invalid 'loggerName' value. LoggerName must be a string value")
2665
}
2766

28-
return this.loggers[loggerName] = this.loggers[loggerName] || new Logger(loggerName)
67+
return LoggingCollector.loggers[loggerName] = LoggingCollector.loggers[loggerName] || new Logger(loggerName)
2968
},
3069

3170
push(logger, logLevel, message, exception) {
3271
const messageObj = {
3372
logger,
34-
logLevel,
3573
message,
3674
exception,
75+
'log-level': logLevel,
3776
timestamp: Date.now()
3877
}
3978

40-
this.pool.push(messageObj)
79+
LoggingCollector.pool.push(messageObj)
4180

42-
this.checkMessagesLen()
81+
LoggingCollector.checkMessagesLen()
4382
},
4483

45-
checkMessagesLen: function() {
46-
if (this.pool.length >= this.numOfMessages) {
47-
this.sendRequest()
84+
checkMessagesLen() {
85+
if (LoggingCollector.pool.length >= LoggingCollector.numOfMessages) {
86+
LoggingCollector.sendRequest()
4887
}
4988
},
5089

51-
flush : Utils.promisified('_flush'),
52-
flushSync: Utils.synchronized('_flush'),
90+
flush : Utils.promisified(flush),
91+
flushSync: Utils.synchronized(flush),
5392

54-
_flush: function(asyncHandler) {
55-
if (this.pool.length) {
56-
if (this.flushInterval) {
57-
clearTimeout(this.flushInterval)
58-
}
59-
60-
let listeners
61-
62-
const cb = method => function() {
63-
listeners.forEach(callbacks => {
64-
callbacks[method].apply(null, arguments)
65-
})
66-
67-
if (listeners === lastFlushListeners) {
68-
lastFlushListeners = null
69-
}
70-
}
71-
72-
if (asyncHandler) {
73-
listeners = lastFlushListeners = lastFlushListeners ? lastFlushListeners.splice(0) : []
74-
listeners.push(asyncHandler)
75-
}
76-
77-
Request.put({
78-
isAsync : !!asyncHandler,
79-
asyncHandler: asyncHandler && new Async(cb('success'), cb('fault')),
80-
url : Urls.logging(),
81-
data : this.pool
82-
})
83-
84-
this.pool = []
85-
86-
} else if (asyncHandler) {
87-
if (lastFlushListeners) {
88-
lastFlushListeners.push(asyncHandler)
89-
} else {
90-
setTimeout(asyncHandler.success, 0)
91-
}
92-
}
93-
},
94-
95-
sendRequest: function() {
96-
this.flushInterval = setTimeout(() => this.flush(), this.timeFrequency * 1000)
93+
sendRequest() {
94+
LoggingCollector.flushInterval = setTimeout(() => LoggingCollector.flush(), LoggingCollector.timeFrequency * 1000)
9795
},
9896

99-
setLogReportingPolicy: function(numOfMessages, timeFrequency) {
100-
this.numOfMessages = numOfMessages
101-
this.timeFrequency = timeFrequency
97+
setLogReportingPolicy(numOfMessages, timeFrequency) {
98+
LoggingCollector.numOfMessages = numOfMessages
99+
LoggingCollector.timeFrequency = timeFrequency
102100

103101
//TODO: check when set new timeFrequency
104-
this.checkMessagesLen()
102+
LoggingCollector.checkMessagesLen()
105103
}
106104
}
107105

106+
LoggingCollector.reset()
107+
108108
export default LoggingCollector

test/e2e/specs/logging.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import '../helpers/global'
2+
import sandbox from '../helpers/sandbox'
3+
4+
const Backendless = sandbox.Backendless
5+
6+
describe('Backendless.Logging', function() {
7+
8+
sandbox.forSuite()
9+
10+
it('has name', function() {
11+
return Promise.resolve()
12+
.then(() => Backendless.Logging.getLogger('MyLogger'))
13+
.then(logger => expect(logger.name).to.be.equal('MyLogger'))
14+
})
15+
16+
it('equals loggers', function() {
17+
const logger1 = Backendless.Logging.getLogger('MyLogger')
18+
const logger2 = Backendless.Logging.getLogger('MyLogger')
19+
20+
return expect(logger1).to.be.equal(logger2)
21+
})
22+
23+
it('send messages pool', function() {
24+
return Promise.resolve()
25+
.then(() => Backendless.Logging.getLogger('MyLogger'))
26+
.then(logger => {
27+
logger.debug('I\'m debug message')
28+
logger.info('I\'m info message')
29+
logger.warn('I\'m warn message', new Error('I\'m warn exception').stack)
30+
logger.error('I\'m error message', new Error('I\'m error exception').stack)
31+
logger.fatal('I\'m fatal message', new Error('I\'m fatal exception').stack)
32+
logger.trace('I\'m debug message')
33+
logger.debug('I\'m debug message')
34+
logger.debug('I\'m debug message')
35+
36+
return Backendless.Logging.flush()
37+
})
38+
})
39+
40+
})

0 commit comments

Comments
 (0)