refactor: logger bind (#577)

This commit is contained in:
手瓜一十雪
2024-11-28 20:55:28 +08:00
committed by GitHub
parent 9f19ceca87
commit cd6dbd69cc
19 changed files with 109 additions and 117 deletions

View File

@@ -142,7 +142,6 @@ export class NTQQFileApi {
}
async createValidSendVideoElement(context: SendMessageContext, filePath: string, fileName: string = '', diyThumbPath: string = ''): Promise<SendVideoElement> {
const logger = this.core.context.logger;
let videoInfo = {
width: 1920,
height: 1080,
@@ -152,9 +151,9 @@ export class NTQQFileApi {
filePath,
};
try {
videoInfo = await getVideoInfo(filePath, logger);
videoInfo = await getVideoInfo(filePath, this.context.logger);
} catch (e) {
logger.logError.bind(logger)('获取视频信息失败,将使用默认值', e);
this.context.logger.logError('获取视频信息失败,将使用默认值', e);
}
let fileExt = 'mp4';
@@ -162,7 +161,7 @@ export class NTQQFileApi {
const tempExt = (await fileType.fileTypeFromFile(filePath))?.ext;
if (tempExt) fileExt = tempExt;
} catch (e) {
this.context.logger.logError.bind(logger)('获取文件类型失败', e);
this.context.logger.logError('获取文件类型失败', e);
}
const newFilePath = filePath + '.' + fileExt;
fs.copyFileSync(filePath, newFilePath);
@@ -183,7 +182,7 @@ export class NTQQFileApi {
ffmpeg(filePath)
.on('error', (err) => {
try {
logger.logDebug('获取视频封面失败,使用默认封面', err);
this.context.logger.logDebug('获取视频封面失败,使用默认封面', err);
if (diyThumbPath) {
fsPromises.copyFile(diyThumbPath, thumbPath).then(() => {
resolve(thumbPath);
@@ -193,7 +192,7 @@ export class NTQQFileApi {
resolve(thumbPath);
}
} catch (error) {
logger.logError.bind(logger)('获取视频封面失败,使用默认封面失败', error);
this.context.logger.logError('获取视频封面失败,使用默认封面失败', error);
}
})
.screenshots({
@@ -230,6 +229,7 @@ export class NTQQFileApi {
}
async createValidSendPttElement(pttPath: string): Promise<SendPttElement> {
const { converted, path: silkPath, duration } = await encodeSilk(pttPath, this.core.NapCatTempPath, this.core.context.logger);
if (!silkPath) {
throw new Error('语音转换失败, 请检查语音文件是否正常');
@@ -239,8 +239,7 @@ export class NTQQFileApi {
throw new Error('文件异常大小为0');
}
if (converted) {
fsPromises.unlink(silkPath).then().catch(
(e) => this.context.logger.logError.bind(this.context.logger)('删除临时文件失败', e)
fsPromises.unlink(silkPath).then().catch((e) => this.context.logger.logError('删除临时文件失败', e)
);
}
return {
@@ -454,7 +453,7 @@ export class NTQQFileApi {
}
}
} catch (error: any) {
this.context.logger.logError.bind(this.context.logger)('获取rkey失败', error.message);
this.context.logger.logError('获取rkey失败', error.message);
}
if (!rkeyData.online_rkey) {
@@ -464,7 +463,7 @@ export class NTQQFileApi {
rkeyData.private_rkey = tempRkeyData.private_rkey;
rkeyData.online_rkey = tempRkeyData.expired_time > Date.now() / 1000;
} catch (e) {
this.context.logger.logError.bind(this.context.logger)('获取rkey失败 Fallback Old Mode', e);
this.context.logger.logError('获取rkey失败 Fallback Old Mode', e);
}
}

View File

@@ -27,7 +27,7 @@ export class NTQQGroupApi {
this.core = core;
}
async initApi() {
this.initCache().then().catch(this.context.logger.logError.bind(this.context.logger));
this.initCache().then().catch(e => this.context.logger.logError(e));
}
async initCache() {
this.groups = await this.getGroups();

View File

@@ -31,7 +31,7 @@ export class NTQQPacketApi {
await this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion())
.then()
.catch((err) => {
this.logger.logError.bind(this.core.context.logger);
this.logger.logError(err);
this.errStack.push(err);
});
}

View File

@@ -27,7 +27,6 @@ export class RkeyManager {
await this.refreshRkey();
} catch (e) {
throw new Error(`获取rkey失败: ${e}`);//外抛
//this.logger.logError.bind(this.logger)('获取rkey失败', e);
}
}
return this.rkeyData;
@@ -50,7 +49,7 @@ export class RkeyManager {
expired_time: temp.expired_time
};
} catch (e) {
this.logger.logError.bind(this.logger)(`[Rkey] Get Rkey ${url} Error `, e);
this.logger.logError(`[Rkey] Get Rkey ${url} Error `, e);
//是否为最后一个url
if (url === this.serverUrl[this.serverUrl.length - 1]) {
throw new Error(`获取rkey失败: ${e}`);//外抛

View File

@@ -127,7 +127,7 @@ export class NapCatCore {
await api.initApi();
}
}
this.initNapCatCoreListeners().then().catch(this.context.logger.logError.bind(this.context.logger));
this.initNapCatCoreListeners().then().catch((e) => this.context.logger.logError(e));
this.context.logger.setFileLogEnabled(
this.configLoader.configData.fileLog,
@@ -154,7 +154,7 @@ export class NapCatCore {
const msgListener = new NodeIKernelMsgListener();
msgListener.onKickedOffLine = (Info: KickedOffLineInfo) => {
// 下线通知
this.context.logger.logError.bind(this.context.logger)('[KickedOffLine] [' + Info.tipsTitle + '] ' + Info.tipsDesc);
this.context.logger.logError('[KickedOffLine] [' + Info.tipsTitle + '] ' + Info.tipsDesc);
this.selfInfo.online = false;
};
msgListener.onRecvMsg = (msgs) => {