简体   繁体   English

在没有console.log的情况下登录vuejs

[英]Logging in vuejs without console.log

I want to have console logging while I'm developing in VueJS but want it disabled for production.我想在 VueJS 中开发时使用控制台日志记录,但希望在生产中禁用它。

There is a module vuejs-logger that works fine inside of a Vue component and will be disabled in production mode.有一个模块vuejs-logger可以在 Vue 组件内部正常工作,并且在生产模式下将被禁用。 But it will not work (for instance) in my service files or library files (outside of Vue instance), nor in Vuex.但它不会在我的服务文件或库文件(在 Vue 实例之外)中工作(例如),也不会在 Vuex 中工作。

Does anyone have any other suggestions?有没有人有其他建议? Vuejs integrated or otherwise? Vuejs 集成还是其他方式?

Stay safe, TIA保持安全,TIA

Edit: Just to clarify, vuejs-logger Typescript integration is the real problem.编辑:澄清一下,vuejs-logger Typescript 集成才是真正的问题。 It throws up so many TS errors that I cannot use it.它引发了太多的 TS 错误,我无法使用它。 The actual JS logging seems to work... I'm still interested in other solutions that may work smoothly.实际的 JS 日志记录似乎有效......我仍然对其他可能顺利工作的解决方案感兴趣。

Consider building your own simple wrapper which will work everywhere, assuming you are using Webpack , like following:考虑构建您自己的简单包装器,它可以在任何地方使用,假设您使用的是 Webpack ,如下所示:

declare const NODE_ENV: any;

export function log(message: String, level?: 'info' | 'warn' | 'error') {

    // WHEN RUNNING WEBPACK WITH `PRODUCTION` build,
    // IT WILL REMOVE FOLLWOING CODE.

    if (NODE_ENV !== 'production') {

        if (level === 'error') {
            console.error(message);
        } else if (level === 'warn') {
            console.warn(message);
        } else {
            console.log(message);
        }
    }
}

Being a simple function, you can literally import this anywhere in your code including component files or other non-component files.作为一个简单的函数,您可以在代码中的任何地方直接导入它,包括组件文件或其他非组件文件。 The important point here is to note the use of NODE_ENV variable which will be injected by Webpack when running in Production mode.这里的重点是注意NODE_ENV变量的使用, NODE_ENV变量将在生产模式下运行时由 Webpack 注入。 Similar, setup exists for Rollup and other bundlers.类似地,存在 Rollup 和其他打包器的设置。 Read more about this here . 在此处阅读更多相关信息。 When Webpack is bundling the code with production mode , it will ignore the code inside if (NODE_ENV !== 'production') { /* some code */ } construct.当 Webpack 将代码与生产模式捆绑在一起时,它会忽略if (NODE_ENV !== 'production') { /* some code */ }构造中的if (NODE_ENV !== 'production') { /* some code */ }

Also, if you need to wrap it inside a plugin so that it is available on every component instance using this pointer then:此外,如果您需要将其包装在插件中,以便使用this指针在每个组件实例上都可用,则:

const MyLogger = {
    install(Vue: Vue, options: any) {

        // Add an instance method
        Vue.prototype.log = log;
    }
};

Vue.install(MyLogger);

Of course, if you are using TypeScript, then you must teach TypeScript using module augmentation like this:当然,如果您使用的是 TypeScript,那么您必须像这样使用模块扩充来教授 TypeScript:

import Vue from 'vue';

declare module 'vue/types/vue' {
    interface Vue {
        log(message: string, level?: 'info' | 'warn' | 'error'): void;
    }
}

This above snippet should be present inside your typings folder like specified using tsconfig.json .这上面的代码中应该存在里面的typings文件夹,就像使用指定tsconfig.json The directory path would be: ROOT/typings/vue/index.d.ts .目录路径为: ROOT/typings/vue/index.d.ts

In tsconfig.json file, typings array should be set to:tsconfig.json文件, typings阵列应设置为:

"typeRoots": [
    "./node_modules/@types",
    "./typings"
],

It is also possible to use the vuejs3-logger in modules that are not components.也可以在非组件模块中使用vuejs3-logger These are the required steps:这些是必需的步骤:

  1. create a module logger.js with content创建一个包含内容的模块logger.js
import VueLogger from 'vuejs3-logger'
import { createApp } from 'vue'

const isProduction = process.env.NODE_ENV === 'production';

const vlOptions = {
    isEnabled: true,
    logLevel : isProduction ? 'info' : 'debug',
    stringifyArguments : false,
    showLogLevel : true,
    showMethodName : true,
    separator: '|',
    showConsoleColors: true
};

// create a dummy app that can be used as logger in other js modules
const app = createApp({})
app.use(VueLogger, vlOptions);
const logger = app.config.globalProperties.$log;

export {VueLogger, vlOptions, logger};
  1. use this to create your Vue app as usual像往常一样使用它来创建您的 Vue 应用程序
import {createApp} from 'vue'
import {VueLogger, vlOptions} from './logger.js'
import App from '../components/App.vue'

createApp(App)
  .use(VueLogger, vlOptions)
  .mount('#app');
  1. In your external JavaScript module, do this:在您的外部 JavaScript 模块中,执行以下操作:
import {logger} from './logger.js'

logger.debug("Hi there!")

The point is that when you change the logging level in logger.js , all your JavaScript modules will adapt to this logging level as well.关键是当您更改logger.js的日志记录级别时,您的所有 JavaScript 模块也将适应此日志记录级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM