UNPKG

4.49 kBJavaScriptView Raw
1"use strict";
2var __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}));
9var __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});
14var __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};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.logger = void 0;
23const colors = __importStar(require("kleur/colors"));
24const levels = {
25 debug: 20,
26 info: 30,
27 warn: 40,
28 error: 50,
29 silent: 90,
30};
31/** Custom logger heavily-inspired by https://github.com/pinojs/pino with extra features like log retentian */
32class SnowpackLogger {
33 constructor() {
34 /** set the log level (can be changed after init) */
35 this.level = 'info';
36 /** configure maximum number of logs to keep (default: 500) */
37 this.logCount = 500;
38 this.history = []; // this is immutable; must be accessed with Logger.getHistory()
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 // test if this level is enabled or not
56 if (levels[this.level] > levels[level]) {
57 return; // do nothing
58 }
59 // format
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 // add to log history and remove old logs to keep memory low
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 // log
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 // logger takes a possibly processor-intensive task, and only
85 // processes it when this log level is enabled
86 task && task(this);
87 }
88 /** emit messages only visible when --debug is passed */
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 /** emit general info */
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 /** emit non-fatal warnings */
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 /** emit critical error messages */
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 /** get full logging history */
109 getHistory() {
110 return this.history;
111 }
112 /** listen for events */
113 on(event, callback) {
114 this.callbacks[event] = callback;
115 }
116}
117/** export one logger to rest of app */
118exports.logger = new SnowpackLogger();