fix: 修复全局变量失效

This commit is contained in:
ningmengchongshui 2024-06-09 12:31:15 +08:00
parent 31aab55f65
commit 32fd1e27f2
2 changed files with 43 additions and 28 deletions

View File

@ -2,10 +2,13 @@ import { segment as se } from 'icqq'
import { RedisClientType } from 'redis'
import { Yunzai } from './bot.js'
import { plugin as p } from './core/index.js'
/**
*
*/
declare global {
let redis: RedisClientType
let Bot: typeof Yunzai.prototype
let segment: typeof se
let logger: typeof console
let plugin: typeof p.prototype
}
let logger: any
}

View File

@ -2,20 +2,15 @@ import log4js from 'log4js'
import chalk from 'chalk'
import cfg from './config.js'
import fs from 'node:fs'
/**
*
*/
export default function setLog () {
let file = './logs'
if (!fs.existsSync(file)) {
fs.mkdirSync(file)
}
/** 调整error日志等级 */
*
* @returns
*/
function createLog() {
// log4js.levels.levels[5].level = Number.MAX_VALUE
// log4js.levels.levels.sort((a, b) => a.level - b.level)
log4js.configure({
appenders: {
console: {
@ -57,40 +52,57 @@ export default function setLog () {
const commandLogger = log4js.getLogger('command')
const errorLogger = log4js.getLogger('error')
/* eslint-disable no-useless-call */
/** 全局变量 logger */
global.logger = {
trace () {
/** 调整error日志等级 */
const logger = {
trace() {
defaultLogger.trace.call(defaultLogger, ...arguments)
},
debug () {
debug() {
defaultLogger.debug.call(defaultLogger, ...arguments)
},
info () {
info() {
defaultLogger.info.call(defaultLogger, ...arguments)
},
// warn及以上的日志采用error策略
warn () {
warn() {
commandLogger.warn.call(defaultLogger, ...arguments)
},
error () {
error() {
errorLogger.error.call(errorLogger, ...arguments)
},
fatal () {
fatal() {
errorLogger.fatal.call(errorLogger, ...arguments)
},
mark () {
mark() {
errorLogger.mark.call(commandLogger, ...arguments)
}
}
logColor()
return logger
}
/**
*
*/
function logColor () {
*
*/
export default function setLog() {
/**
*
*/
let file = './logs'
/**
*
*/
if (!fs.existsSync(file)){
fs.mkdirSync(file, {
'recursive': true
})
}
/** 全局变量 logger */
global.logger = createLog()
logger.chalk = chalk
logger.red = chalk.red
logger.green = chalk.green
@ -98,4 +110,4 @@ function logColor () {
logger.blue = chalk.blue
logger.magenta = chalk.magenta
logger.cyan = chalk.cyan
}
}