@autofleet/logger 1.2.3 → 1.2.4

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.
@@ -0,0 +1,16 @@
1
+ interface EntityObject {
2
+ entityId: string;
3
+ entityType: string;
4
+ businessModelId?: string;
5
+ fleetId?: string;
6
+ demandSourceId?: string;
7
+ }
8
+ export default class DebugConsoleLog {
9
+ entites: EntityObject;
10
+ constructor(entites: EntityObject);
11
+ private generateLevelLog;
12
+ info(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
13
+ warn(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
14
+ error(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
15
+ }
16
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const Events = require('@autofleet/events');
16
+ const logger_1 = __importDefault(require("./logger"));
17
+ const events = new Events({ logger: logger_1.default });
18
+ class DebugConsoleLog {
19
+ constructor(entites) {
20
+ this.entites = entites;
21
+ }
22
+ generateLevelLog(level) {
23
+ return (msg, payload, additonalEntityies) => __awaiter(this, void 0, void 0, function* () {
24
+ logger_1.default[level](msg, payload);
25
+ events.send('log_events', 1, Object.assign(Object.assign({ action: 'logEvent', level }, this.entites), { payload: JSON.stringify(payload) }));
26
+ });
27
+ }
28
+ info(msg, payload, additonalEntityies) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ return this.generateLevelLog('info')(msg, payload, additonalEntityies);
31
+ });
32
+ }
33
+ warn(msg, payload, additonalEntityies) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ return this.generateLevelLog('warn')(msg, payload, additonalEntityies);
36
+ });
37
+ }
38
+ error(msg, payload, additonalEntityies) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ return this.generateLevelLog('error')(msg, payload, additonalEntityies);
41
+ });
42
+ }
43
+ }
44
+ exports.default = DebugConsoleLog;
@@ -0,0 +1,4 @@
1
+ import logger from './logger';
2
+ import DebugConsoleLog from './debug-console-log';
3
+ export { DebugConsoleLog };
4
+ export default logger;
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DebugConsoleLog = void 0;
7
+ const logger_1 = __importDefault(require("./logger"));
8
+ const debug_console_log_1 = __importDefault(require("./debug-console-log"));
9
+ exports.DebugConsoleLog = debug_console_log_1.default;
10
+ exports.default = logger_1.default;
@@ -0,0 +1,4 @@
1
+ import winston from 'winston';
2
+ declare const logger: winston.Logger;
3
+ export declare const httpMorganInfo: (options: any) => void;
4
+ export default logger;
package/dist/logger.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.httpMorganInfo = void 0;
7
+ const winston_1 = __importDefault(require("winston"));
8
+ const { createLogger } = winston_1.default;
9
+ require('dotenv').config();
10
+ const { env } = process;
11
+ const isProd = env.NODE_ENV === 'production';
12
+ const infoEnvNmaes = [
13
+ 'production',
14
+ 'staging',
15
+ 'test',
16
+ ];
17
+ const serializeError = (error) => {
18
+ const fields = ['code', 'status', 'statusCode', 'message', 'messageData', 'name', 'stack', 'stackTrace'];
19
+ return fields.reduce((v, field) => (Object.assign(Object.assign({}, v), { [field]: error[field] })), {});
20
+ };
21
+ const getLevel = () => {
22
+ if (env.LOG_LEVEL)
23
+ return env.LOG_LEVEL;
24
+ if (infoEnvNmaes.includes(env.NODE_ENV || ''))
25
+ return 'info';
26
+ if (env.NODE_ENV === 'development')
27
+ return 'debug';
28
+ return 'debug';
29
+ };
30
+ const enumerateErrorFormat = winston_1.default.format((info) => {
31
+ // fix for this crap: https://github.com/googleapis/nodejs-logging-winston/issues/285
32
+ // eslint-disable-next-line no-param-reassign
33
+ info.severity = info.level.toUpperCase();
34
+ if (info instanceof Error) {
35
+ return Object.assign({
36
+ message: info.message,
37
+ stack: info.stack,
38
+ }, info);
39
+ }
40
+ // eslint-disable-next-line array-callback-return
41
+ Object.keys(info).map((k) => {
42
+ if (info[k] instanceof Error) {
43
+ // eslint-disable-next-line no-param-reassign
44
+ info[k] = serializeError(info[k]);
45
+ }
46
+ });
47
+ return info;
48
+ });
49
+ const getFormat = () => {
50
+ if (isProd) {
51
+ return winston_1.default.format.combine(enumerateErrorFormat(), winston_1.default.format.json());
52
+ }
53
+ return winston_1.default.format.combine(enumerateErrorFormat(), winston_1.default.format.splat(), winston_1.default.format.colorize(), winston_1.default.format.simple());
54
+ };
55
+ const logger = createLogger({
56
+ level: getLevel(),
57
+ format: getFormat(),
58
+ transports: [
59
+ new winston_1.default.transports.Console(),
60
+ ],
61
+ exitOnError: false,
62
+ });
63
+ const httpMorganInfo = (options) => {
64
+ logger.info(options.url, {
65
+ httpRequest: {
66
+ status: options.status,
67
+ requestUrl: options.url,
68
+ requestMethod: options.method,
69
+ responseSize: options['content-length'],
70
+ latency: {
71
+ seconds: parseInt(options['response-time'], 10) / 1000,
72
+ nanos: parseInt(options['response-time'], 10) * 1000000,
73
+ },
74
+ userAgent: options['user-agent'],
75
+ },
76
+ });
77
+ };
78
+ exports.httpMorganInfo = httpMorganInfo;
79
+ exports.default = logger;
@@ -0,0 +1,16 @@
1
+ interface EntityObject {
2
+ entityId: string;
3
+ entityType: string;
4
+ businessModelId?: string;
5
+ fleetId?: string;
6
+ demandSourceId?: string;
7
+ }
8
+ export default class DebugConsoleLog {
9
+ entites: EntityObject;
10
+ constructor(entites: EntityObject);
11
+ private generateLevelLog;
12
+ info(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
13
+ warn(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
14
+ error(msg: string, payload?: any, additonalEntityies?: any): Promise<void>;
15
+ }
16
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const events_1 = __importDefault(require("@autofleet/events"));
16
+ const logger_1 = __importDefault(require("./logger"));
17
+ const events = new events_1.default();
18
+ class DebugConsoleLog {
19
+ constructor(entites) {
20
+ this.entites = entites;
21
+ }
22
+ generateLevelLog(level) {
23
+ return (msg, payload, additonalEntityies) => __awaiter(this, void 0, void 0, function* () {
24
+ logger_1.default[level](msg, payload);
25
+ events.send('log_events', 1, Object.assign(Object.assign({ action: 'logEvent', level }, this.entites), { payload: JSON.stringify(payload) }));
26
+ });
27
+ }
28
+ info(msg, payload, additonalEntityies) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ return this.generateLevelLog('info')(msg, payload, additonalEntityies);
31
+ });
32
+ }
33
+ warn(msg, payload, additonalEntityies) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ return this.generateLevelLog('warn')(msg, payload, additonalEntityies);
36
+ });
37
+ }
38
+ error(msg, payload, additonalEntityies) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ return this.generateLevelLog('error')(msg, payload, additonalEntityies);
41
+ });
42
+ }
43
+ }
44
+ exports.default = DebugConsoleLog;
@@ -0,0 +1,4 @@
1
+ import logger from './logger';
2
+ import DebugConsoleLog from './debug-console-log';
3
+ export { DebugConsoleLog };
4
+ export default logger;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DebugConsoleLog = void 0;
7
+ const logger_1 = __importDefault(require("./logger"));
8
+ const debug_console_log_1 = __importDefault(require("./debug-console-log"));
9
+ exports.DebugConsoleLog = debug_console_log_1.default;
10
+ exports.default = logger_1.default;
@@ -0,0 +1,4 @@
1
+ import winston from 'winston';
2
+ declare const logger: winston.Logger;
3
+ export declare const httpMorganInfo: (options: any) => void;
4
+ export default logger;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.httpMorganInfo = void 0;
7
+ const winston_1 = __importDefault(require("winston"));
8
+ const { createLogger } = winston_1.default;
9
+ require('dotenv').config();
10
+ const { env } = process;
11
+ const isProd = env.NODE_ENV === 'production';
12
+ const infoEnvNmaes = [
13
+ 'production',
14
+ 'staging',
15
+ 'test',
16
+ ];
17
+ const serializeError = (error) => {
18
+ const fields = ['code', 'status', 'statusCode', 'message', 'messageData', 'name', 'stack', 'stackTrace'];
19
+ return fields.reduce((v, field) => (Object.assign(Object.assign({}, v), { [field]: error[field] })), {});
20
+ };
21
+ const getLevel = () => {
22
+ if (env.LOG_LEVEL)
23
+ return env.LOG_LEVEL;
24
+ if (infoEnvNmaes.includes(env.NODE_ENV || ''))
25
+ return 'info';
26
+ if (env.NODE_ENV === 'development')
27
+ return 'debug';
28
+ return 'debug';
29
+ };
30
+ const enumerateErrorFormat = winston_1.default.format((info) => {
31
+ // fix for this crap: https://github.com/googleapis/nodejs-logging-winston/issues/285
32
+ // eslint-disable-next-line no-param-reassign
33
+ info.severity = info.level.toUpperCase();
34
+ if (info instanceof Error) {
35
+ return Object.assign({
36
+ message: info.message,
37
+ stack: info.stack,
38
+ }, info);
39
+ }
40
+ // eslint-disable-next-line array-callback-return
41
+ Object.keys(info).map((k) => {
42
+ if (info[k] instanceof Error) {
43
+ // eslint-disable-next-line no-param-reassign
44
+ info[k] = serializeError(info[k]);
45
+ }
46
+ });
47
+ return info;
48
+ });
49
+ const getFormat = () => {
50
+ if (isProd) {
51
+ return winston_1.default.format.combine(enumerateErrorFormat(), winston_1.default.format.json());
52
+ }
53
+ return winston_1.default.format.combine(enumerateErrorFormat(), winston_1.default.format.splat(), winston_1.default.format.colorize(), winston_1.default.format.simple());
54
+ };
55
+ const logger = createLogger({
56
+ level: getLevel(),
57
+ format: getFormat(),
58
+ transports: [
59
+ new winston_1.default.transports.Console(),
60
+ ],
61
+ exitOnError: false,
62
+ });
63
+ const httpMorganInfo = (options) => {
64
+ logger.info(options.url, {
65
+ httpRequest: {
66
+ status: options.status,
67
+ requestUrl: options.url,
68
+ requestMethod: options.method,
69
+ responseSize: options['content-length'],
70
+ latency: {
71
+ seconds: parseInt(options['response-time'], 10) / 1000,
72
+ nanos: parseInt(options['response-time'], 10) * 1000000,
73
+ },
74
+ userAgent: options['user-agent'],
75
+ },
76
+ });
77
+ };
78
+ exports.httpMorganInfo = httpMorganInfo;
79
+ exports.default = logger;
package/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const winston = require('winston');
2
2
  const sizeof = require('object-sizeof');
