UNPKG

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