import { EventEmitter } from "@angular/core" ; import * as log4javascript from "log4javascript" ; import { AjaxAppenderConfiguration } from "./ajax-appender.configuration" ; import { JsonLayout } from "./json-layout.model" ; import { LogLevelConverter } from "./log-level.converter" ; /**
 * An appender which sends the log messages to a server via HTTP.
 *
 * A typical configuration could be:
 *
 * ```json
 * {
 *   "url": "https://my.backend.xy/LoggingBackend",
 *   "batchSize": 10,
 *   "timerInterval": 60000,
 *   "threshold": "INFO"
 * }
 * ```
 */ export class AjaxAppender extends log4javascript . Appender { private static batchSizeDefault = 1 ; private static timerIntervalDefault = 0 ; private static thresholdDefault = "WARN" ; param message public appenderFailed : EventEmitter < string > ; private ajaxAppender : log4javascript . AjaxAppender ; private url : string ; private withCredentials : boolean ; param configuration constructor ( configuration ) { super ( ) ; if ( ! configuration ) { throw new Error ( "configuration must be not empty" ) ; } if ( ! configuration . url ) { throw new Error ( "url must be not empty" ) ; } this . ajaxAppender = new log4javascript . AjaxAppender ( configuration . url , configuration . withCredentials ) ; this . url = configuration . url ; this . withCredentials = configuration . withCredentials ; this . ajaxAppender . setLayout ( new JsonLayout ( false , false ) ) ; this . ajaxAppender . addHeader ( "Content-Type" , "application/json; charset=utf-8" ) ; this . ajaxAppender . setSendAllOnUnload ( true ) ; this . appenderFailed = new EventEmitter < string > ( ) ; this . ajaxAppender . setFailCallback ( ( message ) => { this . appenderFailed . emit ( message ) ; } ) ; this . configure ( { batchSize : configuration . batchSize || AjaxAppender . batchSizeDefault , threshold : configuration . threshold || AjaxAppender . thresholdDefault , timerInterval : configuration . timerInterval || AjaxAppender . timerIntervalDefault , url : configuration . url , withCredentials : configuration . withCredentials } ) ; } param configuration public configure ( configuration ) { if ( configuration ) { if ( configuration . url && configuration . url !== this . url ) { throw new Error ( "url must not be changed" ) ; } if ( configuration . withCredentials && configuration . withCredentials !== this . withCredentials ) { throw new Error ( "withCredentials must not be changed" ) ; } if ( configuration . batchSize ) { this . setBatchSize ( configuration . batchSize ) ; } if ( typeof configuration . timerInterval === "number" ) { this . setTimerInterval ( configuration . timerInterval ) ; } if ( configuration . threshold ) { const convertedThreshold = LogLevelConverter . levelToLog4Javascript ( LogLevelConverter . levelFromString ( configuration . threshold ) ) ; this . setThreshold ( convertedThreshold ) ; } } } param loggingEvent public append ( loggingEvent ) { this . ajaxAppender . append ( loggingEvent ) ; } return public toString ( ) { return "Ionic.Logging.AjaxAppender" ; } /**
     * Get the internally used appender.
     * Mainly for unit testing purposes.
     */ public getInternalAppender ( ) { return this . ajaxAppender ; } /**
     * Returns the number of log messages sent in each request.
     */ public getBatchSize ( ) { return this . ajaxAppender . getBatchSize ( ) ; } param batchSize public setBatchSize ( batchSize ) { this . ajaxAppender . setBatchSize ( batchSize ) ; } /**
     * Returns the appender's layout.
     */ public getLayout ( ) { return this . ajaxAppender . getLayout ( ) ; } /**
     * Sets the appender's layout.
     */ public setLayout ( layout ) { this . ajaxAppender . setLayout ( layout ) ; } /**
     * Returns the length of time in milliseconds between each sending of queued log messages.
     */ public getTimerInterval ( ) { return this . ajaxAppender . getTimerInterval ( ) ; } param timerInterval public setTimerInterval ( timerInterval ) { this . ajaxAppender . setTimed ( timerInterval > 0 ) ; this . ajaxAppender . setTimerInterval ( timerInterval ) ; } }