|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { isString } from "./utils.js"; |
| 6 | + |
| 7 | +// The available logging levels for a message. |
| 8 | +export enum LoggingLevel { |
| 9 | + // Will result in calling the `console.debug` API. |
| 10 | + Debug = "debug", |
| 11 | + // Will result in calling the `console.info` API. |
| 12 | + Info = "info", |
| 13 | + // Will result in calling the `console.warn` API. |
| 14 | + Warn = "warn", |
| 15 | + // Will result in calling the `console.error` API. |
| 16 | + Error = "error" |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Logs a message to the console, tagging it as a message that is coming from Glean. |
| 21 | + * |
| 22 | + * # Important |
| 23 | + * |
| 24 | + * The message is always logged on the `debug` level. |
| 25 | + * |
| 26 | + * @param modulePath The path to the entity which logging this message. |
| 27 | + * This should be a dotted camel case string, so spaces. Note that whatever path is |
| 28 | + * given here will be prefixed with `Glean.`. |
| 29 | + * @param message The message to log. |
| 30 | + * @param level The level in which to log this message, default is LoggingLevel.Debug. |
| 31 | + */ |
| 32 | +export default function log( |
| 33 | + modulePath: string, |
| 34 | + message: string | string[], |
| 35 | + level = LoggingLevel.Debug |
| 36 | +): void { |
| 37 | + const prefix = `(Glean.${modulePath})`; |
| 38 | + if (isString(message)) { |
| 39 | + console[level](prefix, message); |
| 40 | + } else { |
| 41 | + console[level](prefix, ...message); |
| 42 | + } |
| 43 | +} |
0 commit comments