File

src/lib/ajax-appender.model.ts

Description

An appender which sends the log messages to a server via HTTP.

A typical configuration could be:

{
  "url": "https://my.backend.xy/LoggingBackend",
  "batchSize": 10,
  "timerInterval": 60000,
  "threshold": "INFO"
}

Index

Properties
Methods

Constructor

constructor(configuration: AjaxAppenderConfiguration)

Creates a new instance of the appender.

Parameters :
Name Type Optional Description
configuration AjaxAppenderConfiguration No

configuration for the appender.

Properties

Public appenderFailed
Type : EventEmitter<string>

Event triggered when the appender could not send log messages to the server.

Parameters :
Name Description
message

error message

Methods

Public append
append(loggingEvent: log4javascript.LoggingEvent)

Appender-specific method to append a log message.

Parameters :
Name Type Optional Description
loggingEvent log4javascript.LoggingEvent No

event to be appended.

Returns : void
Public configure
configure(configuration: AjaxAppenderConfiguration)

Configures the logging depending on the given configuration.

Only the defined properties get overwritten. Neither url nor withCredentials can be modified.

Parameters :
Name Type Optional Description
configuration AjaxAppenderConfiguration No

configuration data.

Returns : void
Public getBatchSize
getBatchSize()

Returns the number of log messages sent in each request.

Returns : number
Public getInternalAppender
getInternalAppender()

Get the internally used appender. Mainly for unit testing purposes.

Public getLayout
getLayout()

Returns the appender's layout.

Returns : log4javascript.Layout
Public getTimerInterval
getTimerInterval()

Returns the length of time in milliseconds between each sending of queued log messages.

Returns : number
Public setBatchSize
setBatchSize(batchSize: number)

Sets the number of log messages to send in each request.

Parameters :
Name Type Optional Description
batchSize number No

new batch size

Returns : void
Public setLayout
setLayout(layout: log4javascript.Layout)

Sets the appender's layout.

Parameters :
Name Type Optional
layout log4javascript.Layout No
Returns : void
Public setTimerInterval
setTimerInterval(timerInterval: number)

Sets the length of time in milliseconds between each sending of queued log messages.

Parameters :
Name Type Optional Description
timerInterval number No

new timer interval

Returns : void
Public toString
toString()

Gets the appender's name. Mainly for unit testing purposes.

Returns : string

appender's name

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";

	/**
	 * Event triggered when the appender could not send log messages to the server.
	 *
	 * @param message error message
	 */
	public appenderFailed: EventEmitter<string>;

	private ajaxAppender: log4javascript.AjaxAppender;
	private url: string;
	private withCredentials: boolean;

	/**
	 * Creates a new instance of the appender.
	 *
	 * @param configuration configuration for the appender.
	 */
	constructor(configuration: AjaxAppenderConfiguration) {
		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: any) => {
			this.appenderFailed.emit(message);
		});

		// process remaining configuration
		this.configure({
			batchSize: configuration.batchSize || AjaxAppender.batchSizeDefault,
			threshold: configuration.threshold || AjaxAppender.thresholdDefault,
			timerInterval: configuration.timerInterval || AjaxAppender.timerIntervalDefault,
			url: configuration.url,
			withCredentials: configuration.withCredentials
		});

	}

	/**
	 * Configures the logging depending on the given configuration.
	 *
	 * Only the defined properties get overwritten.
	 * Neither url nor withCredentials can be modified.
	 *
	 * @param configuration configuration data.
	 */
	public configure(configuration: AjaxAppenderConfiguration): void {
		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);
			}
		}
	}

	/**
	 * Appender-specific method to append a log message.
	 *
	 * @param loggingEvent event to be appended.
	 */
	public append(loggingEvent: log4javascript.LoggingEvent): void {
		this.ajaxAppender.append(loggingEvent);
	}

	/**
	 * Gets the appender's name.
	 * Mainly for unit testing purposes.
	 *
	 * @return appender's name
	 */
	public toString(): string {
		return "Ionic.Logging.AjaxAppender";
	}

	/**
	 * Get the internally used appender.
	 * Mainly for unit testing purposes.
	 */
	public getInternalAppender(): log4javascript.AjaxAppender {
		return this.ajaxAppender;
	}

	/**
	 * Returns the number of log messages sent in each request.
	 */
	public getBatchSize(): number {
		return this.ajaxAppender.getBatchSize();
	}

	/**
	 * Sets the number of log messages to send in each request.
	 *
	 * @param batchSize new batch size
	 */
	public setBatchSize(batchSize: number): void {
		this.ajaxAppender.setBatchSize(batchSize);
	}

	/**
	 * Returns the appender's layout.
	 */
	public getLayout(): log4javascript.Layout {
		return this.ajaxAppender.getLayout();
	}

	/**
	 * Sets the appender's layout.
	 */
	public setLayout(layout: log4javascript.Layout): void {
		this.ajaxAppender.setLayout(layout);
	}

	/**
	 * Returns the length of time in milliseconds between each sending of queued log messages.
	 */
	public getTimerInterval(): number {
		return this.ajaxAppender.getTimerInterval();
	}

	/**
	 * Sets the length of time in milliseconds between each sending of queued log messages.
	 *
	 * @param timerInterval new timer interval
	 */
	public setTimerInterval(timerInterval: number): void {
		this.ajaxAppender.setTimed(timerInterval > 0);
		this.ajaxAppender.setTimerInterval(timerInterval);
	}
}

results matching ""

    No results matching ""