Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit c5d8146

Browse files
authored
Replace signale with pino (#346)
1 parent 40a7d9a commit c5d8146

10 files changed

Lines changed: 648 additions & 92 deletions

File tree

config/default.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ module.exports = {
99
maxPollingInterval: parseInt(process.env.MAX_POLLING_INTERVAL, 10),
1010
minConfirmations: 12,
1111
minPollingInterval: parseInt(process.env.MIN_POLLING_INTERVAL, 10),
12+
pino: {
13+
elasticsearch: {
14+
batchSize: 200,
15+
index: 'logs_event_extractor',
16+
url: process.env.PINO_ELASTIC_SEARCH_URL || null,
17+
},
18+
},
1219
v1: {
1320
genesisBlock: 4145578,
1421
},

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@0x-event-extractor/app",
33
"main": "src/index.js",
44
"scripts": {
5+
"dev": "nodemon pino . | pino-pretty -t",
56
"lint": "eslint .",
67
"start": "node src",
78
"test": "jest",
@@ -22,11 +23,13 @@
2223
"husky": "3.0.8",
2324
"jest": "25.1.0",
2425
"lint-staged": "9.4.2",
26+
"nodemon": "2.0.2",
27+
"pino-pretty": "3.6.1",
2528
"prettier": "1.19.1"
2629
},
2730
"engines": {
2831
"node": "10.17.0",
29-
"yarn": "1.21.1"
32+
"yarn": "1.22.4"
3033
},
3134
"private": true,
3235
"workspaces": [

packages/core/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"delay": "^4.3.0",
1313
"lodash": "^4.17.15",
1414
"mongoose": "^5.7.3",
15-
"promise-poller": "^1.9.1",
16-
"signale": "^1.4.0"
15+
"pino": "6.0.0",
16+
"pino-elasticsearch": "4.4.0",
17+
"promise-poller": "^1.9.1"
1718
}
1819
}

packages/core/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ const db = require('./util/db');
77
const errorLogger = require('./util/error-logger');
88
const getJobs = require('./jobs');
99
const jobRunner = require('./util/job-runner');
10+
const logging = require('./util/logging');
11+
const model = require('./model');
1012
const web3 = require('../../shared/src/web3');
1113

1214
const configure = async initialConfig => {
1315
config.init(initialConfig);
16+
logging.init(config.get('pino'));
1417
errorLogger.configure({
1518
bugsnagToken: config.get('bugsnag.token'),
1619
});
1720
await db.connect(config.get('database.connectionString'));
21+
await model.init();
1822
web3.configure({ endpoint: config.get('web3.endpoint') });
1923
extractorV1.configure({ networkId: config.get('web3.networkId') });
2024
extractorV2.configure({ networkId: config.get('web3.networkId') });

packages/core/src/jobs/extract-events.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
const extractorV1 = require('@0x-event-extractor/extractor-v1');
22
const extractorV2 = require('@0x-event-extractor/extractor-v2');
33
const extractorV3 = require('@0x-event-extractor/extractor-v3');
4-
const signale = require('signale');
54

5+
const { getLogger } = require('../util/logging');
66
const BlockRange = require('../model/block-range');
77
const Event = require('../model/event');
88
const getCurrentBlock = require('../ethereum/get-current-block');
99
const getNextBlockRange = require('../events/get-next-block-range');
10-
const withTimer = require('../util/with-timer');
1110
const withTransaction = require('../util/with-transaction');
1211

1312
const extractEventsForProtocol = async (protocolVersion, extractorConfig) => {
1413
// Scope all logging for the job to the specified protocol version
15-
const logger = signale.scope(`extract events v${protocolVersion}`);
14+
const logger = getLogger(`extract events v${protocolVersion}`);
1615

1716
// Determine which blocks we should fetch log entries from
1817
const { currentBlock, fetchLogEntries, getEventData } = extractorConfig;
18+
19+
logger.info(`current block is ${currentBlock}`);
20+
1921
const nextBlockRange = await getNextBlockRange({
2022
currentBlock,
2123
protocolVersion,
2224
});
2325

24-
logger.info(`current block is ${currentBlock}`);
25-
2626
if (nextBlockRange === null) {
2727
logger.info('no more blocks to process');
2828
return;
2929
}
3030

3131
const { fromBlock, toBlock } = nextBlockRange;
3232

33-
logger.info(`next block range is ${fromBlock} to ${toBlock}`);
33+
logger.info(`fetching events from block range ${fromBlock} to ${toBlock}`);
3434

35-
const logEntries = await withTimer(logger, `fetch entries`, () =>
36-
fetchLogEntries(fromBlock, toBlock),
37-
);
35+
const logEntries = await fetchLogEntries(fromBlock, toBlock);
3836

3937
if (logEntries.length === 0) {
40-
logger.info(`no entries were found`);
38+
logger.info(`no events found in block range ${fromBlock} to ${toBlock}`);
4139
} else {
42-
logger.info(`${logEntries.length} entries were found`);
40+
logger.info(
41+
`${logEntries.length} events found in block range ${fromBlock} to ${toBlock}`,
42+
);
4343
}
4444

4545
// Persistence operations are wrapped in a transaction to ensure consistency
@@ -55,34 +55,34 @@ const extractEventsForProtocol = async (protocolVersion, extractorConfig) => {
5555
type: logEntry.event,
5656
}));
5757

58-
await withTimer(logger, `persist ${events.length} events`, async () => {
59-
await Event.insertMany(events, { session });
60-
});
58+
await Event.insertMany(events, { session });
6159
}
6260

