ihrz
Version:
iHrz in ts!
97 lines (87 loc) • 3.52 kB
JavaScript
/*
・ iHorizon Discord Bot (https://gitlab.com/ihrz/ihrz)
・ Licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
・ Under the following terms:
・ Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
・ NonCommercial — You may not use the material for commercial purposes.
・ ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
・ No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
・ Mainly developed by Kisakay (https://gitlab.com/Kisakay)
・ Copyright © 2020-2025 iHorizon
*/
import { log as _ } from 'console';
import "./functions/colors.js";
var LogLevel;
(function (LogLevel) {
LogLevel["LOG"] = "LOG";
LogLevel["WARN"] = "WRN";
LogLevel["ERROR"] = "ERR";
LogLevel["LEGACY"] = "LEG";
})(LogLevel || (LogLevel = {}));
function getCurrentTime() {
const now = new Date();
const shardId = global.client?.shard?.ids[0] ?? "X";
const timestamp = now.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
return `SHARD#${shardId} ${timestamp}`;
}
function formatMessage(level, message, ...optionalParams) {
const timestamp = getCurrentTime();
const prefix = `[${timestamp} ${level}]:`;
const coloredPrefix = applyColorToPrefix(prefix, level);
const messageStr = typeof message === 'object' ? JSON.stringify(message, null, 2) : String(message);
const paramsStr = optionalParams.length > 0
? ' ' + optionalParams.map(param => typeof param === 'object' ? JSON.stringify(param, null, 2) : String(param)).join(' ')
: '';
return coloredPrefix + ' ' + messageStr + paramsStr;
}
function applyColorToPrefix(prefix, level) {
switch (level) {
case LogLevel.LOG:
return prefix.green;
case LogLevel.WARN:
return prefix.yellow;
case LogLevel.ERROR:
return prefix.red;
case LogLevel.LEGACY:
return prefix.cyan;
default:
return prefix;
}
}
const logger = {
log(message, ...optionalParams) {
const formattedMessage = formatMessage(LogLevel.LOG, message, ...optionalParams);
_(formattedMessage);
},
warn(message, ...optionalParams) {
const formattedMessage = formatMessage(LogLevel.WARN, message, ...optionalParams);
_(formattedMessage);
},
err(message, ...optionalParams) {
const formattedMessage = formatMessage(LogLevel.ERROR, message, ...optionalParams);
_(formattedMessage);
if (typeof process !== 'undefined' && process.stderr) {
process.stderr.write(formattedMessage + '\n');
}
},
legacy(message, ...optionalParams) {
if (optionalParams.length > 0) {
_(message, ...optionalParams);
}
else {
_(message);
}
},
returnLog(message, ...optionalParams) {
return formatMessage(LogLevel.LOG, message, ...optionalParams);
}
};
export default logger;