3
- const Events = require('@autofleet/events');
4
3
 
5
4
  const { createLogger } = winston;
6
5
  require('dotenv').config();
@@ -83,23 +82,6 @@ const getFormat = () => {
83
82
  );
84
83
  };
85
84
 
86
- const clientFacingLogLevel = [
87
- 'error',
88
- 'warn',
89
- 'info',
90
- ];
91
-
92
-
93
- const createClientFacingLog = (logger, level, events) => async (msg, entities, payload) => {
94
- logger[level](msg, payload);
95
- events.send('log_events', 1, {
96
- action: 'logEvent',
97
- level,
98
- ...entities,
99
- payload: JSON.stringify(payload),
100
- });
101
- };
102
-
103
85
  const createLoggerInstance = (level, exceptionHandlers) => {
104
86
  const logger = createLogger({
105
87
  level,
@@ -130,13 +112,6 @@ const createLoggerInstance = (level, exceptionHandlers) => {
130
112
  logger.httpMorganInfo = undefined;
131
113
  }
132
114
 
133
-
134
- const events = new Events({ logger });
135
- logger.events = events;
136
- clientFacingLogLevel.forEach((currentLevel) => {
137
- logger[`clientFacing${currentLevel.charAt(0).toUpperCase()}${currentLevel.substring(1)}`] = createClientFacingLog(logger, currentLevel, events);
138
- });
139
-
140
115
  return logger;
141
116
  };
142
117
 
package/index.test.js CHANGED
@@ -1,4 +1,3 @@
1
- const EventsMock = require('@autofleet/events/mocks');
2
1
  const Logger = require('./index');
3
2
 
4
3
  const { env } = process;
@@ -51,28 +50,5 @@ describe('Logger', () => {
51
50
  expect(logger.verbose).toBeFunction();
52
51
  expect(logger.debug).toBeFunction();
53
52
  expect(logger.silly).toBeFunction();
54
- expect(logger.clientFacingInfo).toBeFunction();
55
- expect(logger.clientFacingWarn).toBeFunction();
56
- expect(logger.clientFacingError).toBeFunction();
57
- });
58
-
59
- it('Sends event with client facing methods', () => {
60
- const logger = Logger();
61
- const eventsMock = EventsMock.mockSendEvent(logger.events);
62
- logger.clientFacingInfo('Message', { businessModelId: 'uuid' }, { attr1: 'Value' });
63
- eventsMock.calledWithAndTimes([
64
- [
65
- 'log_events',
66
- 1,
67
- {
68
- action: 'logEvent',
69
- businessModelId: 'uuid',
70
- level: 'info',
71
- payload: JSON.stringify({
72
- attr1: 'Value',
73
- }),
74
- },
75
- ],
76
- ]);
77
53
  });
78
54
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/logger",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "coverage": "jest --coverage --forceExit --runInBand",
@@ -23,9 +23,7 @@
23
23
  },
24
24
  "homepage": "https://github.com/Autofleet/logger",
25
25
  "dependencies": {
26
- "@autofleet/events": "^1.3.1",
27
26
  "@google-cloud/logging-winston": "^0.10.2",
28
- "@google-cloud/pubsub": "^0.20.1",
29
27
  "dotenv": "^5.0.1",
30
28
  "jest": "^22.4.4",
31
29
  "object-sizeof": "^1.6.1",
@@ -0,0 +1,44 @@
1
+ const Events = require('@autofleet/events');
2
+ import logger from './logger';
3
+
4
+
5
+ interface EntityObject {
6
+ entityId: string;
7
+ entityType: string;
8
+ businessModelId?: string;
9
+ fleetId?: string;
10
+ demandSourceId?: string;
11
+ }
12
+
13
+ const events = new Events({ logger });
14
+
15
+ export default class DebugConsoleLog {
16
+ entites: EntityObject;
17
+ constructor(entites: EntityObject) {
18
+ this.entites = entites;
19
+ }
20
+
21
+ private generateLevelLog(level: 'info' | 'warn'| 'error') {
22
+ return async (msg: string, payload: any, additonalEntityies?: any) => {
23
+ logger[level](msg, payload);
24
+ events.send('log_events', 1, {
25
+ action: 'logEvent',
26
+ level,
27
+ ...this.entites,
28
+ payload: JSON.stringify(payload),
29
+ });
30
+ }
31
+ }
32
+
33
+ async info(msg: string, payload?: any, additonalEntityies?: any) {
34
+ return this.generateLevelLog('info')(msg, payload, additonalEntityies);
35
+ }
36
+
37
+ async warn(msg: string, payload?: any, additonalEntityies?: any) {
38
+ return this.generateLevelLog('warn')(msg, payload, additonalEntityies);
39
+ }
40
+
41
+ async error(msg: string, payload?: any, additonalEntityies?: any) {
42
+ return this.generateLevelLog('error')(msg, payload, additonalEntityies);
43
+ }
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import logger from './logger';
2
+ import DebugConsoleLog from './debug-console-log';
3
+
4
+ export {
5
+ DebugConsoleLog
6
+ };
7
+
8
+ export default logger;
package/src/logger.ts ADDED
@@ -0,0 +1,91 @@
1
+ import winston from 'winston';
2
+
3
+ const { createLogger } = winston;
4
+ require('dotenv').config();
5
+
6
+ const { env } = process;
7
+
8
+ const isProd = env.NODE_ENV === 'production';
9
+
10
+ const infoEnvNmaes = [
11
+ 'production',
12
+ 'staging',
13
+ 'test',
14
+ ];
15
+
16
+ const serializeError = (error: any) => {
17
+ const fields = ['code', 'status', 'statusCode', 'message', 'messageData', 'name', 'stack', 'stackTrace'];
18
+ return fields.reduce((v, field) => ({ ...v, [field]: error[field] }), {});
19
+ };
20
+
21
+ const getLevel = () => {
22
+ if (env.LOG_LEVEL) return env.LOG_LEVEL;
23
+ if (infoEnvNmaes.includes(env.NODE_ENV || '')) return 'info';
24
+ if (env.NODE_ENV === 'development') return 'debug';
25
+ return 'debug';
26
+ };
27
+
28
+ const enumerateErrorFormat = winston.format((info) => {
29
+ // fix for this crap: https://github.com/googleapis/nodejs-logging-winston/issues/285
30
+ // eslint-disable-next-line no-param-reassign
31
+ info.severity = info.level.toUpperCase();
32
+ if (info instanceof Error) {
33
+ return Object.assign({
34
+ message: info.message,
35
+ stack: info.stack,
36
+ }, info);
37
+ }
38
+
39
+ // eslint-disable-next-line array-callback-return
40
+ Object.keys(info).map((k) => {
41
+ if (info[k] instanceof Error) {
42
+ // eslint-disable-next-line no-param-reassign
43
+ info[k] = serializeError(info[k]);
44
+ }
45
+ });
46
+
47
+ return info;
48
+ });
49
+
50
+ const getFormat = () => {
51
+ if (isProd) {
52
+ return winston.format.combine(
53
+ enumerateErrorFormat(),
54
+ winston.format.json(),
55
+ );
56
+ }
57
+
58
+ return winston.format.combine(
59
+ enumerateErrorFormat(),
60
+ winston.format.splat(),
61
+ winston.format.colorize(),
62
+ winston.format.simple(),
63
+ );
64
+ };
65
+
66
+ const logger = createLogger({
67
+ level: getLevel(),
68
+ format: getFormat(),
69
+ transports: [
70
+ new winston.transports.Console(),
71
+ ],
72
+ exitOnError: false,
73
+ });
74
+
75
+ export const httpMorganInfo = (options: any) => {
76
+ logger.info(options.url, {
77
+ httpRequest: {
78
+ status: options.status,
79
+ requestUrl: options.url,
80
+ requestMethod: options.method,
81
+ responseSize: options['content-length'],
82
+ latency: {
83
+ seconds: parseInt(options['response-time'], 10) / 1000,
84
+ nanos: parseInt(options['response-time'], 10) * 1000000,
85
+ },
86
+ userAgent: options['user-agent'],
87
+ },
88
+ });
89
+ };
90
+
91
+ export default logger;
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "esModuleInterop": true
9
+ }
10
+ }