6361
// Log details of the queried block range so that we know where
6462
// to start from in the next iteration.
65-
await withTimer(logger, 'log queried block range', async () => {
66-
await BlockRange.findOneAndUpdate(
67-
{ fromBlock, protocolVersion, toBlock },
68-
{
69-
$set: {
70-
date: new Date(),
71-
events: logEntries.length,
72-
fromBlock,
73-
protocolVersion,
74-
toBlock,
75-
},
76-
},
77-
{
78-
upsert: true,
79-
new: true,
80-
runValidators: true,
81-
session,
63+
await BlockRange.findOneAndUpdate(
64+
{ fromBlock, protocolVersion, toBlock },
65+
{
66+
$set: {
67+
date: new Date(),
68+
events: logEntries.length,
69+
fromBlock,
70+
protocolVersion,
71+
toBlock,
8272
},
83-
);
84-
});
73+
},
74+
{
75+
upsert: true,
76+
new: true,
77+
runValidators: true,
78+
session,
79+
},
80+
);
8581
});
82+
83+
if (logEntries.length > 0) {
84+
logger.info(`persisted ${logEntries.length} events to database`);
85+
}
8686
};
8787

8888
const extractEvents = async () => {

packages/core/src/model/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const BlockRange = require('./block-range');
2+
const Event = require('./event');
3+
4+
const init = async () => {
5+
BlockRange.createCollection();
6+
Event.createCollection();
7+
};
8+
9+
module.exports = { BlockRange, Event, init };

packages/core/src/util/db.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
const mongoose = require('mongoose');
2-
const signale = require('signale');
32

43
const { logError } = require('./error-logger');
5-
6-
const logger = signale.scope('mongodb');
4+
const { getLogger } = require('./logging');
75

86
mongoose.Promise = global.Promise;
97

108
module.exports = {
119
connect: async connectionString => {
10+
const logger = getLogger();
11+
1212
mongoose.connection.on('connecting', () => {
1313
logger.info('connecting to database');
1414
});
1515

1616
mongoose.connection.on('connected', () => {
17-
logger.success('database connection established');
17+
logger.info('database connection established');
1818
});
1919

2020
mongoose.connection.on('error', err => {

packages/core/src/util/error-logger.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const _ = require('lodash');
22
const bugsnag = require('@bugsnag/js');
3-
const signale = require('signale');
4-
5-
const logger = signale.scope('application');
63

74
let bugsnagClient;
85

@@ -11,7 +8,7 @@ const logError = (error, metaData) => {
118
bugsnagClient.notify(error, { metaData });
129
}
1310

14-
logger.error(error);
11+
console.error(error); // eslint-disable-line no-console
1512
};
1613

1714
const configure = ({ appVersion, bugsnagToken }) => {
@@ -23,8 +20,8 @@ const configure = ({ appVersion, bugsnagToken }) => {
2320
appVersion,
2421
});
2522
} else {
26-
process.on('uncaughtException', logger.error);
27-
process.on('unhandledRejection', logger.error);
23+
process.on('uncaughtException', console.error); // eslint-disable-line no-console
24+
process.on('unhandledRejection', console.error); // eslint-disable-line no-console
2825
}
2926
};
3027

packages/core/src/util/logging.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const _ = require('lodash');
2+
const os = require('os');
3+
const pino = require('pino');
4+
const pinoElastic = require('pino-elasticsearch');
5+
6+
let logger;
7+
8+
const createIndexFormatter = prefix => logTime => {
9+
const date = new Date(logTime);
10+
const year = date.getUTCFullYear();
11+
const month = _.padStart(date.getUTCMonth() + 1, 2, 0);
12+
const day = _.padStart(date.getUTCDate(), 2, 0);
13+
14+
// Log indexes roll over every day
15+
return `${prefix}_${year}_${month}_${day}`;
16+
};
17+
18+
const init = config => {
19+
const streamToElasticsearch =
20+
config.elasticsearch.url === null || config.elasticsearch.url.length === 0
21+
? undefined
22+
: pinoElastic({
23+
'bulk-size': config.elasticsearch.batchSize,
24+
consistency: 'one',
25+
ecs: true,
26+
index: createIndexFormatter(config.elasticsearch.index),
27+
node: config.elasticsearch.url,
28+
type: 'log',
29+
});
30+
31+
logger = pino(
32+
{
33+
base: { group: 'application', pid: process.pid, hostname: os.hostname() },
34+
level: 'info',
35+
},
36+
streamToElasticsearch,
37+
);
38+
};
39+
40+
const getLogger = logGroup => {
41+
if (logGroup !== undefined) {
42+
return logger.child({ group: logGroup });
43+
}
44+
45+
return logger;
46+
};
47+
48+
module.exports = { getLogger, init };

0 commit comments

Comments
 (0)