Skip to content

Commit df9563b

Browse files
authored
Merge pull request #192 from layerx-labs/dev
BEPRO 2.22
2 parents 611636b + fa20493 commit df9563b

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed

elastic-apm-node.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require(`dotenv`).config();
22

33
module.exports = {
4-
serverUrl: process.env.ELASTIC_APM_SERVER_URL, // E.g. https://my-deployment-name.apm.us-west2.gcp.elastic-cloud.com
5-
secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
6-
serviceName: process.env.ELASTIC_APM_SERVICE_NAME,
7-
active: process.env.ELASTIC_APM_ACTIVE === "true",
8-
logLevel: process.env.ELASTIC_APM_LOG_LEVEL
4+
serverUrl: process.env.NEXT_ELASTIC_APM_SERVER_URL, // E.g. https://my-deployment-name.apm.us-west2.gcp.elastic-cloud.com
5+
secretToken: process.env.NEXT_ELASTIC_APM_SECRET_TOKEN,
6+
serviceName: process.env.NEXT_ELASTIC_APM_SERVICE_NAME,
7+
active: process.env.NEXT_ELASTIC_APM_ACTIVE === "true",
8+
logLevel: process.env.NEXT_ELASTIC_APM_LOG_LEVEL
99
}

src/utils/elastic-logger.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {Client} from "@elastic/elasticsearch";
2+
import {LoggerPlugin} from "@taikai/scribal/dist/lib/types";
3+
4+
const {
5+
NEXT_ELASTIC_SEARCH_URL: node,
6+
NEXT_ELASTIC_SEARCH_USERNAME: username,
7+
NEXT_ELASTIC_SEARCH_PASSWORD: password,
8+
NEXT_LOG_APP_NAME: index = "webnetwork-events-logs"
9+
} = process.env;
10+
11+
export const elasticLoggerMaker = (): LoggerPlugin => ({
12+
log(level, contents) {
13+
if (!node || !username || !password) {
14+
console.debug(`\n\tTrying to use elastic-search indexer but missing env-vars\n`);
15+
return;
16+
}
17+
18+
/* this is needed because Scribal makes magic */
19+
const _params = contents?.[1]?.[0];
20+
const params =
21+
(typeof _params === "string" || typeof _params === "number")
22+
? {value: _params}
23+
: Array.isArray(_params)
24+
? {value_array: _params}
25+
: _params;
26+
27+
new Client({node, auth: {username, password}})
28+
.index({
29+
index: `bepro-processor-logs-${index}`,
30+
document: {
31+
level,
32+
message: contents[0],
33+
params,
34+
createdAt: new Date().toISOString(),
35+
}
36+
})
37+
.catch(e => {
38+
console.log(`Failed to log on elastic`, e);
39+
});
40+
41+
}
42+
})

src/utils/logger-handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Scribal from "./scribal";
22

33
/* eslint-disable */
4-
export const info = (message: string, ...rest) => Scribal.i(message, ...rest);
5-
export const error = (message: string, ...rest) => Scribal.e(message, ...rest);
6-
export const log = (message: string, ...rest) => Scribal.d(message, ...rest);
7-
export const warn = (message: string, ...rest) => Scribal.w(message, ...rest);
8-
export const debug = (message: string, ...rest) => Scribal.d(message, ...rest);
9-
export const trace = (message: string, ...rest) => Scribal.d(message, ...rest);
4+
export const info = (message: string, ...rest) => Scribal.i([message, rest]);
5+
export const error = (message: string, ...rest) => Scribal.e([message, rest]);
6+
export const log = (message: string, ...rest) => Scribal.d([message, rest]);
7+
export const warn = (message: string, ...rest) => Scribal.w([message, rest]);
8+
export const debug = (message: string, ...rest) => Scribal.d([message, rest]);
9+
export const trace = (message: string, ...rest) => Scribal.d([message, rest]);
1010

1111
export default {error, info, warn, log, debug, trace}

src/utils/scribal.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import Scribal from "@taikai/scribal";
2+
import {elasticLoggerMaker} from "src/utils/elastic-logger";
23

34
const ScribalConfig = {
45
logService: {
56
console: {
6-
silent: process.env.LOG_TO_CONSOLE === 'false',
7-
logLevel: (process.env.LOG_LEVEL || 'debug') as any,
7+
silent: process.env.NEXT_LOG_TO_CONSOLE === 'false',
8+
logLevel: (process.env.NEXT_LOG_LEVEL || 'debug') as any,
89
},
910
elastic: {
10-
silent: process.env.LOG_TO_ELASTIC === 'false',
11-
level: (process.env.LOG_LEVEL || 'debug') as any,
11+
silent: process.env.NEXT_LOG_TO_ELASTIC === 'false',
12+
level: (process.env.NEXT_LOG_LEVEL || 'debug') as any,
1213
},
1314
file: {
14-
silent: process.env.LOG_TO_FILE === 'false',
15-
logLevel: (process.env.LOG_LEVEL || 'debug') as any,
16-
logFileDir: process.env.LOG_FILE_DIR || 'logs',
17-
logDailyRotation: process.env.DAILY_ROTATION_FILE === 'true',
15+
silent: process.env.NEXT_LOG_TO_FILE === 'false',
16+
logLevel: (process.env.NEXT_LOG_LEVEL || 'debug') as any,
17+
logFileDir: process.env.NEXT_LOG_FILE_DIR || 'logs',
18+
logDailyRotation: process.env.NEXT_DAILY_ROTATION_FILE === 'true',
1819
logDailyRotationOptions: {
19-
maxSize: process.env.DAILY_ROTATION_FILE_MAX_SIZE || '20m',
20-
datePattern: process.env.DAILY_ROTATION_FILE_DATE_PATTERN || 'YYYY-MM-DD',
20+
maxSize: process.env.NEXT_DAILY_ROTATION_FILE_MAX_SIZE || '20m',
21+
datePattern: process.env.NEXT_DAILY_ROTATION_FILE_DATE_PATTERN || 'YYYY-MM-DD',
2122
},
2223
},
2324
}
2425
}
2526

2627
export default (() => {
27-
const appName = process.env.LOG_APP_NAME || `bepro-events`;
28+
const appName = process.env.LOG_APP_NAME || `webnetwork-events-logs`;
2829
const hostname = process.env.LOG_HOST_NAME || `localhost`;
2930

3031
const scribal = new Scribal([]);
3132
scribal.init({appName, hostname, version: '*', ...ScribalConfig.logService});
33+
scribal.addLogger(elasticLoggerMaker, ScribalConfig.logService.elastic);
3234

3335
return scribal;
3436
})()

0 commit comments

Comments
 (0)