All files / packages/logger/dist index.d.mts

0% Statements 0/0
0% Branches 1/1
0% Functions 1/1
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { FormatKeys } from '@https-enable/colors';
import { MapTuple, Prettify } from '@https-enable/types';
import stream from 'node:stream';
 
interface UncolorizeOptions {
    /**
     * Disables the uncolorize format for `info.level` if set to `false`.
     */
    level?: boolean;
    /**
     * Disables the uncolorize format for `info.message` if set to `false`.
     */
    message?: boolean;
    /**
     * Disables the uncolorize format for `info[MESSAGE]` if set to `false`.
     */
    raw?: boolean;
}
declare const _default$g: FormatWrap<UncolorizeOptions>;
 
interface TimestampOptions {
    /**
     * Either the format as a string accepted by the [fecha](https://github.com/taylorhakes/fecha)
     * module or a function that returns a formatted date. If no format is provided `new
     * Date().toISOString()` will be used.
     */
    format?: string | (() => string);
    /**
     * The name of an alias for the timestamp property, that will be added to the `info` object.
     */
    alias?: string;
}
declare const _default$f: FormatWrap<TimestampOptions>;
 
declare class Splatter {
    constructor();
    /**
     * 检查 tokens <= splat.length,将 { splat, meta } 分配到 `info` 中,并写入此实例。
     *
     * @param info Logform 信息消息。
     * @param tokens 一组字符串插值标记。
     * @returns 修改后的信息消息
     * @private
     */
    private _splat;
    /**
     * 使用 `util.format` 完成 `info.message` 提供的 `info` 消息。
     * 如果没有标记,则 `info` 是不可变的。
     *
     * @param info Logform 信息消息。
     * @returns 修改后的信息消息
     */
    transform: TransformFunction;
}
declare const _default$e: () => Splatter;
 
declare const _default$d: FormatWrap<Record<any, any>>;
 
type TemplateFn = (info: TransformableInfo) => any;
declare class Printf {
    template: TemplateFn;
    constructor(templateFn: TemplateFn);
    transform: TransformFunction;
}
declare const _default$c: (templateFn: TemplateFn) => Printf;
 
interface PrettyPrintOptions {
    /**
     * A `number` that specifies the maximum depth of the `info` object being stringified by
     * `util.inspect`. Defaults to `2`.
     */
    depth?: number;
    /**
     * Colorizes the message if set to `true`. Defaults to `false`.
     */
    colorize?: boolean;
}
declare const _default$b: FormatWrap<PrettyPrintOptions>;
 
interface PadLevelsOptions {
    /**
     * Log levels. Defaults to `configs.npm.levels` from [triple-beam](https://github.com/winstonjs/triple-beam)
     * module.
     */
    levels?: Record<string, number>;
    filler?: string;
}
declare class Padder {
    paddings: Record<string, string>;
    options: PadLevelsOptions;
    constructor(opts?: PadLevelsOptions);
    /**
     * Returns the maximum length of keys in the specified `levels` Object.
     * @param levels Set of all levels to calculate longest level against.
     * @returns Maximum length of the longest level string.
     */
    static getLongestLevel(levels: Record<string, any>): number;
    /**
     * Returns the padding for the specified `level` assuming that the
     * maximum length of all levels it's associated with is `maxLength`.
     * @param level Level to calculate padding for.
     * @param filler Repeatable text to use for padding.
     * @param maxLength Length of the longest level
     * @returns Padding string for the `level`
     */
    static paddingForLevel(level: string, filler: string, maxLength: number): string;
    /**
     * Returns an object with the string paddings for the given `levels`
     * using the specified `filler`.
     * @param levels Set of all levels to calculate padding for.
     * @param filler Repeatable text to use for padding.
     * @returns Mapping of level to desired padding.
     */
    static paddingForLevels(levels: Record<string, any>, filler?: string): Record<string, string>;
    /**
     * Prepends the padding onto the `message` based on the `LEVEL` of
     * the `info`. This is based on the behavior of `winston@2` which also
     * prepended the level onto the message.
     *
     * See: https://github.com/winstonjs/winston/blob/2.x/lib/winston/logger.js#L198-L201
     *
     * @param info Logform info object
     * @param opts Options passed along to this instance.
     * @returns Modified logform info object.
     */
    transform: TransformFunction<PadLevelsOptions>;
}
declare const _default$a: (opts: PadLevelsOptions) => Padder;
 
declare const _default$9: FormatWrap<Record<any, any>>;
 
interface MetadataOptions {
    /**
     * The name of the key used for the metadata object. Defaults to `metadata`.
     */
    key?: string;
    /**
     * An array of keys that should not be added to the metadata object.
     */
    fillExcept?: string[];
    /**
     * An array of keys that will be added to the metadata object.
     */
    fillWith?: string[];
}
/**
 * Adds in a "metadata" object to collect extraneous data, similar to the metadata
 * object in winston 2.x.
 */
