-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.js
52 lines (43 loc) · 1.35 KB
/
example.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
#!/usr/bin/env node
var cluster = require("cluster");
var winston = require("winston");
var WinstonCluster = require("./lib/winston-cluster").WinstonCluster;
var logLevel = "info";
if (cluster.isMaster) {
// Create parent thread logger
// Other transports (ie. file writing) should be bound to this
var logger = new winston.Logger({
transports: [
new winston.transports.Console({
level: logLevel,
}),
],
});
// Start child threads
var cpuCount = require("os").cpus().length;
for (var i = 0; i < cpuCount; i++) {
cluster.fork();
}
// Bind event listeners to child threads using the local logger instance
WinstonCluster.bindListeners(logger);
// Logging works as normal in the main thread
logger.info("Started server!");
// Server thread logic here
// ...
} else {
// Create a new logger instance for the child thread using the cluster transport to
// send data back to the parent thread.
// Note that log level must also be set here to avoid filtering prior to sending logs back
var logger = new winston.Logger({
transports: [
new winston.transports.Cluster({
level: logLevel,
}),
],
});
// Logging in this instance now passed via IPC back to the parent
// (and tagged with the worker thread ID)
logger.info("Test Message!");
// Client thread logic here
// ...
}