import * as log4javascript from "log4javascript" ; import { LocalStorageAppenderConfiguration } from "./local-storage-appender.configuration" ; import { LogLevelConverter } from "./log-level.converter" ; import { LogLevel } from "./log-level.model" ; import { LogMessage } from "./log-message.model" ; /**
 * An appender which stores the log messages in the browser's local storage.
 *
 * The messages are saved JSON-serialized.
 * You have to configure which key is used for storing the messages.
 *
 * A typical configuration could be:
 *
 * ```json
 * {
 *   "localStorageKey": "myLogs",
 *   "maxMessages": 500,
 *   "threshold": "INFO"
 * }
 * ```
 */ export class LocalStorageAppender extends log4javascript . Appender { private static maxMessagesDefault = 250 ; private static thresholdDefault = "WARN" ; private maxMessages : number ; private localStorageKey : string ; private logMessages : LogMessage [ ] ; param configuration constructor ( configuration ) { super ( ) ; if ( ! configuration ) { throw new Error ( "configuration must be not empty" ) ; } if ( ! configuration . localStorageKey || configuration . localStorageKey === "" ) { throw new Error ( "localStorageKey must be not empty" ) ; } this . localStorageKey = configuration . localStorageKey ; this . logMessages = LocalStorageAppender . loadLogMessages ( this . localStorageKey ) ; this . configure ( { localStorageKey : configuration . localStorageKey , maxMessages : configuration . maxMessages || LocalStorageAppender . maxMessagesDefault , threshold : configuration . threshold || LocalStorageAppender . thresholdDefault , } ) ; } param localStorageKey return public static loadLogMessages ( localStorageKey ) { let logMessages ; if ( ! localStorageKey || localStorage . getItem ( localStorageKey ) === null ) { logMessages = [ ] ; } else { logMessages = JSON . parse ( localStorage . getItem ( localStorageKey ) ) ; for ( const logMessage of logMessages ) { logMessage . timeStamp = new Date ( logMessage . timeStamp ) ; } } return logMessages ; } param localStorageKey public static removeLogMessages ( localStorageKey ) { localStorage . removeItem ( localStorageKey ) ; } param configuration public configure ( configuration ) { if ( configuration ) { if ( configuration . localStorageKey && configuration . localStorageKey !== this . localStorageKey ) { throw new Error ( "localStorageKey must not be changed" ) ; } if ( configuration . maxMessages ) { this . setMaxMessages ( configuration . maxMessages ) ; } if ( configuration . threshold ) { const convertedThreshold = LogLevelConverter . levelToLog4Javascript ( LogLevelConverter . levelFromString ( configuration . threshold ) ) ; this . setThreshold ( convertedThreshold ) ; } } } param loggingEvent public append ( loggingEvent ) { while ( this . logMessages . length >= this . maxMessages ) { this . logMessages . shift ( ) ; } const message = { level : LogLevel [ LogLevelConverter . levelFromLog4Javascript ( loggingEvent . level ) ] , logger : typeof loggingEvent . logger !== "undefined" ? loggingEvent . logger . name : undefined , message : loggingEvent . messages . slice ( 1 ) , methodName : loggingEvent . messages [ 0 ] , timeStamp : loggingEvent . timeStamp , } ; this . logMessages . push ( message ) ; localStorage . setItem ( this . localStorageKey , JSON . stringify ( this . logMessages ) ) ; } return public toString ( ) { return "Ionic.Logging.LocalStorageAppender" ; } /**
     * Get the key which is used to store the messages in the local storage.
     */ public getLocalStorageKey ( ) { return this . localStorageKey ; } /**
     * Get the maximum number of messages which will be stored in local storage.
     */ public getMaxMessages ( ) { return this . maxMessages ; } param value public setMaxMessages ( value ) { if ( this . maxMessages !== value ) { this . maxMessages = value ; if ( this . logMessages . length > this . maxMessages ) { while ( this . logMessages . length > this . maxMessages ) { this . logMessages . shift ( ) ; } localStorage . setItem ( this . localStorageKey , JSON . stringify ( this . logMessages ) ) ; } } } return public getLogMessages ( ) { return this . logMessages ; } /**
     * Removes all messages from local storage.
     * Mainly for unit testing purposes.
     */ public clearLog ( ) { this . logMessages = [ ] ; localStorage . removeItem ( this . localStorageKey ) ; } }