2.Eric-MVVM-Framework-日志管理

Eric-MVVM-Framework基于CocosCreator3.7.4引擎实现

创建项目

日志管理

  1. 创建LogMgr类管理日志
/**
 * 日志管理器类,负责管理和输出不同类型的日志
 */
export class LogMgr {
    /**
     * 单例模式
     */
    private static _instance: LogMgr;
    public static get instance() {
        if (!this._instance) {
            this._instance = new LogMgr();
        }
        return this._instance;
    }
    constructor() {

    }
    /**
     * 输出调试信息
     */
    get debug() {
        return console.log.bind(this, '%c【调试】', 'color: white; background-color: #007BFF; font-weight: bold; font-size: 14px;');
    }

    /**
     * 输出一般信息
     */
    get info() {
        return console.log.bind(this, '%c【信息】', 'color: white; background-color: #28A745; font-weight: bold; font-size: 14px;');
    }
    /**
    * 输出警告信息
    */
    get warn() {
        return console.log.bind(this, '%c【警告】', 'color: black; background-color: #FFC107; font-weight: bold; font-size: 14px;');

    }
    /**
     * 输出错误信息
     */
    get err() {
        return console.log.bind(this, '%c【错误】', 'color: white; background-color: #DC3545; font-weight: bold; font-size: 14px;');
    }
}


  1. 创建全局映射
/**
 * 源码作者:<EricShang>
 */

import { LogMgr } from "../managers/LogMgr";



/**
 * Eric类,负责映射框架接口
 */
class Eric{ 
    /** 日志管理器 */
    log = LogMgr.instance;
}
/** 扩展 Window 接口 */
declare global {
    interface Window{
        eric: Eric;
    }
    var eric: Eric;
}

/** 创建 Eric 类的实例并赋值给全局 window 对象 */
window.eric = new Eric();

  1. 测试
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Test')
export class Test extends Component {
    start() {
        eric.log.debug('Test debug');
        eric.log.err('Test error');
        eric.log.warn('Test warn');
        eric.log.info('Test info');
        
    }

    update(deltaTime: number) {
        
    }
}


  1. 运行预览
    image
posted @ 2024-11-22 16:29  EricShx  阅读(16)  评论(0)    收藏  举报