File size: 2,588 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
import winston from 'winston';
import { Settings } from './settings';

// Map log levels to short labels and emojis
const levelMap: { [key: string]: string } = {
  error: 'ERR',
  warn: 'WRN',
  info: 'INF',
  debug: 'DBG',
};

const moduleMap: { [key: string]: string } = {
  server: '🌐  SERVER',
  wrappers: 'πŸ“¦  WRAPPERS',
  crypto: 'πŸ”’  CRYPTO',
  addon: '🧩  ADDON',
  parser: 'πŸ”  PARSER',
  mediaflow: '🌊  MEDIAFLOW',
  stremthru: '✨  STREMTHRU',
  cache: 'πŸ—„οΈ  CACHE',
  regex: 'πŸ…°οΈ  REGEX',
};

// Define colors for each log level
const levelColors: { [key: string]: string } = {
  ERR: 'red',
  WRN: 'yellow',
  INF: 'cyan',
  DBG: 'magenta',
};

const emojiLevelMap: { [key: string]: string } = {
  error: '❌',
  warn: '⚠️ ',
  info: 'πŸ”΅',
  debug: '🐞',
};

// Apply colors to Winston
winston.addColors(levelColors);

export const createLogger = (module: string) => {
  // cloudflare workers do not play nice with winston
  // so we disable winston logs in cloudflare workers
  try {
    __dirname;
  } catch (e) {
    return console;
  }
  const isJsonFormat = Settings.LOG_FORMAT === 'json';
  return winston.createLogger({
    level: Settings.LOG_LEVEL || 'info', // Default to 'info' if not set
    format: isJsonFormat
      ? winston.format.combine(
          winston.format.timestamp(),
          winston.format.json() // Use JSON format when LOG_FORMAT=json
        )
      : winston.format.combine(
          winston.format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss.SSS',
          }),
          winston.format.printf(({ timestamp, level, message, func }) => {
            const emoji = emojiLevelMap[level] || '';
            const formattedModule = moduleMap[module] || module;
            level = levelMap[level] || level.toUpperCase();
            // Apply color to the level
            const coloredLevel = winston.format
              .colorize()
              .colorize(level, `${level}`);
            const formatLine = (line: unknown) => {
              return `${emoji} | ${coloredLevel} | ${timestamp} | ${formattedModule} ${
                func ? ' (' + func + ')' : ''
              } > ${line}`;
            };
            if (typeof message === 'string') {
              return message.split('\n').map(formatLine).join('\n');
            }
            return formatLine(message);
          })
        ),
    transports: [new winston.transports.Console()],
  });
};

export function maskSensitiveInfo(message: string) {
  if (Settings.LOG_SENSITIVE_INFO) {
    return message;
  }
  return '<redacted>';
}