From 1e63fc6ab109cd15a0ad626465479eab52d589de Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Sun, 21 Sep 2025 19:17:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scripts/Main/Common/Logger.ts | 88 +++++++++++++++++++++ assets/Scripts/Main/Common/Logger.ts.meta | 9 +++ assets/Scripts/chat18x/data/GirlChatData.ts | 7 +- 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 assets/Scripts/Main/Common/Logger.ts create mode 100644 assets/Scripts/Main/Common/Logger.ts.meta diff --git a/assets/Scripts/Main/Common/Logger.ts b/assets/Scripts/Main/Common/Logger.ts new file mode 100644 index 00000000..8816a234 --- /dev/null +++ b/assets/Scripts/Main/Common/Logger.ts @@ -0,0 +1,88 @@ +/** + * Logger 日志工具类 + * + * 功能: + * - 按等级输出日志(debug/info/warn/error) + * - 在 Dev/Test 环境下输出全部日志 + * - 在 Prod 环境下只输出 error + * - 可扩展日志上报(比如接入远程日志系统) + */ + +import { AppConfig } from "db://assets/Scripts/chat18x/config/env/appConfig"; + +export enum LogLevel { + Debug = 0, + Info = 1, + Warn = 2, + Error = 3, + None = 4, // 不打印任何日志 +} + +class Logger { + private level: LogLevel; + + constructor() { + // 根据环境决定日志等级 + if (AppConfig.I.isDev()) { + this.level = LogLevel.Debug; // Dev 输出所有日志 + } else if (AppConfig.I.isTest()) { + this.level = LogLevel.Debug; // Test 输出所有日志 + } else if (AppConfig.I.isProd()) { + this.level = LogLevel.Error; // Prod 只输出 error + } else { + this.level = LogLevel.None; // 不打印任何日志 + } + } + + /** 修改日志等级(运行时可动态调整) */ + public setLevel(level: LogLevel) { + this.level = level; + } + + public debug(...args: any[]) { + if (this.level <= LogLevel.Debug) { + console.debug("[DEBUG]", ...args); + } + } + + public info(...args: any[]) { + if (this.level <= LogLevel.Info) { + console.info("[INFO]", ...args); + } + } + + public log(...args: any[]) { + if (this.level <= LogLevel.Info) { + console.log("[LOG]", ...args); + } + } + + public warn(...args: any[]) { + if (this.level <= LogLevel.Warn) { + console.warn("[WARN]", ...args); + } + } + + public error(...args: any[]) { + if (this.level <= LogLevel.Error) { + console.error("[ERROR]", ...args); + // 这里也可以加远程上报,例如: + // this.reportError(args); + } + } + + // 示例:远程上报错误日志 + private reportError(args: any[]) { + try { + // fetch("https://log-server.xxx.com/report", { + // method: "POST", + // body: JSON.stringify({ msg: args, time: Date.now() }), + // }); + } catch (e) { + console.error("日志上报失败", e); + } + } +} + +// 全局单例 +export const logger = new Logger(); diff --git a/assets/Scripts/Main/Common/Logger.ts.meta b/assets/Scripts/Main/Common/Logger.ts.meta new file mode 100644 index 00000000..0ded8f20 --- /dev/null +++ b/assets/Scripts/Main/Common/Logger.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "90ee00a3-2349-4a60-a6f0-6e4bebb5e964", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/data/GirlChatData.ts b/assets/Scripts/chat18x/data/GirlChatData.ts index 9bc3557d..017961ce 100644 --- a/assets/Scripts/chat18x/data/GirlChatData.ts +++ b/assets/Scripts/chat18x/data/GirlChatData.ts @@ -5,6 +5,7 @@ import { BaseData } from "./BaseData"; import proto from 'db://assets/Scripts/proto/proto.pb.js'; import Utils from "../../Main/Common/Utils"; import { InnerMsgCode } from "../../Main/Config/InnerMsgCode"; +import { logger } from "db://assets/Scripts/Main/Common/Logger"; export class GirlChatData extends BaseData { // id @@ -75,7 +76,7 @@ export class GirlChatData extends BaseData { chatTotalCount: this.chatTotalCount, }; Utils.sendInnerMsg(InnerMsgCode.ChatTotalCountChange, msgData); - console.log("当前技师 ", this.id, " ,总聊天次数:", this.chatTotalCount); + logger.log("当前技师 ", this.id, " ,总聊天次数:", this.chatTotalCount); } /** 获取剩余聊天次数 */ @@ -86,7 +87,7 @@ export class GirlChatData extends BaseData { /** 保存剩余聊天次数 */ public setChatRemainCount(value: number): void { this.chatRemainCount = value; - console.log("当前技师 ", this.id, " ,剩余聊天次数:", this.chatRemainCount); + logger.log("当前技师 ", this.id, " ,剩余聊天次数:", this.chatRemainCount); } /** 更新剩余聊天次数 */ @@ -143,7 +144,7 @@ export class GirlChatData extends BaseData { // 排序:根据 msgId 从大到小排序 this.chatRecordIds.sort((a, b) => b - a); - console.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord); + logger.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord); } /** 获取聊天记录的所有 id */