Yuki Takei 8 лет назад
Родитель
Сommit
f7d26abb62

+ 8 - 1
config/webpack.common.js

@@ -3,6 +3,7 @@
  */
  */
 
 
 const webpack = require('webpack');
 const webpack = require('webpack');
+const path = require('path');
 const helpers = require('./helpers');
 const helpers = require('./helpers');
 
 
 /*
 /*
@@ -42,6 +43,12 @@ module.exports = function(options) {
     resolve: {
     resolve: {
       extensions: ['.js', '.json'],
       extensions: ['.js', '.json'],
       modules: [helpers.root('src'), helpers.root('node_modules')],
       modules: [helpers.root('src'), helpers.root('node_modules')],
+      alias: {
+        '@root': path.resolve(__dirname, '../'),
+        '@alias/logger': path.resolve(__dirname, '../lib/service/logger'),
+        // replace bunyan
+        'bunyan': 'browser-bunyan',
+      }
     },
     },
     module: {
     module: {
       rules: [
       rules: [
@@ -112,4 +119,4 @@ module.exports = function(options) {
 
 
     ]
     ]
   };
   };
-}
+};

+ 6 - 1
lib/service/logger/index.js

@@ -1,6 +1,7 @@
-const bunyan = require('bunyan');
+const bunyan = require('bunyan');   // will be replaced to browser-bunyan on browser by webpack
 const minimatch = require('minimatch');
 const minimatch = require('minimatch');
 
 
+const isBrowser = typeof window !== 'undefined';
 const isProd = process.env.NODE_ENV === 'production';
 const isProd = process.env.NODE_ENV === 'production';
 
 
 const config = require('@root/config').logger;
 const config = require('@root/config').logger;
@@ -15,6 +16,10 @@ let loggers = {};
  * @param {string} name Logger name
  * @param {string} name Logger name
  */
  */
 function determineLoggerLevel(name) {
 function determineLoggerLevel(name) {
+  if (isBrowser && isProd) {
+    'error';
+  }
+
   let level = config.default;
   let level = config.default;
 
 
   // retrieve configured level
   // retrieve configured level

+ 12 - 2
lib/service/logger/stream.dev.js

@@ -1,6 +1,16 @@
+const isBrowser = typeof window !== 'undefined';
+
 let stream = undefined;
 let stream = undefined;
 
 
-const bunyanFormat = require('bunyan-format');
-stream = bunyanFormat({ outputMode: 'short' });
+// browser settings
+if (isBrowser) {
+  const ConsoleFormattedStream = require('@browser-bunyan/console-formatted-stream').ConsoleFormattedStream;
+  stream = new ConsoleFormattedStream();
+}
+// node settings
+else {
+  const bunyanFormat = require('bunyan-format');
+  stream = bunyanFormat({ outputMode: 'short' });
+}
 
 
 module.exports = stream;
 module.exports = stream;

+ 14 - 4
lib/service/logger/stream.prod.js

@@ -1,10 +1,20 @@
+const isBrowser = typeof window !== 'undefined';
+
 let stream = undefined;
 let stream = undefined;
 
 
-const isFormat = !(process.env.FORMAT_NODE_LOG === 'false');
+// browser settings
+if (isBrowser) {
+  const ConsoleFormattedStream = require('@browser-bunyan/console-formatted-stream').ConsoleFormattedStream;
+  stream = new ConsoleFormattedStream();
+}
+// node settings
+else {
+  const isFormat = !(process.env.FORMAT_NODE_LOG === 'false');
 
 
-if (isFormat) {
-  const bunyanFormat = require('bunyan-format');
-  stream = bunyanFormat({ outputMode: 'long' });
+  if (isFormat) {
+    const bunyanFormat = require('bunyan-format');
+    stream = bunyanFormat({ outputMode: 'long' });
+  }
 }
 }
 
 
 module.exports = stream;
 module.exports = stream;

+ 9 - 9
lib/util/interceptor-manager.js

@@ -1,4 +1,4 @@
-const debug = require('debug')('crowi:InterceptorManager')
+const logger = require('@alias/logger')('growi:InterceptorManager');
 
 
 /**
 /**
  * the manager class of Interceptor
  * the manager class of Interceptor
@@ -23,7 +23,7 @@ class InterceptorManager {
    */
    */
   addInterceptors(interceptors) {
   addInterceptors(interceptors) {
     const interceptorIds = interceptors.map((i) => i.getId());
     const interceptorIds = interceptors.map((i) => i.getId());
-    debug(`adding interceptors '${interceptorIds}'`);
+    logger.debug(`adding interceptors '${interceptorIds}'`);
     this.interceptors = this.interceptors.concat(interceptors);
     this.interceptors = this.interceptors.concat(interceptors);
   }
   }
 
 
@@ -34,7 +34,7 @@ class InterceptorManager {
    * @param {any} args
    * @param {any} args
    */
    */
   process(contextName, ...args) {
   process(contextName, ...args) {
-    debug(`processing the context '${contextName}'`);
+    logger.debug(`processing the context '${contextName}'`);
 
 
     // filter only contexts matches to specified 'contextName'
     // filter only contexts matches to specified 'contextName'
     const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
     const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
@@ -42,8 +42,8 @@ class InterceptorManager {
     const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
     const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
     const sequentials = matchInterceptors.filter((i) => !i.isProcessableParallel());
     const sequentials = matchInterceptors.filter((i) => !i.isProcessableParallel());
 
 
-    debug(`${parallels.length} parallel interceptors found.`);
-    debug(`${sequentials.length} sequencial interceptors found.`);
+    logger.debug(`${parallels.length} parallel interceptors found.`);
+    logger.debug(`${sequentials.length} sequencial interceptors found.`);
 
 
     return Promise.all(
     return Promise.all(
       // parallel
       // parallel
@@ -57,7 +57,7 @@ class InterceptorManager {
         }, Promise.resolve(...args)/* initial Promise */)
         }, Promise.resolve(...args)/* initial Promise */)
       ])
       ])
     ).then(() => {
     ).then(() => {
-      debug(`end processing the context '${contextName}'`);
+      logger.debug(`end processing the context '${contextName}'`);
       return;
       return;
     });
     });
   }
   }
