File

src/lib/logger.model.ts

Description

Logger for writing log messages.

Index

Methods

Constructor

constructor(logger?: string | any)

Creates a new instance of a logger.

Parameters :
Name Type Optional
logger string | any Yes

Methods

Public debug
debug(methodName: string, ...params: any[])

Logs a message at level DEBUG.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public entry
entry(methodName: string, ...params: any[])

Logs the entry into a method. The method name will be logged at level INFO, the parameters at level DEBUG.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public error
error(methodName: string, ...params: any[])

Logs a message at level ERROR.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public exit
exit(methodName: string, ...params: any[])

Logs the exit of a method. The method name will be logged at level INFO, the parameters at level DEBUG.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public fatal
fatal(methodName: string, ...params: any[])

Logs a message at level FATAL.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public formatArgument
formatArgument(arg: any)

Formats the given argument.

Parameters :
Name Type Optional
arg any No
Returns : string
Public getInternalLogger
getInternalLogger()

Returns the internal Logger (for unit tests only).

Public getLogLevel
getLogLevel()

Get the log level.

Returns : LogLevel
Public info
info(methodName: string, ...params: any[])

Logs a message at level INFO.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public setLogLevel
setLogLevel(level: LogLevel)

Set the log level.

Parameters :
Name Type Optional Description
level LogLevel No

the new log level

Returns : void
Public trace
trace(methodName: string, ...params: any[])

Logs a message at level TRACE.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
Public warn
warn(methodName: string, ...params: any[])

Logs a message at level WARN.

Parameters :
Name Type Optional Description
methodName string No

name of the method

params any[] No

optional parameters to be logged; objects will be formatted as JSON

Returns : void
import * as log4javascript from "log4javascript";

import { LogLevelConverter } from "./log-level.converter";
import { LogLevel } from "./log-level.model";

/**
 * Logger for writing log messages.
 */
export class Logger {

	private logger: log4javascript.Logger;

	/**
	 * Creates a new instance of a logger.
	 */
	constructor(logger?: string | any) {
		if (typeof logger === "undefined") {
			this.logger = log4javascript.getRootLogger();
		} else if (typeof logger === "string") {
			this.logger = log4javascript.getLogger(logger);
		} else {
			this.logger = logger;
		}
	}

	/**
	 * Get the log level.
	 */
	public getLogLevel(): LogLevel {
		return LogLevelConverter.levelFromLog4Javascript(this.logger.getLevel());
	}

	/**
	 * Set the log level.
	 *
	 * @param level the new log level
	 */
	public setLogLevel(level: LogLevel): void {
		this.logger.setLevel(LogLevelConverter.levelToLog4Javascript(level));
	}

	/**
	 * Logs a message at level TRACE.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public trace(methodName: string, ...params: any[]): void {
		if (this.logger.isTraceEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.trace.apply(this.logger, args);
		}
	}

	/**
	 * Logs a message at level DEBUG.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public debug(methodName: string, ...params: any[]): void {
		if (this.logger.isDebugEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.debug.apply(this.logger, args);
		}
	}

	/**
	 * Logs a message at level INFO.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public info(methodName: string, ...params: any[]): void {
		if (this.logger.isInfoEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.info.apply(this.logger, args);
		}
	}

	/**
	 * Logs a message at level WARN.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public warn(methodName: string, ...params: any[]): void {
		if (this.logger.isWarnEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.warn.apply(this.logger, args);
		}
	}

	/**
	 * Logs a message at level ERROR.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public error(methodName: string, ...params: any[]): void {
		if (this.logger.isErrorEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.error.apply(this.logger, args);
		}
	}

	/**
	 * Logs a message at level FATAL.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public fatal(methodName: string, ...params: any[]): void {
		if (this.logger.isFatalEnabled()) {
			const args = [methodName];
			for (const param of params) {
				args.push(this.formatArgument(param));
			}
			this.logger.fatal.apply(this.logger, args);
		}
	}

	/**
	 * Logs the entry into a method.
	 * The method name will be logged at level INFO, the parameters at level DEBUG.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public entry(methodName: string, ...params: any[]): void {
		if (this.logger.isInfoEnabled()) {
			const args = [methodName, "entry"];
			if (this.logger.isDebugEnabled()) {
				for (const param of params) {
					args.push(this.formatArgument(param));
				}
			}
			this.logger.info.apply(this.logger, args);
		}
	}

	/**
	 * Logs the exit of a method.
	 * The method name will be logged at level INFO, the parameters at level DEBUG.
	 *
	 * @param methodName name of the method
	 * @param params optional parameters to be logged; objects will be formatted as JSON
	 */
	public exit(methodName: string, ...params: any[]): void {
		if (this.logger.isInfoEnabled()) {
			const args = [methodName, "exit"];
			if (this.logger.isDebugEnabled()) {
				for (const param of params) {
					args.push(this.formatArgument(param));
				}
			}
			this.logger.info.apply(this.logger, args);
		}
	}

	/**
	 * Formats the given argument.
	 */
	public formatArgument(arg: any): string {
		if (typeof arg === "string") {
			return arg;
		} else if (typeof arg === "number") {
			return arg.toString();
		} else if (arg instanceof Error) {
			// JSON.stringify() returns here "{ }"
			return arg.toString();
		} else {
			try {
				return JSON.stringify(arg);
			} catch (e) {
				return e.message;
			}
		}
	}

	/**
	 * Returns the internal Logger (for unit tests only).
	 */
	public getInternalLogger(): log4javascript.Logger {
		return this.logger;
	}
}

results matching ""

    No results matching ""