declare const _default$8: FormatWrap<MetadataOptions>;
 
declare const _default$7: FormatWrap<Record<any, any>>;
 
interface LabelOptions {
    /**
     * A label to be added before the message.
     */
    label?: string;
    /**
     * If set to `true` the `label` will be added to `info.message`. If set to `false` the `label`
     * will be added as `info.label`.
     */
    message?: boolean;
}
declare const _default$6: FormatWrap<LabelOptions>;
 
interface JsonOptions {
    /**
     * A function that influences how the `info` is stringified.
     */
    replacer?: (this: any, key: string, value: any) => any;
    /**
     * The number of white space used to format the json.
     */
    space?: number;
    /**
     * If `true`, bigint values are converted to a number. Otherwise, they are ignored.
     * This option is ignored by default as Logform stringifies BigInt in the default replacer.
     * @default true
     */
    bigint?: boolean;
    /**
     * Defines the value for circular references.
     * Set to `undefined`, circular properties are not serialized (array entries are replaced with null).
     * Set to `Error`, to throw on circular references.
     * @default "[Circular]"
     */
    circularValue?: string | null | TypeErrorConstructor | ErrorConstructor;
    /**
     * If `true`, guarantee a deterministic key order instead of relying on the insertion order.
     * @default true
     */
    deterministic?: boolean;
    /**
     * Maximum number of entries to serialize per object (at least one).
     * The serialized output contains information about how many entries have not been serialized.
     * Ignored properties are counted as well (e.g., properties with symbol values).
     * Using the array replacer overrules this option.
     * @default Infinity
     */
    maximumBreadth?: number;
    /**
     * Maximum number of object nesting levels (at least 1) that will be serialized.
     * Objects at the maximum level are serialized as `"[Object]"` and arrays as `"[Array]"`.
     * @default Infinity
     */
    maximumDepth?: number;
}
declare const _default$5: FormatWrap<JsonOptions>;
 
interface ErrorsOptions {
    /**
     * If `true`, the `Error` object's `stack` property will be appended to the `info` object.
     */
    stack?: boolean;
    /**
     * If `true`, the `Error` object's `cause` property will be appended to the `info` object.
     */
    cause?: boolean;
}
declare const _default$4: FormatWrap<ErrorsOptions>;
 
declare const LEVEL: unique symbol;
declare const MESSAGE: unique symbol;
declare const SPLAT: unique symbol;
/**
 * 简易日志级别
 * @description 供外部使用
 */
declare const SimpleLevelTuple: readonly ["ERROR", "WARN", "INFO", "DEBUG"];
 
type ColorMap = Record<string, string | string[]>;
interface ColorizeOptions {
    /**
     * If set to `true` the color will be applied to the `level`.
     */
    level?: boolean;
    /**
     * If set to `true` the color will be applied to the `message` and `level`.
     */
    all?: boolean;
    /**
     * If set to `true` the color will be applied to the `message`.
     */
    message?: boolean;
    /**
     * An object containing the colors for the log levels. For example: `{ info: 'blue', error: 'red' }`.
     */
    colors?: Record<string, FormatKeys>;
}
/**
 * 颜色处理器类
 */
declare class Colorizer {
    static allColors: Record<string, FormatKeys[] | FormatKeys>;
    options: ColorizeOptions;
    constructor(opts?: ColorizeOptions);
    /**
     * 添加颜色配置到静态属性
     */
    static addColors(colors: ColorMap): Record<string, FormatKeys | FormatKeys[]>;
    /**
     * 实例方法添加颜色配置
     */
    addColors(colors: ColorMap): Record<string, FormatKeys | FormatKeys[]>;
    /**
     * 执行颜色处理的核心方法
     */
    private colorize;
    /**
     * 转换日志信息的入口方法
     */
    transform: TransformFunction<ColorizeOptions>;
}
declare const _default$3: (opts?: ColorizeOptions) => Colorizer;
 
type Format = InstanceType<format['Format']>;
declare function combine(...formats: Format[]): FormatClass<Record<any, any>>;
 
type CliFormatOptions = ColorizeOptions & PadLevelsOptions;
/**
 * Cli format class that handles initial state for a separate
 * Colorizer and Padder instance.
 */
declare class CliFormat {
    colorizer: Colorizer;
    padder: Padder;
    options: CliFormatOptions;
    constructor(opts?: CliFormatOptions);
    transform: TransformFunction<CliFormatOptions>;
}
declare const _default$2: (opts: CliFormatOptions) => CliFormat;
 
declare const _default$1: FormatWrap<Record<any, any>>;
 
interface TransformableInfo {
  level: string
  message?: string
  [LEVEL]?: string
  [MESSAGE]?: any
  [SPLAT]?: any
  [key: string | symbol]: any
}
 
