-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom-logger-bunyan.js
60 lines (49 loc) · 1.3 KB
/
custom-logger-bunyan.js
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
const actors = require('comedy');
const bunyan = require('bunyan');
/**
* This example demonstrates the use of custom logger that replaces
* default Comedy logger. Here we use Bunyan for logging.
*/
/**
* Adapts Bunyan logger to Comedy actor system. There are
* 4 required methods that should be implemented in this adapter:
* error(), warn(), info(), and debug().
*/
class BunyanLoggerAdapter {
constructor() {
this.log = bunyan.createLogger({ name: 'comedy-examples' });
}
error(...msg) {
this.log.error(...msg);
}
warn(...msg) {
this.log.warn(...msg);
}
info(...msg) {
this.log.info(...msg);
}
debug(...msg) {
this.log.debug(...msg);
}
}
/**
* A test actor that uses logging.
*/
class TestActor {
initialize(selfActor) {
this.log = selfActor.getLog();
this.log.info(`${selfActor.getName()} initialized!`);
}
test(msg) {
this.log.info('Received test message:', msg);
}
}
// We configure actor system to use our custom Bunyan-based logger.
// When actor system is created, we should already see some Bunyan messages
// in console.
let system = actors({
root: TestActor,
logger: BunyanLoggerAdapter
});
// Send a message to test actor. Another message should appear in log.
system.rootActor().then(actor => actor.sendAndReceive('test', 'Hello!'));