|
| 1 | +// Copyright 2023 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +type JSONValue = |
| 12 | + | null |
| 13 | + | boolean |
| 14 | + | number |
| 15 | + | string |
| 16 | + | JSONValue[] |
| 17 | + | { [key: string]: JSONValue }; |
| 18 | + |
| 19 | +interface Logger { |
| 20 | + /** |
| 21 | + * Logs a message with additional context and an optional error at debug |
| 22 | + * level. |
| 23 | + * @param msg - the string message to log |
| 24 | + * @param context - additional structured context to include with the |
| 25 | + * message when shipped to a remote service |
| 26 | + * @param error - a possible JS Error to include with the message when |
| 27 | + * shipped to a remote service. Typed as unknown for convenience but |
| 28 | + * anything passed here which is not instanceof Error will not be attached. |
| 29 | + */ |
| 30 | + debug( |
| 31 | + msg: string, |
| 32 | + context?: Record<string, JSONValue>, |
| 33 | + error?: unknown, |
| 34 | + ): void; |
| 35 | + /** |
| 36 | + * Logs a message with additional context and an optional error at info |
| 37 | + * level. |
| 38 | + * @param msg - the string message to log |
| 39 | + * @param context - additional structured context to include with the |
| 40 | + * message when shipped to a remote service |
| 41 | + * @param error - a possible JS Error to include with the message when |
| 42 | + * shipped to a remote service. Typed as unknown for convenience but |
| 43 | + * anything passed here which is not instanceof Error will not be attached. |
| 44 | + */ |
| 45 | + info(msg: string, context?: Record<string, JSONValue>, error?: unknown): void; |
| 46 | + /** |
| 47 | + * Logs a message with additional context and an optional error at warn |
| 48 | + * level. |
| 49 | + * @param msg - the string message to log |
| 50 | + * @param context - additional structured context to include with the |
| 51 | + * message when shipped to a remote service |
| 52 | + * @param error - a possible JS Error to include with the message when |
| 53 | + * shipped to a remote service. Typed as unknown for convenience but |
| 54 | + * anything passed here which is not instanceof Error will not be attached. |
| 55 | + */ |
| 56 | + warn(msg: string, context?: Record<string, JSONValue>, error?: unknown): void; |
| 57 | + /** |
| 58 | + * Logs a message with additional context and an optional error at error |
| 59 | + * level. |
| 60 | + * @param msg - the string message to log |
| 61 | + * @param context - additional structured context to include with the |
| 62 | + * message when shipped to a remote service |
| 63 | + * @param error - a possible JS Error to include with the message when |
| 64 | + * shipped to a remote service. Typed as unknown for convenience but |
| 65 | + * anything passed here which is not instanceof Error will not be attached. |
| 66 | + */ |
| 67 | + error( |
| 68 | + msg: string, |
| 69 | + context?: Record<string, JSONValue>, |
| 70 | + error?: unknown, |
| 71 | + ): void; |
| 72 | +} |
| 73 | + |
| 74 | +let logger: Logger = console; |
| 75 | + |
| 76 | +/** |
| 77 | + * Sets the logger returned by {@link getLogger}. It was added to allow |
| 78 | + * cockroach cloud to pass in a custom logger which attaches additional metadata |
| 79 | + * to each call and sends errors up to datadog. |
| 80 | + * @param newLogger the Logger to set |
| 81 | + */ |
| 82 | +export function setLogger(newLogger: Logger) { |
| 83 | + logger = newLogger; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * @returns the most recent logger set by {@link setLogger}, or console if one was never set. |
| 88 | + */ |
| 89 | +export function getLogger(): Logger { |
| 90 | + return logger; |
| 91 | +} |
0 commit comments