|
| 1 | +import { format } from 'util'; |
| 2 | + |
| 3 | +// safely preserve unpatched console.* methods in case of compat require |
| 4 | +const unpatchedConsole = { |
| 5 | + debug: console.debug, |
| 6 | + info: console.info, |
| 7 | + log: console.log, |
| 8 | + warn: console.warn, |
| 9 | + error: console.error, |
| 10 | +}; |
| 11 | + |
| 12 | +// Determine if structured logs are supported (node >= 10). If something goes wrong, |
| 13 | +// assume no since unstructured is safer. |
| 14 | +const SUPPORTS_STRUCTURED_LOGS = |
| 15 | + parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10; |
| 16 | + |
| 17 | +// Map LogSeverity types to their equivalent `console.*` method. |
| 18 | +const CONSOLE_SEVERITY: { |
| 19 | + [severity: string]: 'debug' | 'info' | 'warn' | 'error'; |
| 20 | +} = { |
| 21 | + DEBUG: 'debug', |
| 22 | + INFO: 'info', |
| 23 | + NOTICE: 'info', |
| 24 | + WARNING: 'warn', |
| 25 | + ERROR: 'error', |
| 26 | + CRITICAL: 'error', |
| 27 | + ALERT: 'error', |
| 28 | + EMERGENCY: 'error', |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity) for more. |
| 33 | + */ |
| 34 | +export type LogSeverity = |
| 35 | + | 'DEBUG' |
| 36 | + | 'INFO' |
| 37 | + | 'NOTICE' |
| 38 | + | 'WARNING' |
| 39 | + | 'ERROR' |
| 40 | + | 'CRITICAL' |
| 41 | + | 'ALERT' |
| 42 | + | 'EMERGENCY'; |
| 43 | + |
| 44 | +/** |
| 45 | + * `LogEntry` represents a structured Cloud Logging entry. All keys aside from `severity` and `message` are |
| 46 | + * included in the `jsonPayload` of the logged entry. |
| 47 | + */ |
| 48 | +export interface LogEntry { |
| 49 | + severity: LogSeverity; |
| 50 | + message?: string; |
| 51 | + [key: string]: any; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Writes a `LogEntry` to `stdout`/`stderr` (depending on severity). |
| 56 | + * @param entry The LogEntry including severity, message, and any additional structured metadata. |
| 57 | + */ |
| 58 | +export function write(entry: LogEntry) { |
| 59 | + if (SUPPORTS_STRUCTURED_LOGS) { |
| 60 | + unpatchedConsole[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(entry)); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let message = entry.message || ''; |
| 65 | + const jsonPayload: { [key: string]: any } = {}; |
| 66 | + let jsonKeyCount = 0; |
| 67 | + for (const k in entry) { |
| 68 | + if (!['severity', 'message'].includes(k)) { |
| 69 | + jsonKeyCount++; |
| 70 | + jsonPayload[k] = entry[k]; |
| 71 | + } |
| 72 | + } |
| 73 | + if (jsonKeyCount > 0) { |
| 74 | + message = `${message} ${JSON.stringify(jsonPayload, null, 2)}`; |
| 75 | + } |
| 76 | + unpatchedConsole[CONSOLE_SEVERITY[entry.severity]](message); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Writes a `DEBUG` severity log. If the last argument provided is a plain object, |
| 81 | + * it will be added to the `jsonPayload` in the Cloud Logging entry. |
| 82 | + * @param args Arguments, concatenated into the log message with space separators. |
| 83 | + */ |
| 84 | +export function debug(...args: any[]) { |
| 85 | + write(entryFromArgs('DEBUG', args)); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Writes an `INFO` severity log. If the last argument provided is a plain object, |
| 90 | + * it will be added to the `jsonPayload` in the Cloud Logging entry. |
| 91 | + * @param args Arguments, concatenated into the log message with space separators. |
| 92 | + */ |
| 93 | +export function log(...args: any[]) { |
| 94 | + write(entryFromArgs('INFO', args)); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Writes an `INFO` severity log. If the last argument provided is a plain object, |
| 99 | + * it will be added to the `jsonPayload` in the Cloud Logging entry. |
| 100 | + * @param args Arguments, concatenated into the log message with space separators. |
| 101 | + */ |
| 102 | +export function info(...args: any[]) { |
| 103 | + write(entryFromArgs('INFO', args)); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Writes a `WARNING` severity log. If the last argument provided is a plain object, |
| 108 | + * it will be added to the `jsonPayload` in the Cloud Logging entry. |
| 109 | + * @param args Arguments, concatenated into the log message with space separators. |
| 110 | + */ |
| 111 | +export function warn(...args: any[]) { |
| 112 | + write(entryFromArgs('WARNING', args)); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Writes an `ERROR` severity log. If the last argument provided is a plain object, |
| 117 | + * it will be added to the `jsonPayload` in the Cloud Logging entry. |
| 118 | + * @param args Arguments, concatenated into the log message with space separators. |
| 119 | + */ |
| 120 | +export function error(...args: any[]) { |
| 121 | + write(entryFromArgs('ERROR', args)); |
| 122 | +} |
| 123 | + |
| 124 | +function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry { |
| 125 | + let entry = {}; |
| 126 | + const lastArg = args[args.length - 1]; |
| 127 | + if (typeof lastArg == 'object' && lastArg.constructor == Object) { |
| 128 | + entry = args.pop(); |
| 129 | + } |
| 130 | + return Object.assign({}, entry, { |
| 131 | + severity, |
| 132 | + // mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args |
| 133 | + message: format.apply(null, args), |
| 134 | + }); |
| 135 | +} |
0 commit comments