123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 'use strict';
- const logLevel = {
- TRACE: 0,
- DEBUG: 1,
- INFO: 2,
- WARN: 3,
- ERROR: 4
- };
- const logLevelDesc = {
- [logLevel.TRACE]: 'TRACE',
- [logLevel.DEBUG]: 'DEBUG',
- [logLevel.INFO]: 'INFO',
- [logLevel.WARN]: 'WARN',
- [logLevel.ERROR]: 'ERROR'
- };
- const logLevelMethod = {
- [logLevel.TRACE]: 'logTrace',
- [logLevel.DEBUG]: 'logDebug',
- [logLevel.INFO]: 'logInfo',
- [logLevel.WARN]: 'LogWarn',
- [logLevel.ERROR]: 'logError'
- };
- let _logLevel = logLevel.INFO;
- class GameLog
- {
-
- static logLevel(level){
- cc.log(`Logger.js - log level: ${logLevelDesc[level]}`);
- _logLevel = level;
- }
- set logLevel(level){
- this._logLevel = level;
- }
- get logLevel(){
- return this._logLevel;
- }
-
- constructor(name,level){
- this._logLevel = level || _logLevel;
- this._tag = name || '';
- }
-
- getLogName(){
-
- return 'log_name';
- }
-
- readLog(){
-
- return 'log content';
- }
-
- log(msg,...args){
- const content = this._format(msg,...args);
- cc.log(`${this._tag} - ${content}`);
- }
-
- trace(msg,...args){
- const content = this._format(msg,...args);
- cc.log(`${this._tag} - ${content}`);
- this._log(logLevel.TRACE,content);
- }
-
- debug(msg,...args){
- const content = this._format(msg,...args);
- cc.log(`${this._tag} - ${content}`);
- this._log(logLevel.DEBUG,content);
- }
-
- info(msg,...args){
- const content = this._format(msg,...args);
- cc.info(`${this._tag} - ${content}`);
- this._log(logLevel.INFO,content);
- }
-
- warn(msg,...args){
- const content = this._format(msg,...args);
- cc.warn(`${this._tag} - ${content}`);
- this._log(logLevel.WARN,content);
- }
-
- error(msg,...args){
- const content = this._format(msg,...args);
- cc.error(`${this._tag} - ${content}`);
- this._log(logLevel.ERROR,content);
- }
-
- _log(level,info){
- if(level >= this._logLevel){
-
- let method = logLevelMethod[level];
- const ccNativeBridge = require('CCNativeBridge');
-
- }
- }
-
- _format (msg,...args) {
- let result = msg + '';
- if (args.length > 0) {
- if (args.length === 1 && typeof (args[0]) === 'object') {
- const obj = args[0];
- for (let key in obj) {
- const reg = new RegExp('({' + key + '})','g');
- if(obj.hasOwnProperty(key)) {
- result = result.replace(reg, obj[key]);
- }
- }
- } else {
- for (let i = 0; i < args.length; i++) {
- if (args[i] !== undefined) {
- const reg = new RegExp('({)' + i + '(})', 'g');
- result = result.replace(reg, args[i]);
- }
- }
- }
- }
- return result;
- }
- }
- module.exports = {
-
- LEVEL: logLevel,
-
- getLogger: function(name, level){
- return new GameLog(name,level);
- },
-
- logLevel: function(level){
- GameLog.logLevel(level);
- }
- };
|