smyld-lib-common
Version:
SMYLD Javascript Core Library, contains several core APIs that benefit the developers working on SPA applications
61 lines • 1.94 kB
JavaScript
/**
* @enum {string}
* Defines the types of log messages that can be created.
* Each type has a corresponding display label.
*/
export var Type;
(function (Type) {
/** Error message type */
Type["Error"] = "ERR";
/** Information message type */
Type["Info"] = "INFO";
/** Warning message type */
Type["Warning"] = "WARN";
/** Debug message type */
Type["Debug"] = "DEBUG";
/** Default log message type */
Type["Default"] = "LOG";
})(Type || (Type = {}));
/**
* @class
* Represents a log message with type, content, and timestamp.
*/
export class LogMessage {
/** The type of log message */
type;
/** The text content of the log message */
text;
/** The timestamp when the log message was created */
date;
/**
* Creates a new log message.
*
* @param {any} msgText - The message content. If not a string, it will be converted to JSON.
* @param {Type} msgType - The type of log message.
* @param {boolean} [compact=false] - Whether to format JSON output in compact mode.
*
* @example
* // Create a simple text message
* const infoMsg = new LogMessage("Operation completed", Type.Info);
*
* @example
* // Create a message from an object with pretty-printed JSON
* const user = { id: 123, name: "John Doe" };
* const debugMsg = new LogMessage(user, Type.Debug, false);
*
* @example
* // Create a message from an object with compact JSON
* const errorMsg = new LogMessage(error, Type.Error, true);
*/
constructor(msgText, msgType, compact = false) {
this.type = msgType;
if (typeof msgText !== 'string') {
msgText = compact
? JSON.stringify(msgText)
: JSON.stringify(msgText, undefined, 2);
}
this.text = msgText;
this.date = new Date();
}
}
//# sourceMappingURL=LogMessage.js.map