refactor: log dir

This commit is contained in:
linyuchen 2024-03-23 21:08:34 +08:00
parent 95b4b11f02
commit b314e2f3a0
3 changed files with 28 additions and 20 deletions

View File

@ -6,7 +6,7 @@ import path from "node:path";
import {selfInfo} from "./data";
import {DATA_DIR} from "./utils";
export const HOOK_LOG = false;
export const HOOK_LOG = true;
export const ALLOW_SEND_TEMP_MSG = false;

View File

@ -4,16 +4,18 @@ import path from "node:path";
import {DATA_DIR, truncateString} from "./index";
import {getConfigUtil} from "../config";
const date = new Date();
const logFileName = `llonebot-${date.toLocaleString("zh-CN")}.log`.replace(/\//g, "-").replace(/:/g, "-");
const logDir = path.join(DATA_DIR, "logs");
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, {recursive: true});
}
export function log(...msg: any[]) {
if (!getConfigUtil().getConfig().log) {
return //console.log(...msg);
}
let currentDateTime = new Date().toLocaleString();
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const currentDate = `${year}-${month}-${day}`;
const userInfo = selfInfo.uin ? `${selfInfo.nick}(${selfInfo.uin})` : ""
let logMsg = "";
for (let msgItem of msg) {
@ -25,10 +27,11 @@ export function log(...msg: any[]) {
}
logMsg += msgItem + " ";
}
let currentDateTime = new Date().toLocaleString();
logMsg = `${currentDateTime} ${userInfo}: ${logMsg}\n\n`
// sendLog(...msg);
// console.log(msg)
fs.appendFile(path.join(DATA_DIR, `llonebot-${currentDate}.log`), logMsg, (err: any) => {
fs.appendFile(path.join(logDir, logFileName), logMsg, (err: any) => {
})
}

View File

@ -389,24 +389,29 @@ function onLoad() {
selfInfo.nick = selfInfo.uin;
} catch (e) {
log("retry get self info", e);
selfInfo.uin = globalThis.authData?.uin;
selfInfo.uid = globalThis.authData?.uid;
selfInfo.nick = selfInfo.uin;
}
log("self info", selfInfo);
log("self info", selfInfo, globalThis.authData);
if (selfInfo.uin) {
try {
const userInfo = (await NTQQUserApi.getUserDetailInfo(selfInfo.uid));
log("self info", userInfo);
if (userInfo) {
selfInfo.nick = userInfo.nick;
} else {
async function getUserNick(){
try {
getSelfNickCount++;
if (getSelfNickCount < 10) {
return setTimeout(init, 1000);
const userInfo = (await NTQQUserApi.getUserDetailInfo(selfInfo.uid));
log("self info", userInfo);
if (userInfo) {
selfInfo.nick = userInfo.nick;
return
}
} catch (e) {
log("get self nickname failed", e.stack);
}
if (getSelfNickCount < 10) {
return setTimeout(getUserNick, 1000);
}
} catch (e) {
log("get self nickname failed", e.toString());
return setTimeout(init, 1000);
}
getUserNick().then()
start().then();
} else {
setTimeout(init, 1000)