@villedemontreal/logger
Version:
Logger and logging utilities
170 lines • 6.38 kB
TypeScript
import { LogLevel } from '@villedemontreal/general-utils';
import { pino } from 'pino';
import { LoggerConfigs } from './config/configs';
export { LogLevel } from '@villedemontreal/general-utils';
/**
* A Logger.
*/
export interface ILogger {
debug(messageObj: any, txtMsg?: string): void;
info(messageObj: any, txtMsg?: string): void;
warning(messageObj: any, txtMsg?: string): void;
error(messageObj: any, txtMsg?: string): void;
log(level: LogLevel, messageObj: any, txtMsg?: string): void;
}
/**
* Converts a Pino level to its number value.
*/
export declare const convertPinoLevelToNumber: (pinoLogLevel: pino.Level) => number;
/**
* Converts a local LogLevel to a Pino label level.
*/
export declare const convertLogLevelToPinoLabelLevel: (logLevel: LogLevel) => pino.Level;
/**
* Converts a local LogLevel to a Pino number level.
*/
export declare const convertLogLevelToPinoNumberLevel: (logLevel: LogLevel) => number;
/**
* Initialize the logger with the config given in parameter
* This function must be used before using createLogger or Logger Class
* @param {LoggerConfigs} loggerConfig
* @param {string} [name='default']
* @param force if `true`, the logger will be initialized
* again even if it already is.
*/
export declare const initLogger: (loggerConfig: LoggerConfigs, name?: string, force?: boolean) => void;
/**
* Change the global log level of the application. Useful to change dynamically
* the log level of something that is already started.
* @param level The log level to set for the application
*/
export declare const setGlobalLogLevel: (level: LogLevel) => void;
/**
* Shorthands function that return a new logger instance
* Internally, we use the same logger instance but with different context like the name given in parameter
* and this context is kept in this new instance returned.
* @export
* @param {string} name
* @returns {ILogger}
*/
export declare function createLogger(name: string): ILogger;
export declare function isInited(): boolean;
/**
* Logger implementation.
*/
export declare class Logger implements ILogger {
private readonly pino;
/**
* Creates a logger.
*
* @param the logger name. This name should be related
* to the file the logger is created in. On a production
* environment, it's possible that only this name will
* be available to locate the source of the log.
* Streams will be created after the first call to the logger
*/
constructor(name: string);
/**
* Logs a DEBUG level message object.
*
* If the extra "txtMsg" parameter is set, it is
* going to be added to messageObj as a ".msg"
* property (if messageObj is an object) or
* concatenated to messageObj (if it's not an
* object).
*
* Those types of logs are possible :
*
* - log.debug("a simple text message");
* - log.debug({"name": "an object"});
* - log.debug({"name": "an object..."}, "... and an extra text message");
* - log.debug(err, "a catched error and an explanation message");
*/
debug(messageObj: any, txtMsg?: string): void;
/**
* Logs an INFO level message.
*
* If the extra "txtMsg" parameter is set, it is
* going to be added to messageObj as a ".msg"
* property (if messageObj is an object) or
* concatenated to messageObj (if it's not an
* object).
*
* Those types of logs are possible :
*
* - log.info("a simple text message");
* - log.info({"name": "an object"});
* - log.info({"name": "an object..."}, "... and an extra text message");
* - log.info(err, "a catched error and an explanation message");public
*/
info(messageObj: any, txtMsg?: string): void;
/**
* Logs a WARNING level message.
*
* If the extra "txtMsg" parameter is set, it is
* going to be added to messageObj as a ".msg"
* property (if messageObj is an object) or
* concatenated to messageObj (if it's not an
* object).
*
* Those types of logs are possible :
*
* - log.warning("a simple text message");
* - log.warning({"name": "an object"});
* - log.warning({"name": "an object..."}, "... and an extra text message");
* - log.warning(err, "a catched error and an explanation mespublic sage");
*/
warning(messageObj: any, txtMsg?: string): void;
/**
* Logs an ERROR level message.
*
* If the extra "txtMsg" parameter is set, it is
* going to be added to messageObj as a ".msg"
* property (if messageObj is an object) or
* concatenated to messageObj (if it's not an
* object).
*
* Those types of logs are possible :
*
* - log.error("a simple text message");
* - log.error({"name": "an object"});
* - log.error({"name": "an object..."}, "... and an extra text message");
* - log.error(err, "a catched error and an explanatpublic ion message");
*/
error(messageObj: any, txtMsg?: string): void;
/**
* Logs a level specific message.
*
* If the extra "txtMsg" parameter is set, it is
* going to be added to messageObj as a ".msg"
* property (if messageObj is an object) or
* concatenated to messageObj (if it's not an
* object).
*
* Those types of logs are possible :
*
* - log(LogLevel.XXXXX, "a simple text message");
* - log({"name": "an object"});
* - log({"name": "an object..."}, "... and an extra text message");
* - log(err, "a catched error and an epublic xplanation message");
*/
log(level: LogLevel, messageObj: any, txtMsg?: string): void;
/**
* Update the logger based on the parent changes.
* Could use something more precise to handle specific event but
* people could use it to update the child independently from the parent,
* which is not what is intended.
*/
update(): void;
/**
* Adds the file and line number where the log occures.
* This particular code is required since our custom Logger
* is a layer over Pino and therefore adds an extra level
* to the error stack. Without this code, the file and line number
* are not the right ones.
*
* Based by http://stackoverflow.com/a/38197778/843699
*/
private enhanceLog;
}
//# sourceMappingURL=logger.d.ts.map