为Vue.js添加友好日志

const isDebugEnabled = process.env.NODE_ENV !== "production";
const isInfoEnabled = true;
const isErrorEnabled = true;

let loggerName = "default";

console.log(
  "isDebugEnabled,isInfoEnabled,isErrorEnabled=>",
  `${isDebugEnabled},${isInfoEnabled},${isErrorEnabled}`
);

const debug = log => {
  if (isDebugEnabled) {
    console.warn("[", loggerName, "]", log);
  }
};

const info = log => {
  if (isInfoEnabled) {
    console.log("[", loggerName, "]", log);
  }
};

const error = log => {
  if (isErrorEnabled) {
    console.error("[", loggerName, "]", log);
  }
};

const logger = {
  debug,
  info,
  error
};

export const getLogger = name => {
  if (name) {
    loggerName = name;
  }
  return logger;
};

使用

import { getLogger } from "../util/logger";
const logger = getLogger("pages/index");

logger.info("fetch siteConfig and postList finish");
posted @ 2019-03-19 17:29  灯塔下的守望者  阅读(754)  评论(0编辑  收藏  举报