aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/utilities/logger.ts
blob: c21bd76f874940fff1ba16a68f14282b4ac7e6e4 (plain) (blame)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { browser, dev } from "$app/environment";
import { env } from '$env/dynamic/private';
import { StorageKeys } from "$configuration";
import pino, { type Logger, type LoggerOptions } from "pino";
import { createStream } from "pino-seq";
import type { SeqConfig } from "pino-seq";

function get_pino_logger(): Logger {
    const config = {
        name: "greatoffice-app",
        level: LogLevel.current().as_string(),
        customLevels: {
            "INFO": LogLevel.INFO,
            "WARNING": LogLevel.WARNING,
            "ERROR": LogLevel.ERROR,
            "DEBUG": LogLevel.DEBUG,
            "SILENT": LogLevel.SILENT,
        }
    } as LoggerOptions;

    const seq = {
        config: {
            apiKey: browser ? env.SEQ_API_KEY : "",
            serverUrl: browser ? env.SEQ_SERVER_URL : ""
        } as SeqConfig,
        streams: [{
            level: LogLevel.to_string(LogLevel.DEBUG),
        }],
        enabled: () => (
            !browser
            && !dev
            && seq.config.apiKey.length > 0
            && seq.config.serverUrl.length > 0
        )
    };

    return seq.enabled() ? pino(config, createStream(seq.config)) : pino(config);
}

type LogLevelString = "DEBUG" | "INFO" | "WARNING" | "ERROR" | "SILENT";

export const LogLevel = {
    DEBUG: 0,
    INFO: 1,
    WARNING: 2,
    ERROR: 3,
    SILENT: 4,
    current(): { as_string: Function, as_number: Function } {
        const logLevelString = (browser ? window.sessionStorage.getItem(StorageKeys.logLevel) : env.LOG_LEVEL) as LogLevelString;
        return {
            as_number(): number {
                return LogLevel.to_number_or_default(logLevelString, LogLevel.INFO)
            },
            as_string(): LogLevelString {
                return logLevelString.length > 3 ? logLevelString : LogLevel.to_string(LogLevel.INFO);
            }
        }
    },
    to_string(levelInt: number): LogLevelString {
        switch (levelInt) {
            case 0:
                return "DEBUG";
            case 1:
                return "INFO";
            case 2:
                return "WARNING";
            case 3:
                return "ERROR";
            case 4:
                return "SILENT";
            default:
                throw new Error("Unknown LogLevel number " + levelInt);
        }
    },
    to_number_or_default(levelString?: string | null, defaultValue?: number): number {
        if (!levelString && defaultValue) return defaultValue;
        else if (!levelString && !defaultValue) throw new Error("levelString was empty, and no default value was specified");
        switch (levelString?.toUpperCase()) {
            case "DEBUG":
                return 0;
            case "INFO":
                return 1;
            case "WARNING":
                return 2;
            case "ERROR":
                return 3;
            case "SILENT":
                return 4;
            default:
                if (!defaultValue) throw new Error("Unknown LogLevel string " + levelString + ", and no defaultValue");
                else return defaultValue;
        }
    },
};

export function log_warning(message: string, ...additional: any[]): void {
    if (LogLevel.current().as_number() <= LogLevel.WARNING) {
        get_pino_logger().warn(message, additional);
    }
}

export function log_debug(message: string, ...additional: any[]): void {
    if (LogLevel.current().as_number() <= LogLevel.DEBUG) {
        get_pino_logger().debug(message, additional);
    }
}

export function log_info(message: string, ...additional: any[]): void {
    if (LogLevel.current().as_number() <= LogLevel.INFO) {
        get_pino_logger().info(message, additional);
    }
}

export function log_error(message: any, ...additional: any[]): void {
    if (LogLevel.current().as_number() <= LogLevel.ERROR) {
        get_pino_logger().error(message, additional);
    }
}