type TransformFunction<T extends Record<any, any> = Record<any, any>> = (info: TransformableInfo, opts?: T) => TransformableInfo
 
declare class FormatClass<T extends Record<any, any> = Record<any, any>> {
  constructor(options: T = {} as T)
  options?: T
  transform: TransformFunction<T>
}
 
interface FormatWrap<T extends Record<any, any>> {
  (opts?: T): FormatClass<T>
  Format: typeof FormatClass<T>
}
 
interface format<T extends Record<any, any> = Record<any, any>> {
  <P extends T>(transform: TransformFunction<P>): FormatWrap<P>
  Format: typeof FormatClass<T>
  align: typeof _default$1
  cli: typeof _default$2
  combine: typeof combine
  colorize: typeof _default$3
  errors: typeof _default$4
  json: typeof _default$5
  label: typeof _default$6
  logstash: typeof _default$7
  metadata: typeof _default$8
  ms: typeof _default$9
  padLevels: typeof _default$a
  prettyPrint: typeof _default$b
  printf: typeof _default$c
  simple: typeof _default$d
  splat: typeof _default$e
  timestamp: typeof _default$f
  uncolorize: typeof _default$g
}
 
declare const _default: format;
 
declare abstract class Transport<T extends TransportBaseOptions> {
    level?: LogEntry['level'];
    protected eol?: string;
    protected name?: string;
    protected levels: Record<string, number>;
    protected options: T;
    constructor(options: T);
    protected shouldLog(level: string, force?: boolean): boolean;
    protected selfFormat(options?: {
        level: LogEntry['level'];
        message: string;
    }): TransformableInfo | undefined;
    abstract log(info: TransformableInfo, callback?: TransportErrorCallback, options?: {
        level: LogEntry['level'];
        message: string;
    }, force?: boolean): void;
}
 
interface TransportErrorCallback {
  (error?: Error): void
}
 
type TransportInstance<T extends TransportBaseOptions = object> = InstanceType<typeof Transport<T>>
 
type TransportInstanceLog<T extends TransportBaseOptions = object> = TransportInstance<T>['log']
 
interface TransportBaseOptions {
  name?: string
  level?: LogEntry['level']
  levels?: Record<keyof LogLevel, number>
  format?: FormatClass
  eol?: string
}
 
interface ConsoleTransportOptions extends TransportBaseOptions {
}
 
interface WritableStream extends stream.Writable {
  _writableState?: stream.StreamOptions<WritableStream>
}
 
interface StreamTransportOptions extends TransportBaseOptions {
  stream?: WritableStream
}
 
type LogLevel = MapTuple<typeof SimpleLevelTuple>
 
type LogLevelKey = keyof LogLevel
 
interface LoggerOptions {
  level?: LogLevelKey
  levels?: Record<LogLevelKey, number>
  /** 静音 */
  silent?: boolean
  /** 是否是隐式log */
  isImplicit?: boolean
  format?: FormatClass
  transports?: TransportInstance[] | TransportInstance
}
 
interface LogEntry {
  level: LogLevelKey
  message: string
  [optionName: string]: any
}
 
interface LogOptions {
  /** 是否强制输出 | 无视 level 和 isImplicit 和 silent */
  force?: boolean
  /**
   * 是否是隐式log
   * @description 优先级最高,只有为undefined时才会遵从父级配置
   */
  isImplicit?: boolean
  /** 静音 */
  silent?: boolean
}
 
declare class ConsoleTransport extends Transport<ConsoleTransportOptions> {
    constructor(options?: ConsoleTransportOptions);
    log: TransportInstanceLog;
}
 
declare class StreamTransport extends Transport<StreamTransportOptions> {
    stream: NodeJS.WritableStream;
    isObjectMode: boolean;
    constructor(options?: StreamTransportOptions);
    log: TransportInstanceLog;
}
 
declare const transports: {
    Console: typeof ConsoleTransport;
    Stream: typeof StreamTransport;
};
 
declare class Logger {
    private level;
    private levels;
    private silent;
    private transports;
    /**
     * 是否是隐式log
     * @description 隐式 log 全局兜底
     * @default false
     */
    private isImplicit;
    format: ReturnType<typeof Logger.createLogFormat>;
    constructor(options?: Prettify<LoggerOptions>);
    static createLogFormat(colorize?: boolean, label?: string): FormatClass<Record<any, any>>;
    updateLogFormat: (colorize?: boolean) => void;
    private log;
    private shouldLog;
    debug(message?: string, options?: Prettify<LogOptions>): void;
    info(message?: string, options?: Prettify<LogOptions>): void;
    warn(message?: string, options?: Prettify<LogOptions>): void;
    error(message?: string, options?: Prettify<LogOptions>): void;
}
 
export { Logger, transports as Transports, Logger as default, _default as format };