/** * Well known log levels. * * DEBUG < INFO < WARN < ERROR < OFF * * If DEBUG is enabled for a logger, then all messages are logged. * * If INFO is enabled for a logger, then the messages on level INFO, WARN and ERROR are logged. * * If WARN is enabled for a logger, then the messages on level WARN and ERROR are logged. * * If ERROR is enabled for a logger, then the messages on level ERROR are logged. * * If OFF is enabled for a logger, then nothing is logged. */ type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR" | "OFF"; /** * A logger interface. * It is inspired from the interface and behavior of the * [org.slf4j.Logger API](https://www.javadoc.io/doc/org.slf4j/slf4j-api/1.7.22/org/slf4j/Logger.html). * * @example Log debug message * ```ts * import { loggerForName } from "apprt-core/Logger"; * const LOG = loggerForName("mybundle/MyFile"); * LOG.debug("Component started"); * ``` * * @example Use placeholders * ```ts * import { loggerForName } from "apprt-core/Logger"; * const LOG = loggerForName("mybundle/MyFile"); * const name = "Xyz"; * LOG.debug("Component '{}' started", name); * // -> logs "Component 'Xyz' started" * ``` * * @example Test wether log level is enabled * ```ts * import { loggerForName } from "apprt-core/Logger"; * const LOG = loggerForName("mybundle/MyFile"); * * if (LOG.isDebugEnabled()){ * LOG.debug("did something"); * } * ``` * * @example Log prefixed messages * ```ts * import { loggerForName } from "apprt-core/Logger"; * const LOG = loggerForName("mybundle/MyFile"); * const LOGWithPrefix = LOG.withPrefix("[my prefix]"); * LOGWithPrefix.info("My Message"); * // -> logs "[my prefix] My Message" * ``` */ interface Logger { /** * Is DEBUG level enabled for this logger. */ isDebugEnabled(): boolean; /** * Is INFO level enabled for this logger. */ isInfoEnabled(): boolean; /** * Is WARN level enabled for this logger. */ isWarnEnabled(): boolean; /** * Is ERROR level enabled for this logger. */ isErrorEnabled(): boolean; /** * Is given level enabled for this logger. */ isLevelEnabled(level: LogLevel): boolean; /** * Lookup the current logger level. */ getLevel(): LogLevel; /** * Get the current logger name. */ getName(): string; /** * Log a message at level DEBUG. * * The message may contain one or more placeholders '\{\}', which * are replaced by the string value of the given arguments. * * Note that placeholders are always replaced and can not be * escaped at the moment. * * If the list of arguments is longer than the number of placeholders * in the given message, each remaining argument is logged in a separate * log statement. * * @param msg the message, may contain '\{\}' placeholders. * @param args arguments filled into the placeholder or directly logged. */ debug(msg: string, ...args: any[]): void; /** * Log a message at level INFO. * * Similar to {@link debug} except for the used log level. * * @param msg the message, may contain '\{\}' placeholders. * @param args arguments filled into the placeholder or directly logged. */ info(msg: string, ...args: any[]): void; /** * Log a message at level WARN. * * Similar to {@link debug} except for the used log level. * * @param msg the message, may contain '\{\}' placeholders. * @param args arguments filled into the placeholder or directly logged. */ warn(msg: string, ...args: any[]): void; /** * Convenience method to log an error (without an additional message) as a warning. * @param error the error to log */ warn(error: Error): void; /** * Log a message at level ERROR. * * Similar to {@link debug} except for the used log level. * * @param msg the message, may contain '\{\}' placeholders. * @param args arguments filled into the placeholder or directly logged. */ error(msg: string, ...args: any[]): void; /** * Convenience method to log an error (without an additional message) as an error. * @param error the error to log */ error(error: Error): void; /** * Creates a new Logger, which will prefix each log message with the given prefix. * * @example Log prefixed messages * ```ts * const LOG = loggerForName("mybundle/MyFile"); * const LOGWithPrefix = LOG.withPrefix("[my prefix]"); * LOGWithPrefix.info("My Message"); * // -> logs "[my prefix] My Message" * ``` * * @param prefix a prefix */ withPrefix(prefix: string): Logger; } /** * Creates a new Logger. * @param name Slash-separated name of a logger. Whitespace is not allowed. * @returns a logger instance. Returns the same logger instance for an equal logger name. * * @example Logger usage * ```ts * import { loggerForName } from "apprt-core/Logger"; * const LOG = loggerForName("mybundle/MyFile"); * LOG.info("Component started"); * ``` */ declare function loggerForName(name: string): Logger; /** * Changes the log level of a logger. * * @param name name of a logger. Empty string changes the root logger. * @param level the new level of the logger. * * @example Samples * ```ts * import {setLogLevel} from "apprt-core/Logger"; * // Changes the root level to DEBUG * // All loggers without special configuration will now output messages. * setLogLevel("", "DEBUG"); * * // Changes the log level of the bundle 'apprt-core' to DEBUG. * // All loggers in this bundle will now output messages. * setLogLevel("apprt-core", "DEBUG"); * * // Changes the log level of the bundles file 'apprt-core/Promise' to DEBUG. * // Only the special logger will now output messages. * setLogLevel("apprt-core/Promise", "DEBUG"); * * // clears the config for the logger and inherits from the parent. * // this is not allowed for the root logger * setLogLevel("apprt-core/Promise"); * ``` */ declare function setLogLevel(name: string, level?: LogLevel): void; /** * Lookup current level of a logger. * @param name Name of a logger. Empty string checks the root logger. * @returns the current log level. */ declare function getLogLevel(name: string): LogLevel; /** * Sets the logging configuration state. * Note that this function resets any existing configuration state. * * @param config the new configuration. If empty or undefined, the configuration is cleared. * It can be string with the syntax `bundle:LEVEL,otherbundle:LEVEL`, * e.g. `apprt-core:DEBUG, map-init:WARN` */ declare function setLogConfig(config?: [string, LogLevel][] | string): void; /** * Gets the current logging configuration state. * @returns Array like: [["", "DEBUG"] , ["apprt-core", "INFO"], ...]; */ declare function getLogConfig(): [string, LogLevel][]; /** * Type of a effective output method. */ type WriterMethod = (msg: string, ...params: any[]) => void; /** * The effective writer of log messages. * The default Writer implementation is the console. */ interface Writer { /** * Write a debug message. */ debug: WriterMethod; /** * Write a info message. */ info: WriterMethod; /** * Write a warn message. */ warn: WriterMethod; /** * Write a error message. */ error: WriterMethod; } /** * Set the concrete log writer. * By default log messages are written to the console. * * @param newWriter the new Writer. If undefined, the console is used. */ declare function setWriter(newWriter?: Writer): void; /** * Parses a configuration string into a logging configuration. * @param loggingConfStr a string in the format `logger-name:LEVEL, other-logger-name: LEVEL`. * e.g. `apprt-core:DEBUG, map-init:WARN` * or `DEBUG` for the root level. * or `apprt-core` to set it to DEBUG. * @returns the parsed logging configuration. */ declare function readConfigFromString(loggingConfStr?: string): [string, LogLevel][]; export { getLogConfig, getLogLevel, loggerForName, readConfigFromString, setLogConfig, setLogLevel, setWriter }; export type { LogLevel, Logger, Writer, WriterMethod };