1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.logger = void 0;
|
23 | const colors = __importStar(require("kleur/colors"));
|
24 | const levels = {
|
25 | debug: 20,
|
26 | info: 30,
|
27 | warn: 40,
|
28 | error: 50,
|
29 | silent: 90,
|
30 | };
|
31 |
|
32 | class SnowpackLogger {
|
33 | constructor() {
|
34 |
|
35 | this.level = 'info';
|
36 |
|
37 | this.logCount = 500;
|
38 | this.history = [];
|
39 | this.callbacks = {
|
40 | debug: (message) => {
|
41 | console.log(message);
|
42 | },
|
43 | info: (message) => {
|
44 | console.log(message);
|
45 | },
|
46 | warn: (message) => {
|
47 | console.warn(message);
|
48 | },
|
49 | error: (message) => {
|
50 | console.error(message);
|
51 | },
|
52 | };
|
53 | }
|
54 | log({ level, name, message, task, }) {
|
55 |
|
56 | if (levels[this.level] > levels[level]) {
|
57 | return;
|
58 | }
|
59 |
|
60 | let text = message;
|
61 | if (level === 'warn')
|
62 | text = colors.yellow(text);
|
63 | if (level === 'error')
|
64 | text = colors.red(text);
|
65 | const log = `${colors.dim(`[${name}]`)} ${text}`;
|
66 |
|
67 | const lastHistoryItem = this.history[this.history.length - 1];
|
68 | if (lastHistoryItem && lastHistoryItem.val === log) {
|
69 | lastHistoryItem.count++;
|
70 | }
|
71 | else {
|
72 | this.history.push({ val: log, count: 1 });
|
73 | }
|
74 | while (this.history.length > this.logCount) {
|
75 | this.history.shift();
|
76 | }
|
77 |
|
78 | if (typeof this.callbacks[level] === 'function') {
|
79 | this.callbacks[level](log);
|
80 | }
|
81 | else {
|
82 | throw new Error(`No logging method defined for ${level}`);
|
83 | }
|
84 |
|
85 |
|
86 | task && task(this);
|
87 | }
|
88 |
|
89 | debug(message, options) {
|
90 | const name = (options && options.name) || 'snowpack';
|
91 | this.log({ level: 'debug', name, message, task: options === null || options === void 0 ? void 0 : options.task });
|
92 | }
|
93 |
|
94 | info(message, options) {
|
95 | const name = (options && options.name) || 'snowpack';
|
96 | this.log({ level: 'info', name, message, task: options === null || options === void 0 ? void 0 : options.task });
|
97 | }
|
98 |
|
99 | warn(message, options) {
|
100 | const name = (options && options.name) || 'snowpack';
|
101 | this.log({ level: 'warn', name, message, task: options === null || options === void 0 ? void 0 : options.task });
|
102 | }
|
103 |
|
104 | error(message, options) {
|
105 | const name = (options && options.name) || 'snowpack';
|
106 | this.log({ level: 'error', name, message, task: options === null || options === void 0 ? void 0 : options.task });
|
107 | }
|
108 |
|
109 | getHistory() {
|
110 | return this.history;
|
111 | }
|
112 |
|
113 | on(event, callback) {
|
114 | this.callbacks[event] = callback;
|
115 | }
|
116 | }
|
117 |
|
118 | exports.logger = new SnowpackLogger();
|