From 3bc9f78d767e751eed533a79758c2cb7273abb0e Mon Sep 17 00:00:00 2001 From: Domas Date: Tue, 23 Feb 2021 13:02:45 +0200 Subject: [PATCH] Logging: add frontend logging helpers to @grafana/runtime package (#30482) --- packages/grafana-runtime/src/index.ts | 1 + packages/grafana-runtime/src/utils/logging.ts | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 packages/grafana-runtime/src/utils/logging.ts diff --git a/packages/grafana-runtime/src/index.ts b/packages/grafana-runtime/src/index.ts index 22d865593cb..a8f139042df 100644 --- a/packages/grafana-runtime/src/index.ts +++ b/packages/grafana-runtime/src/index.ts @@ -9,5 +9,6 @@ export * from './types'; export * from './measurement'; export { loadPluginCss, SystemJS, PluginCssOptions } from './utils/plugin'; export { reportMetaAnalytics } from './utils/analytics'; +export { logInfo, logDebug, logWarning, logError } from './utils/logging'; export { DataSourceWithBackend, HealthCheckResult, HealthStatus } from './utils/DataSourceWithBackend'; export { toDataQueryError, toDataQueryResponse, frameToMetricFindValue } from './utils/queryResponse'; diff --git a/packages/grafana-runtime/src/utils/logging.ts b/packages/grafana-runtime/src/utils/logging.ts new file mode 100644 index 00000000000..0f8051a54f6 --- /dev/null +++ b/packages/grafana-runtime/src/utils/logging.ts @@ -0,0 +1,50 @@ +import { captureMessage, captureException, Severity as LogLevel } from '@sentry/browser'; +export { LogLevel }; + +// a bit stricter than what Sentry allows +type Contexts = Record>>; + +/** + * Log a message at INFO level. Depending on configuration might be forwarded to backend and logged to stdout or sent to Sentry + * + * @public + */ +export function logInfo(message: string, contexts?: Contexts) { + captureMessage(message, { + level: LogLevel.Info, + contexts, + }); +} + +/** + * Log a message at WARNING level. Depending on configuration might be forwarded to backend and logged to stdout or sent to Sentry + * + * @public + */ +export function logWarning(message: string, contexts?: Contexts) { + captureMessage(message, { + level: LogLevel.Warning, + contexts, + }); +} + +/** + * Log a message at DEBUG level. Depending on configuration might be forwarded to backend and logged to stdout or sent to Sentry + * + * @public + */ +export function logDebug(message: string, contexts?: Contexts) { + captureMessage(message, { + level: LogLevel.Debug, + contexts, + }); +} + +/** + * Log an error. Depending on configuration might be forwarded to backend and logged to stdout or sent to Sentry + * + * @public + */ +export function logError(err: Error, contexts?: Contexts) { + captureException(err, { contexts }); +}