日志控制
This commit is contained in:
@@ -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();
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "4.0.24",
|
||||||
|
"importer": "typescript",
|
||||||
|
"imported": true,
|
||||||
|
"uuid": "90ee00a3-2349-4a60-a6f0-6e4bebb5e964",
|
||||||
|
"files": [],
|
||||||
|
"subMetas": {},
|
||||||
|
"userData": {}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import { BaseData } from "./BaseData";
|
|||||||
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
||||||
import Utils from "../../Main/Common/Utils";
|
import Utils from "../../Main/Common/Utils";
|
||||||
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
|
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
|
||||||
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
||||||
|
|
||||||
export class GirlChatData extends BaseData {
|
export class GirlChatData extends BaseData {
|
||||||
// id
|
// id
|
||||||
@@ -75,7 +76,7 @@ export class GirlChatData extends BaseData {
|
|||||||
chatTotalCount: this.chatTotalCount,
|
chatTotalCount: this.chatTotalCount,
|
||||||
};
|
};
|
||||||
Utils.sendInnerMsg(InnerMsgCode.ChatTotalCountChange, msgData);
|
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 {
|
public setChatRemainCount(value: number): void {
|
||||||
this.chatRemainCount = value;
|
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 从大到小排序
|
// 排序:根据 msgId 从大到小排序
|
||||||
this.chatRecordIds.sort((a, b) => b - a);
|
this.chatRecordIds.sort((a, b) => b - a);
|
||||||
|
|
||||||
console.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord);
|
logger.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 获取聊天记录的所有 id */
|
/** 获取聊天记录的所有 id */
|
||||||
|
|||||||
Reference in New Issue
Block a user