对cocos creator 中log的扩展

本文介绍了如何扩展Cocos Creator的log功能,包括添加日志总开关(SystemSetting.IsOpenLog),日志标记tag,显示时间戳以及彩色日志输出,以增强日志的可读性和定位问题的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

扩展以下内容:

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值