UNPKG

10.8 kBTypeScriptView Raw
1import { Writable } from 'stream';
2/**
3 * A Bunyan `Serializer` function.
4 * @param input The input to be serialized.
5 * **See** {@link https://github.com/forcedotcom/node-bunyan#serializers|Bunyan Serializers API}
6 */
7export declare type Serializer = (input: any) => any;
8/**
9 * A collection of named `Serializer`s.
10 *
11 * **See** {@link https://github.com/forcedotcom/node-bunyan#serializers|Bunyan Serializers API}
12 */
13export interface Serializers {
14 [key: string]: Serializer;
15}
16/**
17 * The common set of `Logger` options.
18 */
19export interface LoggerOptions {
20 /**
21 * The logger name.
22 */
23 name: string;
24 /**
25 * The logger's serializers.
26 */
27 serializers?: Serializers;
28 /**
29 * Whether or not to log source file, line, and function information.
30 */
31 src?: boolean;
32 /**
33 * The desired log level.
34 */
35 level?: LoggerLevelValue;
36 /**
37 * A stream to write to.
38 */
39 stream?: Writable;
40 /**
41 * An array of streams to write to.
42 */
43 streams?: LoggerStream[];
44}
45/**
46 * Standard `Logger` levels.
47 *
48 * **See** {@link https://github.com/forcedotcom/node-bunyan#levels|Bunyan Levels}
49 */
50export declare enum LoggerLevel {
51 TRACE = 10,
52 DEBUG = 20,
53 INFO = 30,
54 WARN = 40,
55 ERROR = 50,
56 FATAL = 60
57}
58/**
59 * A Bunyan stream configuration.
60 *
61 * @see {@link https://github.com/forcedotcom/node-bunyan#streams|Bunyan Streams}
62 */
63export interface LoggerStream {
64 /**
65 * The type of stream -- may be inferred from other properties.
66 */
67 type?: string;
68 /**
69 * The desired log level for the stream.
70 */
71 level?: LoggerLevelValue;
72 /**
73 * The stream to write to. Mutually exclusive with `path`.
74 */
75 stream?: Writable;
76 /**
77 * The name of the stream.
78 */
79 name?: string;
80 /**
81 * A log file path to write to. Mutually exclusive with `stream`.
82 */
83 path?: string;
84 [key: string]: any;
85}
86/**
87 * Any numeric `Logger` level.
88 */
89export declare type LoggerLevelValue = LoggerLevel | number;
90/**
91 * A collection of named `FieldValue`s.
92 *
93 * **See** {@link https://github.com/forcedotcom/node-bunyan#log-record-fields|Bunyan Log Record Fields}
94 */
95export interface Fields {
96 [key: string]: FieldValue;
97}
98/**
99 * All possible field value types.
100 */
101export declare type FieldValue = string | number | boolean;
102/**
103 * Log line interface
104 */
105export interface LogLine {
106 name: string;
107 hostname: string;
108 pid: string;
109 log: string;
110 level: number;
111 msg: string;
112 time: string;
113 v: number;
114}
115/**
116 * A logging abstraction powered by {@link https://github.com/forcedotcom/node-bunyan|Bunyan} that provides both a default
117 * logger configuration that will log to `sfdx.log`, and a way to create custom loggers based on the same foundation.
118 *
119 * ```
120 * // Gets the root sfdx logger
121 * const logger = await Logger.root();
122 *
123 * // Creates a child logger of the root sfdx logger with custom fields applied
124 * const childLogger = await Logger.child('myRootChild', {tag: 'value'});
125 *
126 * // Creates a custom logger unaffiliated with the root logger
127 * const myCustomLogger = new Logger('myCustomLogger');
128 *
129 * // Creates a child of a custom logger unaffiliated with the root logger with custom fields applied
130 * const myCustomChildLogger = myCustomLogger.child('myCustomChild', {tag: 'value'});
131 * ```
132 * **See** https://github.com/forcedotcom/node-bunyan
133 *
134 * **See** https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_cli_log_messages.htm
135 */
136export declare class Logger {
137 /**
138 * The name of the root sfdx `Logger`.
139 */
140 static readonly ROOT_NAME = "sfdx";
141 /**
142 * The default `LoggerLevel` when constructing new `Logger` instances.
143 */
144 static readonly DEFAULT_LEVEL = LoggerLevel.WARN;
145 /**
146 * A list of all lower case `LoggerLevel` names.
147 *
148 * **See** {@link LoggerLevel}
149 */
150 static readonly LEVEL_NAMES: string[];
151 /**
152 * Gets the root logger with the default level and file stream.
153 */
154 static root(): Promise<Logger>;
155 /**
156 * Destroys the root `Logger`.
157 *
158 * @ignore
159 */
160 static destroyRoot(): void;
161 /**
162 * Create a child of the root logger, inheriting this instance's configuration such as `level`, `streams`, etc.
163 *
164 * @param name The name of the child logger.
165 * @param fields Additional fields included in all log lines.
166 */
167 static child(name: string, fields?: Fields): Promise<Logger>;
168 /**
169 * Gets a numeric `LoggerLevel` value by string name.
170 *
171 * @param {string} levelName The level name to convert to a `LoggerLevel` enum value.
172 *
173 * **Throws** *{@link SfdxError}{ name: 'UnrecognizedLoggerLevelName' }* The level name was not case-insensitively recognized as a valid `LoggerLevel` value.
174 * @see {@Link LoggerLevel}
175 */
176 static getLevelByName(levelName: string): LoggerLevelValue;
177 private static readonly lifecycle;
178 private static rootLogger?;
179 private bunyan;
180 /**
181 * Constructs a new `Logger`.
182 *
183 * @param optionsOrName A set of `LoggerOptions` or name to use with the default options.
184 *
185 * **Throws** *{@link SfdxError}{ name: 'RedundantRootLogger' }* More than one attempt is made to construct the root
186 * `Logger`.
187 */
188 constructor(optionsOrName: LoggerOptions | string);
189 /**
190 * Adds a stream.
191 *
192 * @param stream The stream configuration to add.
193 * @param defaultLevel The default level of the stream.
194 */
195 addStream(stream: LoggerStream, defaultLevel?: LoggerLevelValue): void;
196 /**
197 * Adds a file stream to this logger. Resolved or rejected upon completion of the addition.
198 *
199 * @param logFile The path to the log file. If it doesn't exist it will be created.
200 */
201 addLogFileStream(logFile: string): Promise<void>;
202 /**
203 * Gets the name of this logger.
204 */
205 getName(): string;
206 /**
207 * Gets the current level of this logger.
208 */
209 getLevel(): LoggerLevelValue;
210 /**
211 * Set the logging level of all streams for this logger. If a specific `level` is not provided, this method will
212 * attempt to read it from the environment variable `SFDX_LOG_LEVEL`, and if not found,
213 * {@link Logger.DEFAULT_LOG_LEVEL} will be used instead. For convenience `this` object is returned.
214 *
215 * @param {LoggerLevelValue} [level] The logger level.
216 *
217 * **Throws** *{@link SfdxError}{ name: 'UnrecognizedLoggerLevelName' }* A value of `level` read from `SFDX_LOG_LEVEL`
218 * was invalid.
219 *
220 * ```
221 * // Sets the level from the environment or default value
222 * logger.setLevel()
223 *
224 * // Set the level from the INFO enum
225 * logger.setLevel(LoggerLevel.INFO)
226 *
227 * // Sets the level case-insensitively from a string value
228 * logger.setLevel(Logger.getLevelByName('info'))
229 * ```
230 */
231 setLevel(level?: LoggerLevelValue): Logger;
232 /**
233 * Gets the underlying Bunyan logger.
234 */
235 getBunyanLogger(): any;
236 /**
237 * Compares the requested log level with the current log level. Returns true if
238 * the requested log level is greater than or equal to the current log level.
239 *
240 * @param level The requested log level to compare against the currently set log level.
241 */
242 shouldLog(level: LoggerLevelValue): boolean;
243 /**
244 * Use in-memory logging for this logger instance instead of any parent streams. Useful for testing.
245 * For convenience this object is returned.
246 *
247 * **WARNING: This cannot be undone for this logger instance.**
248 */
249 useMemoryLogging(): Logger;
250 /**
251 * Gets an array of log line objects. Each element is an object that corresponds to a log line.
252 */
253 getBufferedRecords(): LogLine[];
254 /**
255 * Reads a text blob of all the log lines contained in memory or the log file.
256 */
257 readLogContentsAsText(): string;
258 /**
259 * Adds a filter to be applied to all logged messages.
260 *
261 * @param filter A function with signature `(...args: any[]) => any[]` that transforms log message arguments.
262 */
263 addFilter(filter: (...args: unknown[]) => unknown): void;
264 /**
265 * Close the logger, including any streams, and remove all listeners.
266 *
267 * @param fn A function with signature `(stream: LoggerStream) => void` to call for each stream with
268 * the stream as an arg.
269 */
270 close(fn?: (stream: LoggerStream) => void): void;
271 /**
272 * Create a child logger, typically to add a few log record fields. For convenience this object is returned.
273 *
274 * @param name The name of the child logger that is emitted w/ log line as `log:<name>`.
275 * @param fields Additional fields included in all log lines for the child logger.
276 */
277 child(name: string, fields?: Fields): Logger;
278 /**
279 * Add a field to all log lines for this logger. For convenience `this` object is returned.
280 *
281 * @param name The name of the field to add.
282 * @param value The value of the field to be logged.
283 */
284 addField(name: string, value: FieldValue): Logger;
285 /**
286 * Logs at `trace` level with filtering applied. For convenience `this` object is returned.
287 *
288 * @param args Any number of arguments to be logged.
289 */
290 trace(...args: any[]): Logger;
291 /**
292 * Logs at `debug` level with filtering applied. For convenience `this` object is returned.
293 *
294 * @param args Any number of arguments to be logged.
295 */
296 debug(...args: unknown[]): Logger;
297 /**
298 * Logs at `debug` level with filtering applied.
299 *
300 * @param cb A callback that returns on array objects to be logged.
301 */
302 debugCallback(cb: () => unknown[] | string): void;
303 /**
304 * Logs at `info` level with filtering applied. For convenience `this` object is returned.
305 *
306 * @param args Any number of arguments to be logged.
307 */
308 info(...args: unknown[]): Logger;
309 /**
310 * Logs at `warn` level with filtering applied. For convenience `this` object is returned.
311 *
312 * @param args Any number of arguments to be logged.
313 */
314 warn(...args: unknown[]): Logger;
315 /**
316 * Logs at `error` level with filtering applied. For convenience `this` object is returned.
317 *
318 * @param args Any number of arguments to be logged.
319 */
320 error(...args: unknown[]): Logger;
321 /**
322 * Logs at `fatal` level with filtering applied. For convenience `this` object is returned.
323 *
324 * @param args Any number of arguments to be logged.
325 */
326 fatal(...args: unknown[]): Logger;
327 private applyFilters;
328 private uncaughtExceptionHandler;
329 private exitHandler;
330}