fix: 整理日志、添加颜色、使用统一的日志函数以提高日志可读性

This commit is contained in:
XBB
2024-06-04 02:59:35 +08:00
parent c698ce9562
commit bac2a6b034
15 changed files with 96 additions and 68 deletions

View File

@@ -3,19 +3,23 @@ import { log } from '@/common/utils/log';
import { getGroup, getGroupMember, selfInfo } from '@/core/data';
import exp from 'constants';
import { Group } from '@/core';
import chalk from 'chalk';
const spSegColor = chalk.blue;// for special segment
const spColor = chalk.cyan;// for special
// todo: 应该放到core去用RawMessage解析打印
export async function logMessage(ob11Message: OB11Message){
export async function logMessage(ob11Message: OB11Message) {
const isSelfSent = ob11Message.sender.user_id.toString() === selfInfo.uin;
let prefix = '';
let group: Group | undefined;
if (isSelfSent){
if (isSelfSent) {
prefix = '发送消息 ';
if (ob11Message.message_type === 'private'){
if (ob11Message.message_type === 'private') {
prefix += '给私聊 ';
prefix += `${ob11Message.target_id}`;
}
else{
else {
prefix += '给群聊 ';
}
}
@@ -25,57 +29,60 @@ export async function logMessage(ob11Message: OB11Message){
}
let msgChain = '';
if (Array.isArray(ob11Message.message)) {
const msgParts = [];
for (const segment of ob11Message.message) {
if (segment.type === 'text') {
msgChain += segment.data.text + '\n';
msgParts.push(segment.data.text);
}
else if (segment.type === 'at') {
const groupMember = await getGroupMember(ob11Message.group_id!, segment.data.qq!);
msgChain += `@${groupMember?.cardName || groupMember?.nick}(${segment.data.qq}) `;
msgParts.push(spSegColor(`[@${groupMember?.cardName || groupMember?.nick}(${segment.data.qq})]`));
}
else if (segment.type === 'reply') {
msgChain += `回复消息(id:${segment.data.id}) `;
msgParts.push(spSegColor(`[回复消息|id:${segment.data.id}]`));
}
else if (segment.type === 'image') {
msgChain += `\n[图片]${segment.data.url}\n`;
msgParts.push(spSegColor(`[图片|${segment.data.url}]`));
}
else if (segment.type === 'face'){
msgChain += `[表情](id:${segment.data.id}) `;
else if (segment.type === 'face') {
msgParts.push(spSegColor(`[表情|id:${segment.data.id}]`));
}
else if (segment.type === 'mface'){
msgChain += `\n[商城表情]${segment.data.url}\n`;
else if (segment.type === 'mface') {
// @ts-expect-error 商城表情 url
msgParts.push(spSegColor(`[商城表情|${segment.data.url}]`));
}
else if (segment.type === 'record') {
msgChain += `[语音]${segment.data.file} `;
msgParts.push(spSegColor(`[语音|${segment.data.file}]`));
}
else if (segment.type === 'file') {
msgChain += `[文件]${segment.data.file} `;
msgParts.push(spSegColor(`[文件|${segment.data.file}]`));
}
else if (segment.type === 'json') {
msgChain += `\n[json]${JSON.stringify(segment.data)}\n`;
msgParts.push(spSegColor(`[json|${JSON.stringify(segment.data)}]`));
}
else if (segment.type === 'markdown') {
msgChain += `\n[json]${segment.data.content}\n`;
msgParts.push(spSegColor(`[markdown|${segment.data.content}]`));
}
else {
msgChain += `${JSON.stringify(segment)}`;
msgParts.push(spSegColor(`[未实现|${JSON.stringify(segment)}]`));
}
}
msgChain = msgParts.join(' ');
}
else {
msgChain = ob11Message.message;
}
let msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${msgChain}`;
if (isSelfSent){
if (isSelfSent) {
msgString = `${prefix}: ${msgChain}`;
}
log(msgString);
}
export async function logNotice(ob11Notice: any){
log('[notice]', ob11Notice);
export async function logNotice(ob11Notice: any) {
log(spColor('[Notice]'), ob11Notice);
}
export async function logRequest(ob11Request: any){
log('[request]', ob11Request);
export async function logRequest(ob11Request: any) {
log(spColor('[Request]'), ob11Request);
}