@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
262 lines (261 loc) • 9.07 kB
TypeScript
import { Logger as PinoLogger } from 'pino';
/**
* The common set of `Logger` options.
*/
export type LoggerOptions = {
/**
* The logger name.
*/
name: string;
/**
* The desired log level.
*/
level?: LoggerLevelValue;
/**
* Create a logger with the fields set
*/
fields?: Fields;
/** log to memory instead of to a file. Intended for Unit Testing */
useMemoryLogger?: boolean;
};
/**
* Standard `Logger` levels.
*
* **See** {@link https://getpino.io/#/docs/api?id=logger-level |Logger Levels}
*/
export declare enum LoggerLevel {
TRACE = 10,
DEBUG = 20,
INFO = 30,
WARN = 40,
ERROR = 50,
FATAL = 60
}
/**
* Any numeric `Logger` level.
*/
export type LoggerLevelValue = LoggerLevel | number;
/**
* An object
*/
export type Fields = Record<string, unknown>;
/**
* All possible field value types.
*/
export type FieldValue = string | number | boolean | Fields;
/**
* Log line interface
*/
export type LogLine = {
name: string;
hostname: string;
pid: string;
log: string;
level: number;
msg: string;
time: string;
v: number;
};
/**
* A logging abstraction powered by {@link https://github.com/pinojs/pino | Pino} that provides both a default
* logger configuration that will log to the default path, and a way to create custom loggers based on the same foundation.
*
* ```
* // Gets the root sfdx logger
* const logger = await Logger.root();
*
* // Creates a child logger of the root sfdx logger with custom fields applied
* const childLogger = await Logger.child('myRootChild', {tag: 'value'});
*
* // Creates a custom logger unaffiliated with the root logger
* const myCustomLogger = new Logger('myCustomLogger');
*
* // Creates a child of a custom logger unaffiliated with the root logger with custom fields applied
* const myCustomChildLogger = myCustomLogger.child('myCustomChild', {tag: 'value'});
*
* // get a raw pino logger from the root instance of Logger
* // you can use these to avoid constructing another Logger wrapper class and to get better type support
* const logger = Logger.getRawRootLogger().child({name: 'foo', otherProp: 'bar'});
* logger.info({some: 'stuff'}, 'a message');
*
*
* // get a raw pino logger from the current instance
* const childLogger = await Logger.child('myRootChild', {tag: 'value'});
* const logger = childLogger.getRawLogger();
* ```
*
* **See** https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_dev_cli_log_messages.htm
*/
export declare class Logger {
/**
* The name of the root sfdx `Logger`.
*/
static readonly ROOT_NAME = "sf";
/**
* The default `LoggerLevel` when constructing new `Logger` instances.
*/
static readonly DEFAULT_LEVEL = LoggerLevel.WARN;
/**
* A list of all lower case `LoggerLevel` names.
*
* **See** {@link LoggerLevel}
*/
static readonly LEVEL_NAMES: string[];
private static rootLogger?;
private pinoLogger;
private memoryLogger?;
/**
* Constructs a new `Logger`.
*
* @param optionsOrName A set of `LoggerOptions` or name to use with the default options.
*
* **Throws** *{@link SfError}{ name: 'RedundantRootLoggerError' }* More than one attempt is made to construct the root
* `Logger`.
*/
constructor(optionsOrName: LoggerOptions | string);
/**
*
* Gets the root logger. It's a singleton
* See also getRawLogger if you don't need the root logger
*/
static root(): Promise<Logger>;
/**
* Gets the root logger. It's a singleton
*/
static getRoot(): Logger;
/**
* Destroys the root `Logger`.
*
* @ignore
*/
static destroyRoot(): void;
/**
* Create a child of the root logger, inheriting this instance's configuration such as `level`, transports, etc.
*
* @param name The name of the child logger.
* @param fields Additional fields included in all log lines.
*/
static child(name: string, fields?: Fields): Promise<Logger>;
/**
* Create a child of the root logger, inheriting this instance's configuration such as `level`, transports, etc.
*
* @param name The name of the child logger.
* @param fields Additional fields included in all log lines.
*/
static childFromRoot(name: string, fields?: Fields): Logger;
/**
* Gets a numeric `LoggerLevel` value by string name.
*
* @param {string} levelName The level name to convert to a `LoggerLevel` enum value.
*
* **Throws** *{@link SfError}{ name: 'UnrecognizedLoggerLevelNameError' }* The level name was not case-insensitively recognized as a valid `LoggerLevel` value.
* @see {@Link LoggerLevel}
*/
static getLevelByName(levelName: string): LoggerLevelValue;
/** get the bare (pino) logger instead of using the class hierarchy */
static getRawRootLogger(): PinoLogger;
/** get the bare (pino) logger instead of using the class hierarchy */
getRawLogger(): PinoLogger;
/**
* Gets the name of this logger.
*/
getName(): string;
/**
* Gets the current level of this logger.
*/
getLevel(): LoggerLevelValue;
/**
* Set the logging level of all streams for this logger. If a specific `level` is not provided, this method will
* attempt to read it from the environment variable `SFDX_LOG_LEVEL`, and if not found,
* {@link Logger.DEFAULT_LOG_LEVEL} will be used instead. For convenience `this` object is returned.
*
* @param {LoggerLevelValue} [level] The logger level.
*
* **Throws** *{@link SfError}{ name: 'UnrecognizedLoggerLevelNameError' }* A value of `level` read from `SFDX_LOG_LEVEL`
* was invalid.
*
* ```
* // Sets the level from the environment or default value
* logger.setLevel()
*
* // Set the level from the INFO enum
* logger.setLevel(LoggerLevel.INFO)
*
* // Sets the level case-insensitively from a string value
* logger.setLevel(Logger.getLevelByName('info'))
* ```
*/
setLevel(level?: LoggerLevelValue): Logger;
/**
* Compares the requested log level with the current log level. Returns true if
* the requested log level is greater than or equal to the current log level.
*
* @param level The requested log level to compare against the currently set log level.
*/
shouldLog(level: LoggerLevelValue): boolean;
/**
* Gets an array of log line objects. Each element is an object that corresponds to a log line.
*/
getBufferedRecords(): LogLine[];
/**
* Reads a text blob of all the log lines contained in memory or the log file.
*/
readLogContentsAsText(): string;
/**
* Create a child logger, typically to add a few log record fields. For convenience this object is returned.
*
* @param name The name of the child logger that is emitted w/ log line. Will be prefixed with the parent logger name and `:`
* @param fields Additional fields included in all log lines for the child logger.
*/
child(name: string, fields?: Fields): Logger;
/**
* Add a field to all log lines for this logger. For convenience `this` object is returned.
*
* @param name The name of the field to add.
* @param value The value of the field to be logged.
*/
addField(name: string, value: FieldValue): Logger;
/**
* Logs at `trace` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
trace(...args: any[]): Logger;
/**
* Logs at `debug` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
debug(...args: unknown[]): Logger;
/**
* Logs at `debug` level with filtering applied.
*
* @param cb A callback that returns on array objects to be logged.
*/
debugCallback(cb: () => unknown[] | string): void;
/**
* Logs at `info` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
info(...args: unknown[]): Logger;
/**
* Logs at `warn` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
warn(...args: unknown[]): Logger;
/**
* Logs at `error` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
error(...args: unknown[]): Logger;
/**
* Logs at `fatal` level with filtering applied. For convenience `this` object is returned.
*
* @param args Any number of arguments to be logged.
*/
fatal(...args: unknown[]): Logger;
}
export declare const computeLevel: (optionsLevel?: number | string) => string;