@trayio/winston 0.11.0

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.
package/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Tray.io, Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ import { DynamicObject } from '@trayio/commons/dynamictype/DynamicType';
2
+ import { CliLogging, CliColors } from '@trayio/commons/cli/CliLogging';
3
+ export declare class WinstonCliLogging implements CliLogging {
4
+ private logger;
5
+ constructor();
6
+ log(message: string | DynamicObject): void;
7
+ logBlock(message: string, color: CliColors): void;
8
+ }
9
+ //# sourceMappingURL=WinstonCliLogging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WinstonCliLogging.d.ts","sourceRoot":"","sources":["../../src/cli/WinstonCliLogging.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAWvE,qBAAa,iBAAkB,YAAW,UAAU;IACnD,OAAO,CAAC,MAAM,CAAgB;;IA8B9B,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa;IAInC,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS;CAkB1C"}
@@ -0,0 +1,60 @@
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.WinstonCliLogging = void 0;
7
+ const CliLogging_1 = require("@trayio/commons/cli/CliLogging");
8
+ const json_colorizer_1 = __importDefault(require("json-colorizer"));
9
+ const winston_1 = require("winston");
10
+ const chalk_1 = __importDefault(require("chalk"));
11
+ class WinstonCliLogging {
12
+ constructor() {
13
+ const logFormatter = (info) => {
14
+ if (typeof info.message === 'object') {
15
+ const messageStr = JSON.stringify(info.message, null, 2);
16
+ return (0, json_colorizer_1.default)(messageStr, {
17
+ colors: {
18
+ STRING_KEY: 'white',
19
+ STRING_LITERAL: 'green',
20
+ NUMBER_LITERAL: 'yellow',
21
+ BRACE: 'gray',
22
+ BRACKET: 'white',
23
+ COLON: 'gray',
24
+ COMMA: 'gray',
25
+ BOOLEAN_LITERAL: 'yellow',
26
+ NULL_LITERAL: 'blue',
27
+ },
28
+ });
29
+ }
30
+ return info.message;
31
+ };
32
+ this.logger = (0, winston_1.createLogger)({
33
+ level: 'info',
34
+ format: winston_1.format.combine(winston_1.format.splat(), winston_1.format.printf(logFormatter)),
35
+ transports: [new winston_1.transports.Console()],
36
+ });
37
+ }
38
+ log(message) {
39
+ this.logger.info(message);
40
+ }
41
+ logBlock(message, color) {
42
+ let messageBlock;
43
+ switch (color) {
44
+ case CliLogging_1.CliColors.blue:
45
+ messageBlock = chalk_1.default.white.bgBlue(` ${message} `);
46
+ break;
47
+ case CliLogging_1.CliColors.red:
48
+ messageBlock = chalk_1.default.white.bgRed(` ${message} `);
49
+ break;
50
+ case CliLogging_1.CliColors.yellow:
51
+ messageBlock = chalk_1.default.white.bgYellow(` ${message} `);
52
+ break;
53
+ default:
54
+ messageBlock = chalk_1.default.white.bgBlue(` ${message} `);
55
+ break;
56
+ }
57
+ this.logger.info(messageBlock);
58
+ }
59
+ }
60
+ exports.WinstonCliLogging = WinstonCliLogging;
@@ -0,0 +1,14 @@
1
+ import { ObservabilityContext, Logging } from '@trayio/commons/logging/Logging';
2
+ declare class WinstonLogging implements Logging {
3
+ private winstonLogger;
4
+ private context;
5
+ constructor(context?: ObservabilityContext);
6
+ info(message: string, context: ObservabilityContext): void;
7
+ debug(message: string, context: ObservabilityContext): void;
8
+ error(message: string, context: ObservabilityContext): void;
9
+ warning(message: string, context: ObservabilityContext): void;
10
+ private getDefaultContext;
11
+ }
12
+ export declare const getLogger: (context?: ObservabilityContext) => WinstonLogging;
13
+ export {};
14
+ //# sourceMappingURL=WinstonLogging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WinstonLogging.d.ts","sourceRoot":"","sources":["../../src/logging/WinstonLogging.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAEhF,cAAM,cAAe,YAAW,OAAO;IACtC,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,OAAO,CAAuB;gBAE1B,OAAO,GAAE,oBAAyB;IAU9C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB;IAInD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB;IAIpD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB;IAIpD,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB;IAItD,OAAO,CAAC,iBAAiB;CAOzB;AAED,eAAO,MAAM,SAAS,aAAa,oBAAoB,mBAC3B,CAAC"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLogger = void 0;
4
+ const winston_1 = require("winston");
5
+ class WinstonLogging {
6
+ constructor(context = {}) {
7
+ this.winstonLogger = (0, winston_1.createLogger)({
8
+ format: winston_1.format.json(),
9
+ exitOnError: false,
10
+ transports: [new winston_1.transports.Console()],
11
+ });
12
+ this.context = this.getDefaultContext(context);
13
+ }
14
+ info(message, context) {
15
+ this.winstonLogger.info(message, Object.assign(Object.assign({}, this.context), context));
16
+ }
17
+ debug(message, context) {
18
+ this.winstonLogger.debug(message, Object.assign(Object.assign({}, this.context), context));
19
+ }
20
+ error(message, context) {
21
+ this.winstonLogger.error(message, Object.assign(Object.assign({}, this.context), context));
22
+ }
23
+ warning(message, context) {
24
+ this.winstonLogger.warn(message, Object.assign(Object.assign({}, this.context), context));
25
+ }
26
+ getDefaultContext(context) {
27
+ context.service = process.env.npm_package_name;
28
+ return context;
29
+ }
30
+ }
31
+ const getLogger = (context = {}) => new WinstonLogging(context);
32
+ exports.getLogger = getLogger;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=WinstonLogging.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WinstonLogging.unit.test.d.ts","sourceRoot":"","sources":["../../src/logging/WinstonLogging.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const winston_1 = require("winston");
4
+ const WinstonLogging_1 = require("./WinstonLogging");
5
+ jest.mock('winston', () => (Object.assign(Object.assign({}, jest.requireActual('winston')), { createLogger: jest.fn(), Logger: jest.fn() })));
6
+ describe('Logger', () => {
7
+ const mockedCreateLogger = winston_1.createLogger;
8
+ beforeEach(() => {
9
+ jest.clearAllMocks();
10
+ });
11
+ describe.each([
12
+ { logLevel: 'info', winstonLogLevel: 'info' },
13
+ { logLevel: 'error', winstonLogLevel: 'error' },
14
+ { logLevel: 'warning', winstonLogLevel: 'warn' },
15
+ { logLevel: 'debug', winstonLogLevel: 'debug' },
16
+ ])('log level %s', ({ logLevel, winstonLogLevel }) => {
17
+ describe('When provided with a message and observability context', () => {
18
+ const mockInfo = jest.fn();
19
+ const mockWinston = {
20
+ [winstonLogLevel]: mockInfo,
21
+ };
22
+ beforeEach(() => {
23
+ mockedCreateLogger.mockImplementation(() => mockWinston);
24
+ const logger = (0, WinstonLogging_1.getLogger)();
25
+ logger[logLevel]('test log message', {
26
+ host: 'localhost',
27
+ });
28
+ });
29
+ it('should instantiate the logger with console and json formatting', () => {
30
+ expect(winston_1.createLogger).toHaveBeenCalledWith({
31
+ format: winston_1.format.json(),
32
+ exitOnError: false,
33
+ transports: [expect.any(winston_1.transports.Console)],
34
+ });
35
+ });
36
+ it('Should log the message and context', () => {
37
+ expect(mockInfo).toHaveBeenCalledWith('test log message', {
38
+ host: 'localhost',
39
+ service: 'tray-typescript-sdk',
40
+ });
41
+ });
42
+ });
43
+ });
44
+ describe('when logging a message', () => {
45
+ describe('when no context is provided', () => {
46
+ const mockInfo = jest.fn();
47
+ const mockWinston = {
48
+ info: mockInfo,
49
+ };
50
+ beforeEach(() => {
51
+ mockedCreateLogger.mockImplementation(() => mockWinston);
52
+ const logger = (0, WinstonLogging_1.getLogger)({ env: 'test' });
53
+ logger.info('test log message', {});
54
+ });
55
+ it('should log the default context of service name and env', () => {
56
+ expect(mockInfo).toHaveBeenCalledWith('test log message', {
57
+ service: 'tray-typescript-sdk',
58
+ env: 'test',
59
+ });
60
+ });
61
+ });
62
+ describe('when an initial context is provided', () => {
63
+ const mockInfo = jest.fn();
64
+ const mockWinston = {
65
+ info: mockInfo,
66
+ };
67
+ beforeEach(() => {
68
+ mockedCreateLogger.mockImplementation(() => mockWinston);
69
+ const logger = (0, WinstonLogging_1.getLogger)({ env: 'test' });
70
+ logger.info('test log message', {});
71
+ logger.info('test log message two', {});
72
+ });
73
+ it('should log the inital context with every subsequent log', () => {
74
+ expect(mockInfo).toHaveBeenNthCalledWith(1, 'test log message', {
75
+ service: 'tray-typescript-sdk',
76
+ env: 'test',
77
+ });
78
+ expect(mockInfo).toHaveBeenNthCalledWith(2, 'test log message two', {
79
+ service: 'tray-typescript-sdk',
80
+ env: 'test',
81
+ });
82
+ });
83
+ });
84
+ });
85
+ });
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@trayio/winston",
3
+ "version": "0.11.0",
4
+ "description": "Tray implementation of Winston logger",
5
+ "author": "Tray.io",
6
+ "license": "MIT",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "dependencies": {
11
+ "@trayio/commons": "0.11.0",
12
+ "chalk": "*",
13
+ "json-colorizer": "*",
14
+ "winston": "*"
15
+ },
16
+ "exports": {
17
+ "./*": "./dist/*.js"
18
+ },
19
+ "typesVersions": {
20
+ "*": {
21
+ "*": [
22
+ "*",
23
+ "dist/*"
24
+ ]
25
+ }
26
+ },
27
+ "files": [
28
+ "/dist"
29
+ ]
30
+ }