2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webpackで環境ごとにLoggerのLogLevelを設定

Last updated at Posted at 2017-08-15

Webpackを使って環境ごとにJSをビルドする際、環境変数 NODE_ENV を参照してLoggerのLogLevelを変更する。

Webpackバージョン: 3.0.0

Loggerライブラリは loglevel を使用します。
https://github.com/pimterry/loglevel

webpack.config.js
plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      NODE_ENV: `"${process.env.NODE_ENV}"`
    }
  })
]
package.json
"scripts": {
  "development": "NODE_ENV=development webpack --color --progress",
  "production": "NODE_ENV=production webpack --color --progress"
}

NODE_ENVを参照して developmentproduction でLogLevelを変更。
development の場合に log.debug, log.info, log.warn, log.error を表示、
production の場合に log.error のみ表示されるよう設定します。

フロントのJS
import log from 'loglevel';

if (process.env.NODE_ENV === 'development') {
  log.setLevel(log.levels.DEBUG);
} else {
  log.setLevel(log.levels.ERROR);
}

ビルド実行
$ npm run development
$ npm run production

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?