import { Injectable } from "@angular/core" ; import { LogLevel } from "./log-level" ; import * as Sentry from "@sentry/browser" ; import { environment } from "../../../environments/environment" ; /**
 * Centrally managed logging to allow log messages to be filtered by level and even sent to a remote logging service
 * that allows developers to monitor and analyse problems.
 *
 * Logging to the remote monitoring server is set only for warnings and errors.
 *
 * To allow remote logging, call Sentry.init during bootstrap in your AppModule or somewhere early on during startup.
 */ @ Injectable ( { providedIn : "root" , } ) export class LoggingService { param options static initRemoteLogging ( options ) { if ( ! options . dsn ) { return ; } const defaultOptions = { release : "ndb-core@" + environment . appVersion , } ; Sentry . init ( Object . assign ( defaultOptions , options ) ) ; } param tagName param value static setLoggingContext ( tagName , value ) { Sentry . configureScope ( ( scope ) => { scope . setTag ( tagName , value ) ; } ) ; } param username static setLoggingContextUser ( username ) { Sentry . setUser ( { username : username } ) ; } param message public debug ( message ) { this . log ( message , LogLevel . DEBUG ) ; } param message public info ( message ) { this . log ( message , LogLevel . INFO ) ; } param message public warn ( message ) { this . log ( message , LogLevel . WARN ) ; } param message public error ( message ) { this . log ( message , LogLevel . ERROR ) ; } param message param logLevel public log ( message , logLevel = LogLevel . INFO ) { this . logToConsole ( message , logLevel ) ; if ( logLevel !== LogLevel . DEBUG && logLevel !== LogLevel . INFO ) { this . logToRemoteMonitoring ( message , logLevel ) ; } } private logToConsole ( message , logLevel ) { switch ( + logLevel ) { case LogLevel . DEBUG : console . debug ( message ) ; break ; case LogLevel . INFO : console . info ( message ) ; break ; case LogLevel . WARN : console . warn ( message ) ; break ; case LogLevel . ERROR : console . error ( message ) ; break ; default : console . log ( message ) ; break ; } } private logToRemoteMonitoring ( message , logLevel ) { if ( logLevel === LogLevel . ERROR ) { Sentry . captureException ( message ) ; } else { Sentry . captureMessage ( message , this . translateLogLevel ( logLevel ) ) ; } } private translateLogLevel ( logLevel ) { switch ( + logLevel ) { case LogLevel . DEBUG : return Sentry . Severity . Debug ; case LogLevel . INFO : return Sentry . Severity . Info ; case LogLevel . WARN : return Sentry . Severity . Warning ; case LogLevel . ERROR : return Sentry . Severity . Error ; default : return Sentry . Severity . Info ; } } }