addons/isl-server/src/logger.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318export type Level = 'log' | 'info' | 'warn' | 'error';
b69ab319
b69ab3110const tzOptions: Intl.DateTimeFormatOptions & {
b69ab3111 // This is an ES2021 feature which is not properly reflected in the types but is available. TODO: update target to ES2021 to fix this.
b69ab3112 // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#fractionalseconddigits
b69ab3113 fractionalSecondDigits: 1 | 2 | 3;
b69ab3114} = {
b69ab3115 timeZoneName: 'short',
b69ab3116 // Show millisecond resolution for extra timing information in the logs
b69ab3117 fractionalSecondDigits: 3,
b69ab3118 // After setting fractionalSecondDigits, it requires setting all fields we want printed to 'numeric'.
b69ab3119 year: 'numeric',
b69ab3120 month: 'numeric',
b69ab3121 day: 'numeric',
b69ab3122 hour: 'numeric',
b69ab3123 minute: 'numeric',
b69ab3124 second: 'numeric',
b69ab3125};
b69ab3126
b69ab3127/** Standardized logging interface for the server.
b69ab3128 * Format: [YYY-MM-DD HH:MM:SS,MILLISECONDS TIMEZONE] [LEVEL] your message here
b69ab3129 * Example: [2025-01-14 17:54:55,092 GMT−8] [INFO] Setup analytics
b69ab3130 */
b69ab3131export abstract class Logger {
b69ab3132 abstract write(level: Level, timeStr: string, ...args: Parameters<typeof console.log>): void;
b69ab3133
b69ab3134 private writeLog(level: Level, ...args: Parameters<typeof console.info>): void {
b69ab3135 const timeStr = `[${new Date().toLocaleString('sv', tzOptions)}]`;
b69ab3136 this.write(level, timeStr, ...args);
b69ab3137 }
b69ab3138
b69ab3139 /**
b69ab3140 * @deprecated use .info instead
b69ab3141 * TODO: we should just use info everywhere, I don't know the distinction between log and info,
b69ab3142 * this was just for compatibility with console.log which isn't particularly important.
b69ab3143 */
b69ab3144 log(...args: Parameters<typeof console.info>): void {
b69ab3145 this.writeLog('log', ...args);
b69ab3146 }
b69ab3147
b69ab3148 info(...args: Parameters<typeof console.info>): void {
b69ab3149 this.writeLog('info', ...args);
b69ab3150 }
b69ab3151
b69ab3152 warn(...args: Parameters<typeof console.info>): void {
b69ab3153 this.writeLog('warn', ...args);
b69ab3154 }
b69ab3155
b69ab3156 error(...args: Parameters<typeof console.info>): void {
b69ab3157 this.writeLog('error', ...args);
b69ab3158 }
b69ab3159
b69ab3160 /** Get all previously logged contents, usually for filing a bug report. */
b69ab3161 getLogFileContents?(): Promise<string>;
b69ab3162
b69ab3163 levelToString(level: Level): string {
b69ab3164 switch (level) {
b69ab3165 case 'log':
b69ab3166 return ' [LOG]';
b69ab3167 case 'info':
b69ab3168 return ' [INFO]';
b69ab3169 case 'warn':
b69ab3170 return ' [WARN]';
b69ab3171 case 'error':
b69ab3172 return '[ERROR]';
b69ab3173 }
b69ab3174 }
b69ab3175}
b69ab3176
b69ab3177const GREY = '\x1b[38;5;8m';
b69ab3178const RED = '\x1b[38;5;9m';
b69ab3179const YELLOW = '\x1b[38;5;11m';
b69ab3180const CLEAR = '\x1b[0m';
b69ab3181
b69ab3182/**
b69ab3183 * Logger that prints to stdout via `console`, with ANSI escape coloring for easy reading.
b69ab3184 * Typically used in dev mode.
b69ab3185 */
b69ab3186export class StdoutLogger extends Logger {
b69ab3187 write(level: Level, timeStr: string, ...args: Parameters<typeof console.log>): void {
b69ab3188 // eslint-disable-next-line no-console
b69ab3189 console[level]('%s%s%s%s', GREY, timeStr, this.levelToString(level), CLEAR, ...args);
b69ab3190 }
b69ab3191
b69ab3192 levelToString(level: Level): string {
b69ab3193 switch (level) {
b69ab3194 case 'log':
b69ab3195 return GREY + ' [LOG]';
b69ab3196 case 'info':
b69ab3197 return GREY + ' [INFO]';
b69ab3198 case 'warn':
b69ab3199 return YELLOW + ' [WARN]';
b69ab31100 case 'error':
b69ab31101 return RED + '[ERROR]';
b69ab31102 }
b69ab31103 }
b69ab31104}