feat(logger}: 支持更好的日志输出, 完善了文档

This commit is contained in:
ImFurina
2025-08-24 23:10:24 +08:00
parent 11e1c62cc3
commit ceb2044500
27 changed files with 915 additions and 832 deletions
+4 -3
View File
@@ -1,5 +1,6 @@
var url = require('url')
var MemoryCache = require('./memory-cache')
const logger = require('./logger.js')
var t = {
ms: 1,
@@ -349,7 +350,7 @@ function ApiCache() {
try {
redis.del(key)
} catch (err) {
console.log('[apicache] error in redis.del("' + key + '")')
logger.info('[apicache] error in redis.del("' + key + '")')
}
}
index.all = index.all.filter(doesntMatch(key))
@@ -373,7 +374,7 @@ function ApiCache() {
try {
redis.del(target)
} catch (err) {
console.log('[apicache] error in redis.del("' + target + '")')
logger.info('[apicache] error in redis.del("' + target + '")')
}
}
@@ -404,7 +405,7 @@ function ApiCache() {
try {
redis.del(key)
} catch (err) {
console.log('[apicache] error in redis.del("' + key + '")')
logger.info('[apicache] error in redis.del("' + key + '")')
}
})
}
+29
View File
@@ -0,0 +1,29 @@
// ANSI 颜色代码
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
bgRed: '\x1b[41m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m'
};
const logger = {
debug: (msg, ...args) => console.info(`${colors.cyan}[DEBUG]${colors.reset}`, msg, ...args),
info: (msg, ...args) => console.info(`${colors.green}[INFO]${colors.reset}`, msg, ...args),
warn: (msg, ...args) => console.info(`${colors.yellow}[WARN]${colors.reset}`, msg, ...args),
error: (msg, ...args) => console.error(`${colors.red}[ERROR]${colors.reset}`, msg, ...args),
success: (msg, ...args) => console.log(`${colors.bright}${colors.green}[SUCCESS]${colors.reset}`, msg, ...args),
critical: (msg, ...args) => console.error(`${colors.bright}${colors.bgRed}[CRITICAL]${colors.reset}`, msg, ...args)
};
// 导出logger
module.exports = logger;
+5 -4
View File
@@ -15,6 +15,7 @@ const anonymous_token = fs.readFileSync(
)
const { URLSearchParams, URL } = require('url')
const { APP_CONF } = require('../util/config.json')
const logger = require('./logger.js')
// request.debug = true // 开启可看到更详细信息
const WNMCID = (function () {
@@ -76,7 +77,7 @@ const createRequest = (uri, data, options) => {
return new Promise((resolve, reject) => {
let headers = options.headers || {}
let ip = options.realIP || options.ip || ''
// console.log(ip)
// logger.info(ip)
if (ip) {
headers['X-Real-IP'] = ip
headers['X-Forwarded-For'] = ip
@@ -201,11 +202,11 @@ const createRequest = (uri, data, options) => {
default:
// 未知的加密方式
console.log('[ERR]', 'Unknown Crypto:', crypto)
logger.info('[ERR]', 'Unknown Crypto:', crypto)
break
}
const answer = { status: 500, body: {}, cookie: [] }
// console.log(headers, 'headers')
// logger.info(headers, 'headers')
let settings = {
method: 'POST',
url: url,
@@ -282,7 +283,7 @@ const createRequest = (uri, data, options) => {
answer.status = 200
}
} catch (e) {
// console.log(e)
// logger.info(e)
// can't decrypt and can't parse directly
answer.body = body
answer.status = res.status