Spaces:
Build error
Build error
File size: 4,245 Bytes
0bfe2e3 |
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 |
import winston from 'winston';
import moment from 'moment-timezone';
import { Env } from './env';
// Map log levels to their full names
const levelMap: { [key: string]: string } = {
error: 'ERROR',
warn: 'WARNING',
info: 'INFO',
debug: 'DEBUG',
verbose: 'VERBOSE',
silly: 'SILLY',
http: 'HTTP',
};
const moduleMap: { [key: string]: string } = {
startup: '๐ STARTUP',
server: '๐ SERVER',
wrappers: '๐ฆ WRAPPERS',
crypto: '๐ CRYPTO',
core: 'โก CORE',
parser: '๐ PARSER',
mediaflow: '๐ MEDIAFLOW',
stremthru: 'โจ STREMTHRU',
cache: '๐๏ธ CACHE',
regex: '๐
ฐ๏ธ REGEX',
database: '๐๏ธ DATABASE',
users: '๐ค USERS',
http: '๐ HTTP',
proxy: '๐ PROXY',
stremio: '๐ฅ STREMIO',
deduplicator: '๐ฏ DEDUPLICATOR',
limiter: 'โ๏ธ LIMITER',
filterer: '๐๏ธ FILTERER',
precomputer: '๐งฎ PRECOMPUTER',
sorter: '๐ SORTER',
proxifier: '๐ PROXIFIER',
fetcher: '๐ SCRAPER',
};
// Define colors for each log level using full names
const levelColors: { [key: string]: string } = {
ERROR: 'red',
WARNING: 'yellow',
INFO: 'cyan',
DEBUG: 'magenta',
HTTP: 'green',
VERBOSE: 'blue',
SILLY: 'grey',
};
const emojiLevelMap: { [key: string]: string } = {
error: 'โ',
warn: 'โ ๏ธ ',
info: '๐ต',
debug: '๐',
verbose: '๐',
silly: '๐คช',
http: '๐',
};
// Calculate the maximum level name length for padding
const MAX_LEVEL_LENGTH = Math.max(
...Object.values(levelMap).map((level) => level.length)
);
// Apply colors to Winston
winston.addColors(levelColors);
export const createLogger = (module: string) => {
const isJsonFormat = Env.LOG_FORMAT === 'json';
const timezone = Env.LOG_TIMEZONE;
const timestampFormat = winston.format((info) => {
info.timestamp = moment().tz(timezone).format('YYYY-MM-DD HH:mm:ss.SSS z');
return info;
});
return winston.createLogger({
level: Env.LOG_LEVEL,
format: isJsonFormat
? winston.format.combine(timestampFormat(), winston.format.json())
: winston.format.combine(
timestampFormat(),
winston.format.printf(({ timestamp, level, message, ...rest }) => {
const emoji = emojiLevelMap[level] || '';
const formattedModule = moduleMap[module] || module;
// Get full level name and pad it for centering
const fullLevel = levelMap[level] || level.toUpperCase();
const padding = Math.floor(
(MAX_LEVEL_LENGTH - fullLevel.length) / 2
);
const paddedLevel =
' '.repeat(padding) +
fullLevel +
' '.repeat(MAX_LEVEL_LENGTH - fullLevel.length - padding);
// Apply color to the padded level
const coloredLevel = winston.format
.colorize()
.colorize(fullLevel, paddedLevel);
const formatLine = (line: unknown) => {
return `${emoji} | ${coloredLevel} | ${timestamp} | ${formattedModule} | ${line} ${
rest ? `${formatJsonToStyledString(rest)}` : ''
}`;
};
if (typeof message === 'string') {
return message.split('\n').map(formatLine).join('\n');
} else if (typeof message === 'object') {
return formatLine(formatJsonToStyledString(message));
}
return formatLine(message);
})
),
transports: [new winston.transports.Console()],
});
};
function formatJsonToStyledString(json: any) {
// return json.formatted
if (json.formatted) {
return json.formatted;
}
// extract keys and values, display space separated key=value pairs
const keys = Object.keys(json);
const values = keys.map((key) => `${key}=${json[key]}`);
return values.join(' ');
}
export function maskSensitiveInfo(message: string) {
if (Env.LOG_SENSITIVE_INFO) {
return message;
}
return '<redacted>';
}
export const getTimeTakenSincePoint = (point: number) => {
const timeNow = new Date().getTime();
const duration = timeNow - point;
if (duration < 1000) {
return `${duration.toFixed(2)}ms`;
} else {
return `${(duration / 1000).toFixed(2)}s`;
}
};
|