coreprocess / babel-plugin-js-logger

Babel plugin to enable js-logger in your entire project efficiently.
13 stars 4 forks source link

Doesn't work with Webpack2, Babel 6.24 #2

Closed ethyde closed 7 years ago

ethyde commented 7 years ago

Hello,

I try to use your plugin with a basic Webpack/Babel Configuration, but i never be able to use logger.info, there is no error, but no logs too.

Can you help me ?

My confing :

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "Firefox ESR"]
      },
      "production": {
        "presets": ["babili"]
      }
    }],
    "stage-0"
  ],
  "plugins": [ "transform-runtime", [ "js-logger", { "format": { "separator": "::", "project": true } } ] ]
}

webpack.config.babel.js

const configWebpackDev = {
  devtool: '#eval',
  context: path.resolve(__dirname, 'app'),
  entry: './app.js',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('developpement')
    })
  ],
  resolve: {
    modules: ['node_modules'],
    extensions: ['.js']
  }
}

package.json

"devDependencies": {
    "babel-core": "^6.25.0",
    "babel-jest": "^20.0.3",
    "babel-loader": "^7.0.0",
    "babel-plugin-external-helpers": "^6.22.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.5.2",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.24.1",
    "babili": "^0.1.2",
    "cross-env": "^5.0.1",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  },
  "dependencies": {
    "babel-runtime": "^6.23.0"
  }
coreprocess commented 7 years ago

Hi @ethyde,

Attached you will find a complete example, which uses the plugin in the backend and the frontend. Does this fix the problem? :-)

logger-example.tar.gz

# unpack the tar.gz file
tar xfzv logger-example.tar.gz
cd logger-example

# install the required modules
yarn # or: npm install

# build all
npm run build

# run the backend
npm start

# open the frontend: http://localhost:3000/
coreprocess commented 7 years ago

Just published the example as github project here: https://github.com/core-process/logger-example

ethyde commented 7 years ago

It's works ! Thanks a lot.

Just another question, is it possible to make it conditional depending on the production or development NODE_ENV value ?

coreprocess commented 7 years ago

@ethyde Awesome!

Currently it is not supported, but we could generate a dummy logger variable when in production mode which provides NOOP logging routines. I will look into this next week.

In case you want to develop this feature yourself, please provide a pull request :-)!

coreprocess commented 7 years ago

@ethyde

Well actually, we do not need any magic for this.

You could evaluate your conditions in the initialization routine (e.g. https://github.com/core-process/logger-example/blob/master/src/frontend/logger-init.js#L2) and use Logger.setLevel(Logger.OFF);.

See https://github.com/jonnyreeves/js-logger for more.

coreprocess commented 7 years ago
const Logger = require('js-logger');
Logger.useDefaults();
if(process.env.NODE_ENV == 'production') {
  Logger.setLevel(Logger.OFF);
}
coreprocess commented 7 years ago

@ethyde Just updated https://github.com/core-process/logger-example to enable configuration of logging via NODE_ENV.

ethyde commented 7 years ago

Many Thanks for your quick and nice help ! :)