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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 12x 12x 12x 12x 12x 12x 26x 12x 12x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 28x 28x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 38x 38x 38x 38x 38x 38x 38x 4x 4x 4x 4x 38x 38x 38x 38x 38x 38x 38x 38x 4x 4x 1x 1x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 76x 76x 266x 266x 266x 266x 76x 4x 4x 14x 14x 14x 14x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 76x 76x 76x 76x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 76x 76x 4x 4x 1x 1x 1x 1x 1x 1x 1x 76x 76x 76x 76x 76x 76x 1x 1x 1x 34x 34x 34x 20x 34x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 38x 38x 38x 38x 2x 2x 38x 38x 38x 38x 2x 2x 2x 2x 2x 2x 2x 38x 38x 38x 38x 38x 38x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 76x 76x 76x 4x 4x 4x 2x 2x 2x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 2x 38x 38x 38x 2x 2x 16x 16x 2x 14x 14x 2x 8x 8x 2x | import { ColorStringRE, isStream } from '@https-enable/utils'; import { styleText } from '@https-enable/colors'; import jsonStringify from 'safe-stable-stringify'; import ms$1 from 'ms'; import util, { inspect } from 'node:util'; import fecha from 'fecha'; import os from 'node:os'; class InvalidFormatError extends Error { // eslint-disable-next-line ts/no-unsafe-function-type constructor(formatFn) { super(`Format functions must be synchronous taking two arguments: (info, opts) Found: ${formatFn.toString().split("\n")[0]} `); Error.captureStackTrace(this, InvalidFormatError); } } const format = function(formatFn) { if (formatFn.length > 2) { throw new InvalidFormatError(formatFn); } class Format { options; constructor(options = {}) { this.options = options; } transform = formatFn; } function createFormatWrap(opts) { return new Format(opts); } createFormatWrap.Format = Format; return createFormatWrap; }; const cli$1 = { levels: { ERROR: 0, WARN: 1, HELP: 2, DATA: 3, INFO: 4, DEBUG: 5, PROMPT: 6, VERBOSE: 7, INPUT: 8, SILLY: 9 }}; const npm = { levels: { ERROR: 0, WARN: 1, INFO: 2, HTTP: 3, VERBOSE: 4, DEBUG: 5, SILLY: 6 }, colors: { ERROR: "red", WARN: "yellow", INFO: "green", HTTP: "green", VERBOSE: "cyan", DEBUG: "blue", SILLY: "magenta" } }; const configs = { cli: cli$1, npm}; const LEVEL = Symbol.for("level"); const MESSAGE = Symbol.for("message"); const SPLAT = Symbol.for("splat"); class Colorizer { static allColors = {}; options; constructor(opts = {}) { if (opts.colors) { this.addColors(opts.colors); } this.options = opts; } /** * 添加颜色配置到静态属性 */ static addColors(colors) { const nextColors = Object.entries(colors).reduce((acc, [level, spec]) => { acc[level] = typeof spec === "string" ? spec.split(/\s+/) : spec; return acc; }, {}); Colorizer.allColors = { ...Colorizer.allColors, ...nextColors }; return Colorizer.allColors; } /** * 实例方法添加颜色配置 */ addColors(colors) { return Colorizer.addColors(colors); } /** * 执行颜色处理的核心方法 */ colorize(lookup, level, message) { if (typeof message === "undefined") { message = level; } const colorSpec = Colorizer.allColors[lookup]; if (!colorSpec) { return String(message); } return styleText(colorSpec, message); } /** * 转换日志信息的入口方法 */ transform = (info, opts) => { const options = { ...this.options, ...opts }; const levelKey = info[LEVEL] || info.level; if (options.all && typeof info[MESSAGE] === "string") { info[MESSAGE] = this.colorize(levelKey, info.level, info[MESSAGE]); } if (options.level || options.all || !options.message) { info.level = this.colorize(levelKey, info.level); } if (options.all || options.message) { info.message = this.colorize(levelKey, info.level, info.message); } return info; }; } const colorize = (opts = {}) => new Colorizer(opts); const levels = (config) => { Colorizer.addColors(config.colors); return config; }; const align = format((info) => { info.message = ` ${info.message}`; return info; }); class Padder { paddings; options; constructor(opts = { levels: configs.npm.levels }) { this.paddings = Padder.paddingForLevels(opts.levels || configs.npm.levels, opts.filler); this.options = opts; } /** * 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) { const lvls = Object.keys(levels).map((level) => level.length); return Math.max(...lvls); } /** * 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, filler, maxLength) { const targetLen = maxLength + 1 - level.length; const rep = Math.floor(targetLen / filler.length); const padding = `${filler}${filler.repeat(rep)}`; return padding.slice(0, targetLen); } /** * 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, filler = " ") { const maxLength = Padder.getLongestLevel(levels); return Object.keys(levels).reduce((acc, level) => { acc[level] = Padder.paddingForLevel(level, filler, maxLength); return acc; }, {}); } /** * 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. */ // eslint-disable-next-line unused-imports/no-unused-vars transform = (info, opts = {}) => { const level = info[LEVEL]; const paddings = level ? this.paddings[level] : ""; info.message = `${paddings}${info.message}`; if (info[MESSAGE]) { info[MESSAGE] = `${paddings}${info[MESSAGE]}`; } return info; }; } const padLevels = (opts) => new Padder(opts); class CliFormat { colorizer; padder; options; constructor(opts = {}) { if (!opts.levels) { opts.levels = configs.cli.levels; } this.colorizer = new Colorizer(opts); this.padder = new Padder(opts); this.options = opts; } /* * function transform (info, opts) * Attempts to both: * 1. Pad the { level } * 2. Colorize the { level, message } * of the given `logform` info object depending on the `opts`. */ transform = (info, opts) => { this.colorizer.transform(this.padder.transform(info, opts), opts); info[MESSAGE] = `${info.level}:${info.message}`; return info; }; } const cli = (opts) => new CliFormat(opts); function cascade(formats) { if (!formats.every(isValidFormat)) { return; } return (info) => { let obj = info; for (let i = 0; i < formats.length; i++) { const format2 = formats[i]; const temp = format2.transform(obj, format2.options); obj = temp; } return obj; }; } function isValidFormat(fmt) { if (typeof fmt.transform !== "function") { throw new TypeError([ "No transform function found on format. Did you create a format instance?", "const myFormat = format(formatFn);", "const instance = myFormat();" ].join("\n")); } return true; } function combine(...formats) { const combinedFormat = format(cascade(formats)); const instance = combinedFormat(); Object.defineProperty(instance, "Format", { value: combinedFormat.Format }); return instance; } const errors = format((einfo, { stack, cause } = {}) => { if (einfo instanceof Error) { const info = { ...einfo, level: einfo.level, [LEVEL]: einfo[LEVEL] || einfo.level, message: einfo.message, [MESSAGE]: einfo[MESSAGE] || einfo.message }; stack && (info.stack = einfo.stack); cause && (info.cause = einfo.cause); return info; } if (!(einfo.message instanceof Error)) return einfo; const err = einfo.message; Object.assign(einfo, err); einfo.message = err.message; einfo[MESSAGE] = err.message; stack && (einfo.stack = err.stack); cause && (einfo.cause = err.cause); return einfo; }); function replacer(key, value) { if (typeof value === "bigint") { return value.toString(); } return value; } const json = format((info, opts = {}) => { const jsonStringify$1 = jsonStringify.configure(opts); info[MESSAGE] = jsonStringify$1(info, opts.replacer || replacer, opts.space); return info; }); const label = format((info, opts = {}) => { if (opts.message) { info.message = `[${opts.label}] ${info.message}`; return info; } info.label = opts.label; return info; }); const logstash = format((info) => { const logstash = {}; if (info.message) { logstash["@message"] = info.message; delete info.message; } if (info.timestamp) { logstash["@timestamp"] = info.timestamp; delete info.timestamp; } logstash["@fields"] = info; info[MESSAGE] = jsonStringify(logstash); return info; }); function fillExcept(info, fillExceptKeys, metadataKey) { const savedKeys = fillExceptKeys.reduce((acc, key) => { acc[key] = info[key]; delete info[key]; return acc; }, {}); const metadata = Object.keys(info).reduce((acc, key) => { acc[key] = info[key]; delete info[key]; return acc; }, {}); Object.assign(info, savedKeys, { [metadataKey]: metadata }); return info; } function fillWith(info, fillWithKeys, metadataKey) { info[metadataKey] = fillWithKeys.reduce((acc, key) => { acc[key] = info[key]; delete info[key]; return acc; }, {}); return info; } const metadata = format((info, opts = {}) => { const metadataKey = opts.key || "metadata"; let fillExceptKeys = []; if (!opts.fillExcept && !opts.fillWith) { fillExceptKeys.push("level"); fillExceptKeys.push("message"); } if (opts.fillExcept) { fillExceptKeys = opts.fillExcept; } if (fillExceptKeys.length > 0) { return fillExcept(info, fillExceptKeys, metadataKey); } if (opts.fillWith) { return fillWith(info, opts.fillWith, metadataKey); } return info; }); let prevTime; const ms = format((info) => { const curr = +/* @__PURE__ */ new Date(); const diff = curr - (prevTime || curr); prevTime = curr; info.ms = `+${ms$1(diff)}`; return info; }); const prettyPrint = format((info, opts = {}) => { const stripped = { ...info }; delete stripped[LEVEL]; delete stripped[MESSAGE]; delete stripped[SPLAT]; info[MESSAGE] = inspect(stripped, false, opts.depth || null, opts.colorize); return info; }); class Printf { template; constructor(templateFn) { this.template = templateFn; } transform = (info) => { info[MESSAGE] = this.template(info); return info; }; } const printf = (templateFn) => new Printf(templateFn); const simple = format((info) => { const stringifiedRest = jsonStringify({ ...info, level: void 0, message: void 0, splat: void 0 }); const padding = info.padding && info.padding[info.level] || ""; if (stringifiedRest && stringifiedRest !== "{}") { info[MESSAGE] = `${info.level}:${padding} ${info.message} ${stringifiedRest}`; } else { info[MESSAGE] = `${info.level}:${padding} ${info.message}`; } return info; }); const formatRegExp = /%[scdjifoO%]/g; const escapedPercent = /%%/g; class Splatter { constructor() { } /** * 检查 tokens <= splat.length,将 { splat, meta } 分配到 `info` 中,并写入此实例。 * * @param info Logform 信息消息。 * @param tokens 一组字符串插值标记。 * @returns 修改后的信息消息 * @private */ _splat(info, tokens) { const msg = info.message || ""; const splat = info[SPLAT] || info.splat || []; const percents = msg.match(escapedPercent); const escapes = percents ? percents.length : 0; const expectedSplat = tokens.length - escapes; const extraSplat = expectedSplat - splat.length; const metas = extraSplat < 0 ? splat.splice(extraSplat, -1 * extraSplat) : []; const metalen = metas.length; if (metalen) { for (let i = 0; i < metalen; i++) { Object.assign(info, metas[i]); } } info.message = util.format(msg, ...splat); return info; } /** * 使用 `util.format` 完成 `info.message` 提供的 `info` 消息。 * 如果没有标记,则 `info` 是不可变的。 * * @param info Logform 信息消息。 * @returns 修改后的信息消息 */ transform = (info) => { const msg = info.message || ""; const splat = info[SPLAT] || info.splat; if (!splat || !splat.length) { return info; } const tokens = msg.match(formatRegExp); if (!tokens && (splat || splat.length)) { const metas = splat.length > 1 ? splat.splice(0) : splat; const metalen = metas.length; if (metalen) { for (let i = 0; i < metalen; i++) { Object.assign(info, metas[i]); } } return info; } if (tokens) { return this._splat(info, tokens); } return info; }; } const splat = () => new Splatter(); const timestamp = format((info, opts = {}) => { if (opts.format) { info.timestamp = typeof opts.format === "function" ? opts.format() : fecha.format(/* @__PURE__ */ new Date(), opts.format); } if (!info.timestamp) { info.timestamp = (/* @__PURE__ */ new Date()).toISOString(); } if (opts.alias) { info[opts.alias] = info.timestamp; } return info; }); function stripColors(str) { return `${str}`.replace(/\x1B\[\d+m/g, ""); } const uncolorize = format((info, opts = {}) => { if (opts.level !== false) { info.level = stripColors(info.level); } if (opts.message !== false) { info.message = stripColors(String(info.message)); } if (opts.raw !== false && info[MESSAGE]) { info[MESSAGE] = stripColors(String(info[MESSAGE])); } return info; }); function exposeFormat(name, requireFormat) { Object.defineProperty(format, name, { get() { return requireFormat(); }, configurable: true }); } exposeFormat("align", () => align); exposeFormat("cli", () => cli); exposeFormat("colorize", () => colorize); exposeFormat("combine", () => combine); exposeFormat("errors", () => errors); exposeFormat("json", () => json); exposeFormat("label", () => label); exposeFormat("logstash", () => logstash); exposeFormat("metadata", () => metadata); exposeFormat("ms", () => ms); exposeFormat("padLevels", () => padLevels); exposeFormat("prettyPrint", () => prettyPrint); exposeFormat("printf", () => printf); exposeFormat("simple", () => simple); exposeFormat("splat", () => splat); exposeFormat("timestamp", () => timestamp); exposeFormat("uncolorize", () => uncolorize); class Transport { level; eol; name; levels; options; constructor(options) { this.options = options; this.name = options.name; this.level = options.level; this.levels = options.levels || levels(configs.npm).levels; this.eol = typeof options.eol === "string" ? options.eol : os.EOL; } // 判断是否满足日志级别要求 shouldLog(level, force = false) { level = level.replace(ColorStringRE, ""); const levels2 = Object.keys(this.levels).reverse(); return force || levels2.indexOf(level) >= levels2.indexOf(this.level || "INFO"); } // 使用局部format selfFormat(options) { if (this.options.format && options) { return this.options.format.transform(options); } } } class ConsoleTransport extends Transport { constructor(options = {}) { super(options); this.name = options.name || "console"; } log = (info, callback, options, force) => { info = this.selfFormat(options) || info; const data = { ...info }; if (!force && !this.shouldLog(data.level)) return callback?.(); console.log(`${data[MESSAGE]}`); callback?.(); }; } class StreamTransport extends Transport { stream; isObjectMode; constructor(options = {}) { super(options); if (!options.stream || !isStream(options.stream)) { throw new Error("options.stream is required."); } this.name = options.name || "console"; this.stream = options.stream; this.isObjectMode = options.stream._writableState?.objectMode || false; this.stream.setMaxListeners(Infinity); } log = (info, callback, options, force) => { if (!force && !this.shouldLog(info.level)) return callback?.(); const handleComplete = (error) => { if (error) return callback?.(error); callback?.(); }; try { info = this.selfFormat(options) || info; let payload; if (this.isObjectMode) payload = { ...info }; else payload = `${info[MESSAGE]}${this.eol}`; const canWrite = this.stream.write(payload, (error) => { handleComplete(error || void 0); }); if (!canWrite) { this.stream.once("drain", () => handleComplete()); } } catch (error) { handleComplete(error instanceof Error ? error : new Error(String(error))); } }; } const transports = { Console: ConsoleTransport, Stream: StreamTransport }; class Logger { level; levels; silent; transports = []; /** * 是否是隐式log * @description 隐式 log 全局兜底 * @default false */ isImplicit; format; constructor(options = {}) { this.level = options.level || "INFO"; this.levels = options.levels || levels(configs.npm).levels; this.silent = options.silent || false; this.isImplicit = options.isImplicit || false; this.format = options.format || Logger.createLogFormat(false); if (options.transports) { this.transports = Array.isArray(options.transports) ? options.transports : [options.transports]; } } static createLogFormat(colorize = false, label) { const colorizer = format.colorize(); const appLabel = !!label && format.label({ label, message: true }); const timestamp = format.timestamp({ format: "YYYY-MM-DD HH:mm:ss.SSS" }); const logFormat = format.printf((info) => { const levelWithoutColor = info.level.replace(ColorStringRE, ""); const space = " ".repeat(6 - levelWithoutColor.length); return `${info.timestamp ? info.timestamp.padEnd(info.timestamp.length + 1) : ""}${info.level}${space} ${info.message || ""}`; }); return colorize ? appLabel ? format.combine(colorizer, appLabel, timestamp, logFormat) : format.combine(colorizer, timestamp, logFormat) : appLabel ? format.combine(appLabel, timestamp, logFormat) : format.combine(timestamp, logFormat); } updateLogFormat = (colorize = false) => { this.format = Logger.createLogFormat(colorize); }; log(level, message, options = {}, callback) { if (options.force || this.shouldLog(level)) { const isImplicit = options.isImplicit ?? this.isImplicit; const isSilent = options.silent ?? this.silent; if (!options.force && isImplicit && !isSilent) { return callback?.(); } else if (options.force || !isSilent) { const transformInfo = this.format.transform({ level, message }); if (!transformInfo[LEVEL]) { transformInfo[LEVEL] = transformInfo.level; } if (!Object.keys(this.levels).includes(level)) { console.error("[winston] Unknown logger level: %s", level); console.error("[winston] levels: %j", this.levels); } if (!this.transports || (Array.isArray(this.transports) ? !this.transports.length : !this.transports)) { console.error( "[winston] Attempt to write logs with no transports, which can increase memory usage: %j", transformInfo ); } try { this.transports.forEach((transport) => { !transport.level && (transport.level = this.level); transport.log(transformInfo, callback, { level, message }, options.force); }); } finally { callback?.(); } return; } return callback?.(); } } shouldLog(level) { const levels2 = Object.keys(this.levels).reverse(); return levels2.indexOf(level) >= levels2.indexOf(this.level); } debug(message = "", options = {}) { this.log("DEBUG", message, options); } info(message = "", options = {}) { this.log("INFO", message, options); } warn(message = "", options = {}) { this.log("WARN", message, options); } error(message = "", options = {}) { this.log("ERROR", message, options); } } export { Logger, transports as Transports, Logger as default, format }; |