Spaces:
Paused
Paused
var __defProp = Object.defineProperty; | |
var __export = (target, all) => { | |
for (var name in all) | |
__defProp(target, name, { get: all[name], enumerable: true }); | |
}; | |
// src/index.ts | |
import { isNodeProcess } from "is-node-process"; | |
import { format } from "outvariant"; | |
// src/colors.ts | |
var colors_exports = {}; | |
__export(colors_exports, { | |
blue: () => blue, | |
gray: () => gray, | |
green: () => green, | |
red: () => red, | |
yellow: () => yellow | |
}); | |
function yellow(text) { | |
return `\x1B[33m${text}\x1B[0m`; | |
} | |
function blue(text) { | |
return `\x1B[34m${text}\x1B[0m`; | |
} | |
function gray(text) { | |
return `\x1B[90m${text}\x1B[0m`; | |
} | |
function red(text) { | |
return `\x1B[31m${text}\x1B[0m`; | |
} | |
function green(text) { | |
return `\x1B[32m${text}\x1B[0m`; | |
} | |
// src/index.ts | |
var IS_NODE = isNodeProcess(); | |
var Logger = class { | |
constructor(name) { | |
this.name = name; | |
this.prefix = `[${this.name}]`; | |
const LOGGER_NAME = getVariable("DEBUG"); | |
const LOGGER_LEVEL = getVariable("LOG_LEVEL"); | |
const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME); | |
if (isLoggingEnabled) { | |
this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug; | |
this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info; | |
this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success; | |
this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning; | |
this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error; | |
} else { | |
this.info = noop; | |
this.success = noop; | |
this.warning = noop; | |
this.error = noop; | |
this.only = noop; | |
} | |
} | |
prefix; | |
extend(domain) { | |
return new Logger(`${this.name}:${domain}`); | |
} | |
/** | |
* Print a debug message. | |
* @example | |
* logger.debug('no duplicates found, creating a document...') | |
*/ | |
debug(message, ...positionals) { | |
this.logEntry({ | |
level: "debug", | |
message: gray(message), | |
positionals, | |
prefix: this.prefix, | |
colors: { | |
prefix: "gray" | |
} | |
}); | |
} | |
/** | |
* Print an info message. | |
* @example | |
* logger.info('start parsing...') | |
*/ | |
info(message, ...positionals) { | |
this.logEntry({ | |
level: "info", | |
message, | |
positionals, | |
prefix: this.prefix, | |
colors: { | |
prefix: "blue" | |
} | |
}); | |
const performance2 = new PerformanceEntry(); | |
return (message2, ...positionals2) => { | |
performance2.measure(); | |
this.logEntry({ | |
level: "info", | |
message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`, | |
positionals: positionals2, | |
prefix: this.prefix, | |
colors: { | |
prefix: "blue" | |
} | |
}); | |
}; | |
} | |
/** | |
* Print a success message. | |
* @example | |
* logger.success('successfully created document') | |
*/ | |
success(message, ...positionals) { | |
this.logEntry({ | |
level: "info", | |
message, | |
positionals, | |
prefix: `\u2714 ${this.prefix}`, | |
colors: { | |
timestamp: "green", | |
prefix: "green" | |
} | |
}); | |
} | |
/** | |
* Print a warning. | |
* @example | |
* logger.warning('found legacy document format') | |
*/ | |
warning(message, ...positionals) { | |
this.logEntry({ | |
level: "warning", | |
message, | |
positionals, | |
prefix: `\u26A0 ${this.prefix}`, | |
colors: { | |
timestamp: "yellow", | |
prefix: "yellow" | |
} | |
}); | |
} | |
/** | |
* Print an error message. | |
* @example | |
* logger.error('something went wrong') | |
*/ | |
error(message, ...positionals) { | |
this.logEntry({ | |
level: "error", | |
message, | |
positionals, | |
prefix: `\u2716 ${this.prefix}`, | |
colors: { | |
timestamp: "red", | |
prefix: "red" | |
} | |
}); | |
} | |
/** | |
* Execute the given callback only when the logging is enabled. | |
* This is skipped in its entirety and has no runtime cost otherwise. | |
* This executes regardless of the log level. | |
* @example | |
* logger.only(() => { | |
* logger.info('additional info') | |
* }) | |
*/ | |
only(callback) { | |
callback(); | |
} | |
createEntry(level, message) { | |
return { | |
timestamp: /* @__PURE__ */ new Date(), | |
level, | |
message | |
}; | |
} | |
logEntry(args) { | |
const { | |
level, | |
message, | |
prefix, | |
colors: customColors, | |
positionals = [] | |
} = args; | |
const entry = this.createEntry(level, message); | |
const timestampColor = customColors?.timestamp || "gray"; | |
const prefixColor = customColors?.prefix || "gray"; | |
const colorize = { | |
timestamp: colors_exports[timestampColor], | |
prefix: colors_exports[prefixColor] | |
}; | |
const write = this.getWriter(level); | |
write( | |
[colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "), | |
...positionals.map(serializeInput) | |
); | |
} | |
formatTimestamp(timestamp) { | |
return `${timestamp.toLocaleTimeString( | |
"en-GB" | |
)}:${timestamp.getMilliseconds()}`; | |
} | |
getWriter(level) { | |
switch (level) { | |
case "debug": | |
case "success": | |
case "info": { | |
return log; | |
} | |
case "warning": { | |
return warn; | |
} | |
case "error": { | |
return error; | |
} | |
} | |
} | |
}; | |
var PerformanceEntry = class { | |
startTime; | |
endTime; | |
deltaTime; | |
constructor() { | |
this.startTime = performance.now(); | |
} | |
measure() { | |
this.endTime = performance.now(); | |
const deltaTime = this.endTime - this.startTime; | |
this.deltaTime = deltaTime.toFixed(2); | |
} | |
}; | |
var noop = () => void 0; | |
function log(message, ...positionals) { | |
if (IS_NODE) { | |
process.stdout.write(format(message, ...positionals) + "\n"); | |
return; | |
} | |
console.log(message, ...positionals); | |
} | |
function warn(message, ...positionals) { | |
if (IS_NODE) { | |
process.stderr.write(format(message, ...positionals) + "\n"); | |
return; | |
} | |
console.warn(message, ...positionals); | |
} | |
function error(message, ...positionals) { | |
if (IS_NODE) { | |
process.stderr.write(format(message, ...positionals) + "\n"); | |
return; | |
} | |
console.error(message, ...positionals); | |
} | |
function getVariable(variableName) { | |
if (IS_NODE) { | |
return process.env[variableName]; | |
} | |
return globalThis[variableName]?.toString(); | |
} | |
function isDefinedAndNotEquals(value, expected) { | |
return value !== void 0 && value !== expected; | |
} | |
function serializeInput(message) { | |
if (typeof message === "undefined") { | |
return "undefined"; | |
} | |
if (message === null) { | |
return "null"; | |
} | |
if (typeof message === "string") { | |
return message; | |
} | |
if (typeof message === "object") { | |
return JSON.stringify(message); | |
} | |
return message.toString(); | |
} | |
export { | |
Logger | |
}; | |