@@ -65,12 +65,12 @@ class InterceptorManager {
   doProcess(interceptor, contextName, ...args) {
   doProcess(interceptor, contextName, ...args) {
     return interceptor.process(contextName, ...args)
     return interceptor.process(contextName, ...args)
       .then((...results) => {
       .then((...results) => {
-        debug(`processing '${interceptor.getId()}' in the context '${contextName}'`);
+        logger.debug(`processing '${interceptor.getId()}' in the context '${contextName}'`);
         return Promise.resolve(...results);
         return Promise.resolve(...results);
       })
       })
       .catch((reason) => {
       .catch((reason) => {
-        debug(`failed when processing '${interceptor.getId()}' in the context '${contextName}'`);
-        debug(reason);
+        logger.debug(`failed when processing '${interceptor.getId()}' in the context '${contextName}'`);
+        logger.debug(reason);
         return Promise.resolve(...args);
         return Promise.resolve(...args);
       });
       });
   }
   }

+ 1 - 1
package.json

@@ -61,6 +61,7 @@
     "body-parser": "^1.18.2",
     "body-parser": "^1.18.2",
     "bootstrap-sass": "~3.3.6",
     "bootstrap-sass": "~3.3.6",
     "bootstrap-select": "^1.12.4",
     "bootstrap-select": "^1.12.4",
+    "browser-bunyan": "^1.3.0",
     "bunyan": "^1.8.12",
     "bunyan": "^1.8.12",
     "bunyan-debug": "^2.0.0",
     "bunyan-debug": "^2.0.0",
     "bunyan-format": "^0.2.1",
     "bunyan-format": "^0.2.1",
@@ -75,7 +76,6 @@
     "css-loader": "^0.28.0",
     "css-loader": "^0.28.0",
     "csv-to-markdown-table": "^0.4.0",
     "csv-to-markdown-table": "^0.4.0",
     "date-fns": "^1.29.0",
     "date-fns": "^1.29.0",
-    "debug": "^3.1.0",
     "diff": "^3.3.0",
     "diff": "^3.3.0",
     "diff2html": "^2.3.3",
     "diff2html": "^2.3.3",
     "eazy-logger": "^3.0.2",
     "eazy-logger": "^3.0.2",

+ 24 - 0
yarn.lock

@@ -2,6 +2,22 @@
 # yarn lockfile v1
 # yarn lockfile v1
 
 
 
 
+"@browser-bunyan/console-formatted-stream@^1.3.0":
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/@browser-bunyan/console-formatted-stream/-/console-formatted-stream-1.3.0.tgz#3dc059aa5c1b2a7a1f26e2706e2bdeb9a09bbe57"
+  dependencies:
+    "@browser-bunyan/levels" "^1.3.0"
+
+"@browser-bunyan/console-raw-stream@^1.3.0":
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/@browser-bunyan/console-raw-stream/-/console-raw-stream-1.3.0.tgz#ccf24b56f2265058297c6517fbecea84ebb7818c"
+  dependencies:
+    "@browser-bunyan/levels" "^1.3.0"
+
+"@browser-bunyan/levels@^1.3.0":
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/@browser-bunyan/levels/-/levels-1.3.0.tgz#a052303ae5d1a1f9b63eeb3a94495a2f429f4831"
+
 "@sinonjs/formatio@^2.0.0":
 "@sinonjs/formatio@^2.0.0":
   version "2.0.0"
   version "2.0.0"
   resolved "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
   resolved "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
@@ -1167,6 +1183,14 @@ brorand@^1.0.1:
   version "1.1.0"
   version "1.1.0"
   resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
   resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
 
 
+browser-bunyan@^1.3.0:
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/browser-bunyan/-/browser-bunyan-1.3.0.tgz#26378dc58d7a98002cc9bfcfba2ea5d712449992"
+  dependencies:
+    "@browser-bunyan/console-formatted-stream" "^1.3.0"
+    "@browser-bunyan/console-raw-stream" "^1.3.0"
+    "@browser-bunyan/levels" "^1.3.0"
+
 browser-stdout@1.3.0:
 browser-stdout@1.3.0:
   version "1.3.0"
   version "1.3.0"
   resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.0.tgz#f351d32969d32fa5d7a5567154263d928ae3bd1f"
   resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.0.tgz#f351d32969d32fa5d7a5567154263d928ae3bd1f"