扩展以下内容:
1.增加日志总开关,代码中的SystemSetting.IsOpenLog,该变量可以自行定义
2.日志标记tag,方便根据tag一眼找到相应日志
3.日志时间,不带时间的日志是没有灵魂的
4.日志颜色,方便打印不同颜色的日志
注:IsArrayContain是自己封装的检测array是否包括target项的函数,可自行定义
import { log } from "util";
import { Time } from "./Time";
import { IsArrayContain } from "./Util";
import SystemSetting from "../../../Game/SystemSetting";
export default class Log {
public static Tags: Array<string> = new Array<string>();
public static Key = {
System: "System",
Manager: "Manager",
Battle: "Battle",
Lobby: "Lobby",
Net: 'Net',
Entry: 'Entry',
WeChat: 'WeChat',
Profiler: "Profiler",
HeartBeat: 'HeartBeat',
Logic: 'Logic',
Login: 'Login',
Physics: 'Physics'
}
public static Init() {
this.OpenTag(Log.Key.Net)
this.OpenTag(Log.Key.System)
this.OpenTag(Log.Key.Manager)
this.OpenTag(Log.Key.Battle)
this.OpenTag(Log.Key.Lobby)
this.OpenTag(Log.Key.Entry)
this.OpenTag(Log.Key.WeChat)
this.OpenTag(Log.Key.Profiler)
this.OpenTag(Log.Key.Logic)
this.OpenTag(Log.Key.Login)
this.OpenTag(Log.Key.HeartBeat)
// this.OpenTag(Log.Key.Physics)
}
public static OpenTag(tag) {
if (!IsArrayContain(this.Tags, tag)) {
this.Tags.push(tag)
}
}
//无颜色
public static Trace(tag:string, msg:any) {
this.print(tag, this.GetString(msg), "")
}
//红色
public static Red(tag, msg) {
this.print(tag, this.GetString(msg), "color:red;")
}
//绿色
public static Green(tag, msg) {
this.print(tag, this.GetString(msg), "color:green;")
}
//橙色
public static Orange(tag, msg) {
this.print(tag, this.GetString(msg), "color:#ee7700;")
}
//灰色
public static Gray(tag, msg) {
this.print(tag, this.GetString(msg), "color:gray;")
}
//蓝色
public static Blue(tag, msg) {
this.print(tag, this.GetString(msg), "color:#3a5fcd;")
}
//紫色
public static Purple(tag, msg) {
this.print(tag, this.GetString(msg), "color:#b23aee;")
}
//深粉色
public static DeepPink(tag, msg) {
this.print(tag, this.GetString(msg), "color:#ff1493;")
}
public static Error(msg) {
cc.error('[err] ' + msg);
}
public static Warn(msg) {
cc.warn('[warn] ' + msg);
}
private static GetString(msg) {
return (!Array.isArray(msg) && typeof msg === 'object') ? JSON.stringify(msg) : msg;
}
private static print(tag, msg, color) {
if (!SystemSetting.IsOpenLog) {
return;
}
//标记没有打开,不打印该日志
if (!IsArrayContain(this.Tags, tag)) {
return;
}
var backLog = console.log || cc.log || log;
backLog.call(this, "%c%s:" + cc.js.formatStr.apply(cc, [msg]), color, '【' + Time.Format(new Date()) + '】' + '【' + tag + '】');
}
public static Track(tag, msg, obj) {
if (!SystemSetting.IsOpenLog) {
return;
}
if (!IsArrayContain(this.Tags, tag)) {
return;
}
console.trace('【' + tag + '】' + msg, obj);
}
}