summaryrefslogtreecommitdiff
path: root/shared/logger/src/errorkit/errorkit-logger.ts
blob: 1290c416cc44054bd519a5d463b1a927b749e642 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { ErrorHub, ValueOf } from './types';
import type { LoggerFactory, Logger } from '../types';

/**
 * Determines the level of logs to send to sentry.
 *
 */
export const ERROR_REPORT_LEVEL = {
    error: 'error',
    error_warn: 'error_warn',
} as const;

type ReportLevel = ValueOf<typeof ERROR_REPORT_LEVEL>;

export class ErrorKitLoggerFactory implements LoggerFactory {
    private readonly errorKit: ErrorHub;
    private readonly reportLevel: ReportLevel;
    constructor(errorKit: ErrorHub, reportLevel?: ReportLevel) {
        this.errorKit = errorKit;
        this.reportLevel = reportLevel ?? ERROR_REPORT_LEVEL.error;
    }
    loggerFor(name: string): Logger {
        return new ErrorKitLogger(name, this.errorKit, this.reportLevel);
    }
}

interface HasToString {
    toString(): string;
}

export class ErrorKitLogger implements Logger {
    private readonly name: string;
    private readonly errorKit: ErrorHub;
    private readonly reportLevel: ReportLevel;
    constructor(name: string, errorKit: ErrorHub, reportLevel: ReportLevel) {
        this.name = name;
        this.errorKit = errorKit;
        this.reportLevel = reportLevel;
    }

    private stringifyConsoleArgs(...args: unknown[]): string {
        return args.reduce((acc: string, val: unknown) => {
            let tempVal: HasToString;
            switch (true) {
                case val instanceof Error: {
                    tempVal = (val as unknown as InstanceType<typeof Error>)
                        .message;
                    break;
                }
                case typeof val === 'object': {
                    try {
                        tempVal = JSON.stringify(val);
                    } catch (e) {
                        tempVal = `failed to stringify ${val}`;
                    }
                    break;
                }
                case typeof val === 'undefined' || val === null: {
                    tempVal = `${val}`;
                    break;
                }
                default: {
                    tempVal = val as HasToString;
                }
            }

            return `${acc} ${tempVal.toString()}`;
        }, `[${this.name}]`) as string;
    }

    debug(..._args: unknown[]): string {
        return '';
    }
    info(..._args: unknown[]): string {
        return '';
    }
    warn(...args: unknown[]): string {
        if (this.reportLevel === ERROR_REPORT_LEVEL.error_warn) {
            this.errorKit.captureMessage(this.stringifyConsoleArgs(...args));
        }
        return '';
    }
    error(...args: unknown[]): string {
        const errors = args.filter((item) => item instanceof Error) as Error[];
        const message = this.stringifyConsoleArgs(...args);

        const error = errors.length === 0 ? new Error(message) : errors[0];
        error.message = message;

        this.errorKit.captureException(error);
        return '';
    }
}