-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
logger.ts
52 lines (46 loc) · 1.57 KB
/
logger.ts
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
import {ApiError} from "@lodestar/api";
import {LogData, Logger, isErrorAborted} from "@lodestar/utils";
import {IClock} from "./clock.js";
export type LoggerVc = Logger & {
isSyncing(e: Error): void;
};
export function getLoggerVc(logger: Logger, clock: IClock): LoggerVc {
let hasLogged = false;
clock.runEverySlot(async () => {
if (hasLogged) hasLogged = false;
});
return {
error(message: string, context?: LogData, e?: Error) {
if (e) {
// Returns true if it's an network error with code 503 = Node is syncing
// https://github.com/ethereum/beacon-APIs/blob/e68a954e1b6f6eb5421abf4532c171ce301c6b2e/types/http.yaml#L62
if (e instanceof ApiError && e.status === 503) {
this.isSyncing(e);
}
// Only log if arg `e` is not an instance of `ErrorAborted`
else if (!isErrorAborted(e)) {
logger.error(message, context, e);
}
} else {
logger.error(message, context, e);
}
},
// error: logger.error.bind(logger),
warn: logger.warn.bind(logger),
info: logger.info.bind(logger),
verbose: logger.verbose.bind(logger),
debug: logger.debug.bind(logger),
trace: logger.trace.bind(logger),
/**
* Throttle "node is syncing" errors to not pollute the console too much.
* Logs once per slot at most.
*/
isSyncing(e: Error) {
if (!hasLogged) {
hasLogged = true;
// Log the full error message, in case the server returns 503 for some unknown reason
logger.warn(`Node is syncing - ${e.message}`);
}
},
};
}