@nocobase/logger 0.9.1-alpha.2 → 0.9.2-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,58 +5,41 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createAppLogger = createAppLogger;
7
7
  exports.default = void 0;
8
-
9
8
  function _crypto() {
10
9
  const data = require("crypto");
11
-
12
10
  _crypto = function _crypto() {
13
11
  return data;
14
12
  };
15
-
16
13
  return data;
17
14
  }
18
-
19
15
  function _lodash() {
20
16
  const data = require("lodash");
21
-
22
17
  _lodash = function _lodash() {
23
18
  return data;
24
19
  };
25
-
26
20
  return data;
27
21
  }
28
-
29
22
  var _createLogger = require("./create-logger");
30
-
31
23
  const _excluded = ["skip", "requestWhitelist", "responseWhitelist"];
32
-
33
24
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
34
-
35
25
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
36
-
37
26
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
38
-
39
27
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
40
-
41
28
  const defaultRequestWhitelist = ['action', 'header.x-role', 'header.x-hostname', 'header.x-timezone', 'header.x-locale', 'referer'];
42
29
  const defaultResponseWhitelist = ['status'];
43
-
44
30
  function createAppLogger(options = {}) {
45
31
  const skip = options.skip,
46
- _options$requestWhite = options.requestWhitelist,
47
- requestWhitelist = _options$requestWhite === void 0 ? defaultRequestWhitelist : _options$requestWhite,
48
- _options$responseWhit = options.responseWhitelist,
49
- responseWhitelist = _options$responseWhit === void 0 ? defaultResponseWhitelist : _options$responseWhit,
50
- others = _objectWithoutProperties(options, _excluded);
51
-
32
+ _options$requestWhite = options.requestWhitelist,
33
+ requestWhitelist = _options$requestWhite === void 0 ? defaultRequestWhitelist : _options$requestWhite,
34
+ _options$responseWhit = options.responseWhitelist,
35
+ responseWhitelist = _options$responseWhit === void 0 ? defaultResponseWhitelist : _options$responseWhit,
36
+ others = _objectWithoutProperties(options, _excluded);
52
37
  const instance = (0, _createLogger.createLogger)(others);
53
-
54
38
  const middleware = /*#__PURE__*/function () {
55
39
  var _ref = _asyncToGenerator(function* (ctx, next) {
56
40
  if (skip && (yield skip(ctx))) {
57
41
  return next();
58
42
  }
59
-
60
43
  const logger = ctx.app['logger'];
61
44
  const startTime = Date.now();
62
45
  const info = {
@@ -70,54 +53,43 @@ function createAppLogger(options = {}) {
70
53
  });
71
54
  ctx.logger.info(`BEGIN: ${ctx.method} ${ctx.url}`);
72
55
  let error;
73
-
74
56
  try {
75
57
  yield next();
76
58
  } catch (e) {
77
59
  error = e;
78
60
  } finally {
79
- info['res'] = ctx.response.toJSON(); // info['status'] = ctx.status;
80
-
61
+ info['res'] = ctx.response.toJSON();
62
+ // info['status'] = ctx.status;
81
63
  if (Math.floor(ctx.status / 100) == 5) {
82
64
  var _ctx$body;
83
-
84
65
  info.level = 'error';
85
66
  info['errors'] = ((_ctx$body = ctx.body) === null || _ctx$body === void 0 ? void 0 : _ctx$body['errors']) || ctx.body;
86
67
  } else if (Math.floor(ctx.status / 100) == 4) {
87
68
  var _ctx$body2;
88
-
89
69
  info.level = 'warn';
90
70
  info['errors'] = ((_ctx$body2 = ctx.body) === null || _ctx$body2 === void 0 ? void 0 : _ctx$body2['errors']) || ctx.body;
91
71
  }
92
-
93
72
  info['responseTime'] = Date.now() - startTime;
94
-
95
73
  if (ctx.action) {
96
74
  var _ctx$action, _ctx$action$toJSON;
97
-
98
75
  info['req']['action'] = (_ctx$action = ctx.action) === null || _ctx$action === void 0 ? void 0 : (_ctx$action$toJSON = _ctx$action.toJSON) === null || _ctx$action$toJSON === void 0 ? void 0 : _ctx$action$toJSON.call(_ctx$action);
99
76
  }
100
-
101
77
  info['req'] = (0, _lodash().pick)(info['req'], requestWhitelist);
102
78
  info['res'] = (0, _lodash().pick)(info['res'], responseWhitelist);
103
79
  ctx.logger.log(info);
104
80
  }
105
-
106
81
  if (error) {
107
82
  throw error;
108
83
  }
109
84
  });
110
-
111
85
  return function middleware(_x, _x2) {
112
86
  return _ref.apply(this, arguments);
113
87
  };
114
88
  }();
115
-
116
89
  return {
117
90
  instance,
118
91
  middleware
119
92
  };
120
93
  }
