mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
optimize: log
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import log4js from 'log4js';
|
||||
import log4js, { Configuration } from 'log4js';
|
||||
import { truncateString } from '@/common/utils/helper';
|
||||
import path from 'node:path';
|
||||
import { SelfInfo } from '@/core';
|
||||
|
||||
export enum LogLevel {
|
||||
DEBUG = 'debug',
|
||||
@@ -10,34 +11,64 @@ export enum LogLevel {
|
||||
FATAL = 'fatal',
|
||||
}
|
||||
|
||||
const logPath = path.join(path.resolve(__dirname), 'logs', 'napcat.log');
|
||||
const logDir = path.join(path.resolve(__dirname), 'logs');
|
||||
|
||||
function genLogConfig(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
|
||||
return {
|
||||
appenders: {
|
||||
fileAppender: { // 输出到文件的appender
|
||||
type: 'file',
|
||||
filename: logPath, // 指定日志文件的位置和文件名
|
||||
maxLogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB
|
||||
},
|
||||
consoleAppender: { // 输出到控制台的appender
|
||||
type: 'console'
|
||||
function getFormattedTimestamp() {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = (now.getMonth() + 1).toString().padStart(2, '0');
|
||||
const day = now.getDate().toString().padStart(2, '0');
|
||||
const hours = now.getHours().toString().padStart(2, '0');
|
||||
const minutes = now.getMinutes().toString().padStart(2, '0');
|
||||
const seconds = now.getSeconds().toString().padStart(2, '0');
|
||||
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
|
||||
}
|
||||
|
||||
const filename = `${getFormattedTimestamp()}.log`;
|
||||
const logPath = path.join(logDir, filename);
|
||||
|
||||
const logConfig: Configuration = {
|
||||
appenders: {
|
||||
FileAppender: { // 输出到文件的appender
|
||||
type: 'file',
|
||||
filename: logPath, // 指定日志文件的位置和文件名
|
||||
maxLoogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB
|
||||
layout: {
|
||||
type: 'pattern',
|
||||
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] - %m'
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ['fileAppender', 'consoleAppender'], level: 'debug' }, // 默认情况下同时输出到文件和控制台
|
||||
file: { appenders: ['fileAppender'], level: fileLogLevel },
|
||||
console: { appenders: ['consoleAppender'], level: consoleLogLevel }
|
||||
ConsoleAppender: { // 输出到控制台的appender
|
||||
type: 'console',
|
||||
layout: {
|
||||
type: 'pattern',
|
||||
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] - %m'
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ['FileAppender', 'ConsoleAppender'], level: 'debug' }, // 默认情况下同时输出到文件和控制台
|
||||
file: { appenders: ['FileAppender'], level: 'debug' },
|
||||
console: { appenders: ['ConsoleAppender'], level: 'debug' }
|
||||
}
|
||||
};
|
||||
|
||||
log4js.configure(logConfig);
|
||||
|
||||
|
||||
export function setLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
|
||||
log4js.configure(genLogConfig(fileLogLevel, consoleLogLevel));
|
||||
logConfig.categories.file.level = fileLogLevel;
|
||||
logConfig.categories.console.level = consoleLogLevel;
|
||||
log4js.configure(logConfig);
|
||||
}
|
||||
|
||||
setLogLevel(LogLevel.DEBUG, LogLevel.INFO);
|
||||
export function setLogSelfInfo(selfInfo: SelfInfo) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
logConfig.appenders.FileAppender.layout.pattern = logConfig.appenders.ConsoleAppender.layout.pattern =
|
||||
`%d{yyyy-MM-dd hh:mm:ss} [%p] ${selfInfo.nick || selfInfo.uin}(${selfInfo.uin}) %m`;
|
||||
log4js.configure(logConfig);
|
||||
}
|
||||
|
||||
let fileLogEnabled = true;
|
||||
let consoleLogEnabled = true;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import { OB11Message } from '@/onebot11/types';
|
||||
import { log } from '@/common/utils/log';
|
||||
import { getGroup } from '@/common/data';
|
||||
import { getGroup, getGroupMember } from '@/common/data';
|
||||
import exp from 'constants';
|
||||
|
||||
export async function logMessage(ob11Message: OB11Message){
|
||||
let prefix = '';
|
||||
@@ -8,6 +9,56 @@ export async function logMessage(ob11Message: OB11Message){
|
||||
const group = await getGroup(ob11Message.group_id!);
|
||||
prefix = `群[${group?.groupName}(${ob11Message.group_id})] `;
|
||||
}
|
||||
const msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${ob11Message.raw_message}`;
|
||||
let msgChain = '';
|
||||
if (Array.isArray(ob11Message.message)) {
|
||||
for (const segment of ob11Message.message) {
|
||||
if (segment.type === 'text') {
|
||||
msgChain += segment.data.text + '\n';
|
||||
}
|
||||
else if (segment.type === 'at') {
|
||||
const groupMember = await getGroupMember(ob11Message.group_id!, segment.data.qq!);
|
||||
msgChain += `@${groupMember?.cardName || groupMember?.nick}(${segment.data.qq}) `;
|
||||
}
|
||||
else if (segment.type === 'reply') {
|
||||
msgChain += `回复消息(id:${segment.data.id}) `;
|
||||
}
|
||||
else if (segment.type === 'image') {
|
||||
msgChain += `\n[图片]${segment.data.url}\n`;
|
||||
}
|
||||
else if (segment.type === 'face'){
|
||||
msgChain += `[表情](id:${segment.data.id}) `;
|
||||
}
|
||||
else if (segment.type === 'mface'){
|
||||
msgChain += `\n[商城表情]${segment.data.url}\n`;
|
||||
}
|
||||
else if (segment.type === 'record') {
|
||||
msgChain += `[语音]${segment.data.file} `;
|
||||
}
|
||||
else if (segment.type === 'file') {
|
||||
msgChain += `[文件]${segment.data.file} `;
|
||||
}
|
||||
else if (segment.type === 'json') {
|
||||
msgChain += `\n[json]${segment.data}\n`;
|
||||
}
|
||||
else if (segment.type === 'markdown') {
|
||||
msgChain += `\n[json]${segment.data.content}\n`;
|
||||
}
|
||||
else {
|
||||
msgChain += `${segment}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
msgChain = ob11Message.message;
|
||||
}
|
||||
const msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${msgChain}`;
|
||||
log(msgString);
|
||||
}
|
||||
|
||||
export async function logNotice(ob11Notice: any){
|
||||
log('[notice]', ob11Notice);
|
||||
}
|
||||
|
||||
export async function logRequest(ob11Request: any){
|
||||
log('[request]', ob11Request);
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ import { dbUtil } from '@/common/utils/db';
|
||||
import { BuddyListener, GroupListener, NodeIKernelBuddyListener } from '../core/src/listeners';
|
||||
import { OB11FriendRequestEvent } from '@/onebot11/event/request/OB11FriendRequest';
|
||||
import { NTQQGroupApi, NTQQUserApi } from '../core/src/apis';
|
||||
import { log, logDebug, logError } from '@/common/utils/log';
|
||||
import { log, logDebug, logError, setLogSelfInfo } from '@/common/utils/log';
|
||||
import { OB11GroupRequestEvent } from '@/onebot11/event/request/OB11GroupRequest';
|
||||
import { OB11GroupAdminNoticeEvent } from '@/onebot11/event/notice/OB11GroupAdminNoticeEvent';
|
||||
import { GroupDecreaseSubType, OB11GroupDecreaseEvent } from '@/onebot11/event/notice/OB11GroupDecreaseEvent';
|
||||
@@ -50,6 +50,7 @@ export class NapCatOnebot11 {
|
||||
log(serviceInfo);
|
||||
NTQQUserApi.getUserDetailInfo(selfInfo.uid).then(user => {
|
||||
selfInfo.nick = user.nick;
|
||||
setLogSelfInfo(selfInfo);
|
||||
}).catch(logError);
|
||||
if (ob11Config.enableHttp) {
|
||||
ob11HTTPServer.start(ob11Config.httpPort, ob11Config.httpHost);
|
||||
@@ -142,7 +143,6 @@ export class NapCatOnebot11 {
|
||||
// message.msgShortId = await dbUtil.addMsg(message);
|
||||
// }
|
||||
OB11Constructor.message(message).then((msg) => {
|
||||
logMessage(msg).then().catch(logError);
|
||||
if (debug) {
|
||||
msg.raw = message;
|
||||
} else {
|
||||
|
@@ -14,6 +14,7 @@ import { isNull } from '@/common/utils/helper';
|
||||
import { dbUtil } from '@/common/utils/db';
|
||||
import { friendRequests, getGroup, groupNotifies, selfInfo } from '@/common/data';
|
||||
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi } from '../../core/src/apis';
|
||||
import { logMessage, logNotice, logRequest } from '@/onebot11/log';
|
||||
|
||||
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||
|
||||
@@ -69,8 +70,15 @@ export function postWsEvent(event: PostEventType) {
|
||||
}
|
||||
}
|
||||
|
||||
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs= true) {
|
||||
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = true) {
|
||||
const config = ob11Config;
|
||||
if (msg.post_type === 'message' || msg.post_type === 'message_sent') {
|
||||
logMessage(msg as OB11Message).then().catch(logError);
|
||||
} else if (msg.post_type === 'notice') {
|
||||
logNotice(msg).then().catch(logError);
|
||||
} else if (msg.post_type === 'request') {
|
||||
logRequest(msg).then().catch(logError);
|
||||
}
|
||||
// 判断msg是否是event
|
||||
if (!config.reportSelfMessage && !reportSelf) {
|
||||
if (msg.post_type === 'message' && (msg as OB11Message).user_id.toString() == selfInfo.uin) {
|
||||
@@ -181,7 +189,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs= tr
|
||||
});
|
||||
}
|
||||
}
|
||||
if (postWs){
|
||||
if (postWs) {
|
||||
postWsEvent(msg);
|
||||
}
|
||||
}
|
||||
|
@@ -125,7 +125,8 @@ export enum OB11MessageDataType {
|
||||
export interface OB11MessageMFace {
|
||||
type: OB11MessageDataType.mface,
|
||||
data: {
|
||||
text: string
|
||||
text: string,
|
||||
url: string
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user