121
-
122
94
  var _default = createAppLogger;
123
95
  exports.default = _default;
@@ -1,5 +1,7 @@
1
1
  import winston, { Logger } from 'winston';
2
2
  import 'winston-daily-rotate-file';
3
+ export declare function getLoggerLevel(): string;
4
+ export declare function getLoggerFilePath(...paths: string[]): string;
3
5
  declare const Transports: {
4
6
  console(options: any): winston.transports.ConsoleTransportInstance;
5
7
  dailyRotateFile(options: any): import("winston-daily-rotate-file");
@@ -11,95 +11,77 @@ Object.defineProperty(exports, "Logger", {
11
11
  });
12
12
  exports.Transports = void 0;
13
13
  exports.createLogger = createLogger;
14
-
14
+ exports.getLoggerFilePath = getLoggerFilePath;
15
+ exports.getLoggerLevel = getLoggerLevel;
15
16
  function _path() {
16
17
  const data = _interopRequireDefault(require("path"));
17
-
18
18
  _path = function _path() {
19
19
  return data;
20
20
  };
21
-
22
21
  return data;
23
22
  }
24
-
25
23
  function _winston() {
26
24
  const data = _interopRequireWildcard(require("winston"));
27
-
28
25
  _winston = function _winston() {
29
26
  return data;
30
27
  };
31
-
32
28
  return data;
33
29
  }
34
-
35
30
  require("winston-daily-rotate-file");
36
-
37
31
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
38
-
39
32
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
40
-
41
33
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
-
43
34
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
44
-
45
35
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
46
-
47
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
48
-
36
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
37
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
38
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
49
39
  const combine = _winston().format.combine,
50
- timestamp = _winston().format.timestamp,
51
- colorize = _winston().format.colorize,
52
- simple = _winston().format.simple;
53
-
54
- function loggingLevel() {
40
+ timestamp = _winston().format.timestamp,
41
+ colorize = _winston().format.colorize,
42
+ simple = _winston().format.simple;
43
+ function getLoggerLevel() {
55
44
  return process.env.LOGGER_LEVEL || 'info';
56
45
  }
57
-
46
+ function getLoggerFilePath(...paths) {
47
+ return _path().default.resolve(process.env.LOGGER_BASE_PATH || _path().default.resolve(process.cwd(), 'storage', 'logs'), ...paths);
48
+ }
58
49
  const Transports = {
59
50
  console(options) {
60
51
  return new (_winston().default.transports.Console)(_objectSpread({
61
52
  format: combine(simple(), colorize())
62
53
  }, options));
63
54
  },
64
-
65
55
  dailyRotateFile(options) {
66
- let dirname = process.env.DAILY_ROTATE_FILE_DIRNAME || _path().default.resolve(process.cwd(), './storage/logs');
67
-
56
+ let dirname = getLoggerFilePath();
68
57
  if (!_path().default.isAbsolute(dirname)) {
69
58
  dirname = _path().default.resolve(process.cwd(), dirname);
70
59
  }
71
-
72
60
  return new (_winston().default.transports.DailyRotateFile)(_objectSpread({
73
61
  dirname,
74
- level: loggingLevel(),
62
+ level: getLoggerLevel(),
75
63
  filename: 'nocobase-%DATE%.log',
76
64
  datePattern: 'YYYY-MM-DD-HH',
77
65
  maxFiles: '14d'
78
66
  }, options));
79
67
  }
80
-
81
68
  };
82
69
  exports.Transports = Transports;
83
-
84
70
  function toArr(value) {
85
71
  if (Array.isArray(value)) {
86
72
  return value;
87
73
  }
88
-
89
74
  return value ? value.split(',') : [];
90
75
  }
91
-
92
76
  function createLogger(options = {}) {
93
77
  const transports = toArr((options === null || options === void 0 ? void 0 : options.transports) || ['console', 'dailyRotateFile']).map(t => {
94
78
  if (typeof t === 'string') {
95
79
  return Transports[t]();
96
80
  }
97
-
98
81
  return t;
99
82
  }).filter(t => t);
100
-
101
83
  const logger = _winston().default.createLogger(_objectSpread(_objectSpread({
102
- level: loggingLevel(),
84
+ level: getLoggerLevel(),
103
85
  levels: _winston().default.config.cli.levels,
104
86
  format: combine(timestamp(), _winston().format.errors({
105
87
  stack: true
@@ -107,8 +89,6 @@ function createLogger(options = {}) {
107
89
  }, options), {}, {
108
90
  transports
109
91
  }));
110
-
111
92
  _winston().default.addColors(_winston().default.config.cli.colors);
112
-
113
93
  return logger;
114
94
  }
package/lib/index.js CHANGED
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
-
7
6
  var _createAppLogger = require("./create-app-logger");
8
-
9
7
  Object.keys(_createAppLogger).forEach(function (key) {
10
8
  if (key === "default" || key === "__esModule") return;
11
9
  if (key in exports && exports[key] === _createAppLogger[key]) return;
@@ -16,9 +14,7 @@ Object.keys(_createAppLogger).forEach(function (key) {
16
14
  }
17
15
  });
18
16
  });
19
-
20
17
  var _createLogger = require("./create-logger");
21
-
22
18
  Object.keys(_createLogger).forEach(function (key) {
23
19
  if (key === "default" || key === "__esModule") return;
24
20
  if (key in exports && exports[key] === _createLogger[key]) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/logger",
3
- "version": "0.9.1-alpha.2",
3
+ "version": "0.9.2-alpha.2",
4
4
  "description": "nocobase logging library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
@@ -15,5 +15,5 @@
15
15
  "winston": "^3.8.2",
16
16
  "winston-daily-rotate-file": "^4.7.1"
17
17
  },
18
- "gitHead": "d588a68eca4feed4642a4cb317301011266fe5c9"
18
+ "gitHead": "1a0de6908b2b1854bdf434fcafa8909cb65bb8a3"
19
19
  }
@@ -4,10 +4,14 @@ import 'winston-daily-rotate-file';
4
4
 
5
5
  const { combine, timestamp, colorize, simple } = format;
6
6
 
7
- function loggingLevel() {
7
+ export function getLoggerLevel(): string {
8
8
  return process.env.LOGGER_LEVEL || 'info';
9
9
  }
10
10
 
11
+ export function getLoggerFilePath(...paths: string[]): string {
12
+ return path.resolve(process.env.LOGGER_BASE_PATH || path.resolve(process.cwd(), 'storage', 'logs'), ...paths);
13
+ }
14
+
11
15
  const Transports = {
12
16
  console(options) {
13
17
  return new winston.transports.Console({
@@ -16,13 +20,13 @@ const Transports = {
16
20
  });
17
21
  },
18
22
  dailyRotateFile(options: any) {
19
- let dirname = process.env.DAILY_ROTATE_FILE_DIRNAME || path.resolve(process.cwd(), './storage/logs');
23
+ let dirname = getLoggerFilePath();
20
24
  if (!path.isAbsolute(dirname)) {
21
25
  dirname = path.resolve(process.cwd(), dirname);
22
26
  }
23
27
  return new winston.transports.DailyRotateFile({
24
28
  dirname,
25
- level: loggingLevel(),
29
+ level: getLoggerLevel(),
26
30
  filename: 'nocobase-%DATE%.log',
27
31
  datePattern: 'YYYY-MM-DD-HH',
28
32
  maxFiles: '14d',
@@ -54,7 +58,7 @@ function createLogger(options: LoggerOptions = {}) {
54
58
  })
55
59
  .filter((t) => t);
56
60
  const logger = winston.createLogger({
57
- level: loggingLevel(),
61
+ level: getLoggerLevel(),
58
62
  levels: winston.config.cli.levels,
59
63
  format: combine(timestamp(), format.errors({ stack: true }), format.json(), colorize()),
60
64
  ...options,