simple-log-methods 0.5.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.
Files changed (32) hide show
  1. package/dist/constants.d.ts +35 -0
  2. package/dist/constants.js +42 -0
  3. package/dist/constants.js.map +1 -0
  4. package/dist/formatLogContentsForEnvironment.d.ts +17 -0
  5. package/dist/formatLogContentsForEnvironment.js +39 -0
  6. package/dist/formatLogContentsForEnvironment.js.map +1 -0
  7. package/dist/formatLogContentsForEnvironment.test.d.ts +1 -0
  8. package/dist/formatLogContentsForEnvironment.test.js +50 -0
  9. package/dist/formatLogContentsForEnvironment.test.js.map +1 -0
  10. package/dist/generateLogMethod.d.ts +6 -0
  11. package/dist/generateLogMethod.js +37 -0
  12. package/dist/generateLogMethod.js.map +1 -0
  13. package/dist/generateLogMethod.test.d.ts +1 -0
  14. package/dist/generateLogMethod.test.js +50 -0
  15. package/dist/generateLogMethod.test.js.map +1 -0
  16. package/dist/generateLogMethods.d.ts +36 -0
  17. package/dist/generateLogMethods.js +22 -0
  18. package/dist/generateLogMethods.js.map +1 -0
  19. package/dist/generateLogMethods.test.d.ts +1 -0
  20. package/dist/generateLogMethods.test.js +13 -0
  21. package/dist/generateLogMethods.test.js.map +1 -0
  22. package/dist/getRecommendedMinimalLogLevelForEnvironment.d.ts +2 -0
  23. package/dist/getRecommendedMinimalLogLevelForEnvironment.js +38 -0
  24. package/dist/getRecommendedMinimalLogLevelForEnvironment.js.map +1 -0
  25. package/dist/identifyEnvironment.d.ts +6 -0
  26. package/dist/identifyEnvironment.js +22 -0
  27. package/dist/identifyEnvironment.js.map +1 -0
  28. package/dist/index.d.ts +3 -0
  29. package/dist/index.js +8 -0
  30. package/dist/index.js.map +1 -0
  31. package/package.json +90 -0
  32. package/readme.md +45 -0
@@ -0,0 +1,35 @@
1
+ /**
2
+ * define the supported log levels
3
+ */
4
+ export declare enum LogLevel {
5
+ ERROR = "error",
6
+ WARN = "warn",
7
+ INFO = "info",
8
+ DEBUG = "debug"
9
+ }
10
+ export declare const isOfLogLevel: import("type-fns").AssessWithAssure<string | number | symbol, LogLevel>;
11
+ /**
12
+ * define specifically supported environments
13
+ */
14
+ export declare enum SupportedEnvironment {
15
+ /**
16
+ * the local environment balances information -vs- oversharing
17
+ * - metadata is stringified to not cause too much visual "noise" in the console
18
+ * - log level should default to being conservatively balanced, to reduce noise in the console
19
+ * - especially when considering the ease of retrying requests with more permissive log-level specified
20
+ */
21
+ LOCAL = "LOCAL",
22
+ /**
23
+ * the aws environment has a few extra considerations for logging
24
+ * - metadata should not be json-stringified, and should be simply nested instead for cloudwatch insights parsing
25
+ * - log level should default to be more permissive, as cloudwatch costs are low enough to be worth the increased visibility in most cases
26
+ */
27
+ AWS_LAMBDA = "AWS_LAMBDA",
28
+ /**
29
+ * the web browser environment allows us to focus on accessibility of information
30
+ * - the metadata should not be json-stringified. instead, it should be simply nested for devtools console accessibility
31
+ * - log level should default to being conservatively balanced, to reduce noise in the console
32
+ * - especially when considering the ease of retrying requests with more permissive log-level specified
33
+ */
34
+ WEB_BROWSER = "WEB_BROWSER"
35
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SupportedEnvironment = exports.isOfLogLevel = exports.LogLevel = void 0;
4
+ const type_fns_1 = require("type-fns");
5
+ /**
6
+ * define the supported log levels
7
+ */
8
+ var LogLevel;
9
+ (function (LogLevel) {
10
+ LogLevel["ERROR"] = "error";
11
+ LogLevel["WARN"] = "warn";
12
+ LogLevel["INFO"] = "info";
13
+ LogLevel["DEBUG"] = "debug";
14
+ })(LogLevel || (exports.LogLevel = LogLevel = {}));
15
+ exports.isOfLogLevel = (0, type_fns_1.createIsOfEnum)(LogLevel);
16
+ /**
17
+ * define specifically supported environments
18
+ */
19
+ var SupportedEnvironment;
20
+ (function (SupportedEnvironment) {
21
+ /**
22
+ * the local environment balances information -vs- oversharing
23
+ * - metadata is stringified to not cause too much visual "noise" in the console
24
+ * - log level should default to being conservatively balanced, to reduce noise in the console
25
+ * - especially when considering the ease of retrying requests with more permissive log-level specified
26
+ */
27
+ SupportedEnvironment["LOCAL"] = "LOCAL";
28
+ /**
29
+ * the aws environment has a few extra considerations for logging
30
+ * - metadata should not be json-stringified, and should be simply nested instead for cloudwatch insights parsing
31
+ * - log level should default to be more permissive, as cloudwatch costs are low enough to be worth the increased visibility in most cases
32
+ */
33
+ SupportedEnvironment["AWS_LAMBDA"] = "AWS_LAMBDA";
34
+ /**
35
+ * the web browser environment allows us to focus on accessibility of information
36
+ * - the metadata should not be json-stringified. instead, it should be simply nested for devtools console accessibility
37
+ * - log level should default to being conservatively balanced, to reduce noise in the console
38
+ * - especially when considering the ease of retrying requests with more permissive log-level specified
39
+ */
40
+ SupportedEnvironment["WEB_BROWSER"] = "WEB_BROWSER";
41
+ })(SupportedEnvironment || (exports.SupportedEnvironment = SupportedEnvironment = {}));
42
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA,uCAA0C;AAE1C;;GAEG;AACH,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,2BAAe,CAAA;IACf,yBAAa,CAAA;IACb,yBAAa,CAAA;IACb,2BAAe,CAAA;AACjB,CAAC,EALW,QAAQ,wBAAR,QAAQ,QAKnB;AACY,QAAA,YAAY,GAAG,IAAA,yBAAc,EAAC,QAAQ,CAAC,CAAC;AAErD;;GAEG;AACH,IAAY,oBAuBX;AAvBD,WAAY,oBAAoB;IAC9B;;;;;OAKG;IACH,uCAAe,CAAA;IAEf;;;;OAIG;IACH,iDAAyB,CAAA;IAEzB;;;;;OAKG;IACH,mDAA2B,CAAA;AAC7B,CAAC,EAvBW,oBAAoB,oCAApB,oBAAoB,QAuB/B"}
@@ -0,0 +1,17 @@
1
+ import { LogLevel } from './constants';
2
+ export declare const formatLogContentsForEnvironment: ({ level, timestamp, message, metadata, }: {
3
+ level: LogLevel;
4
+ timestamp: string;
5
+ message: string;
6
+ metadata?: Record<string, any>;
7
+ }) => string | {
8
+ level: LogLevel;
9
+ timestamp: string;
10
+ message: string;
11
+ metadata: string;
12
+ } | {
13
+ level: LogLevel;
14
+ timestamp: string;
15
+ message: string;
16
+ metadata: Record<string, any> | undefined;
17
+ };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatLogContentsForEnvironment = void 0;
4
+ const constants_1 = require("./constants");
5
+ const identifyEnvironment_1 = require("./identifyEnvironment");
6
+ const formatLogContentsForEnvironment = ({ level, timestamp, message, metadata, }) => {
7
+ const env = (0, identifyEnvironment_1.identifyEnvironment)();
8
+ // if its a "local" environment, then dont stringify the contents - but stringify the metadata to cut down on visual noise
9
+ if (env === constants_1.SupportedEnvironment.LOCAL) {
10
+ return {
11
+ level,
12
+ timestamp,
13
+ message,
14
+ metadata: JSON.stringify(metadata), // json stringify it to cut down on the visual noise in the console
15
+ };
16
+ }
17
+ // if its an aws-lambda environment, then stringify the contents - without stringifying the metadata - to make sure log is fully readable and parseable by cloudwatch
18
+ if (env === constants_1.SupportedEnvironment.AWS_LAMBDA) {
19
+ return JSON.stringify({
20
+ level,
21
+ timestamp,
22
+ message,
23
+ metadata,
24
+ });
25
+ }
26
+ // if its a web-browser environment, then dont stringify the contents - nor the metadata - to make sure log is fully accessible through console devtools
27
+ if (env === constants_1.SupportedEnvironment.WEB_BROWSER) {
28
+ return {
29
+ level,
30
+ timestamp,
31
+ message,
32
+ metadata,
33
+ };
34
+ }
35
+ // if it was not one of the above, we have not supported this environment yet
36
+ throw new Error('unsupported environment detected. this should never occur - and is a bug within simple-log-methods'); // fail fast
37
+ };
38
+ exports.formatLogContentsForEnvironment = formatLogContentsForEnvironment;
39
+ //# sourceMappingURL=formatLogContentsForEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatLogContentsForEnvironment.js","sourceRoot":"","sources":["../src/formatLogContentsForEnvironment.ts"],"names":[],"mappings":";;;AAAA,2CAA6D;AAC7D,+DAA4D;AAErD,MAAM,+BAA+B,GAAG,CAAC,EAC9C,KAAK,EACL,SAAS,EACT,OAAO,EACP,QAAQ,GAMT,EAAE,EAAE;IACH,MAAM,GAAG,GAAG,IAAA,yCAAmB,GAAE,CAAC;IAElC,0HAA0H;IAC1H,IAAI,GAAG,KAAK,gCAAoB,CAAC,KAAK,EAAE,CAAC;QACvC,OAAO;YACL,KAAK;YACL,SAAS;YACT,OAAO;YACP,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,mEAAmE;SACxG,CAAC;IACJ,CAAC;IAED,qKAAqK;IACrK,IAAI,GAAG,KAAK,gCAAoB,CAAC,UAAU,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAC,SAAS,CAAC;YACpB,KAAK;YACL,SAAS;YACT,OAAO;YACP,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAED,wJAAwJ;IACxJ,IAAI,GAAG,KAAK,gCAAoB,CAAC,WAAW,EAAE,CAAC;QAC7C,OAAO;YACL,KAAK;YACL,SAAS;YACT,OAAO;YACP,QAAQ;SACT,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,MAAM,IAAI,KAAK,CACb,oGAAoG,CACrG,CAAC,CAAC,YAAY;AACjB,CAAC,CAAC;AA/CW,QAAA,+BAA+B,mCA+C1C"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const _1 = require(".");
4
+ const constants_1 = require("./constants");
5
+ const formatLogContentsForEnvironment_1 = require("./formatLogContentsForEnvironment");
6
+ const identifyEnvironment_1 = require("./identifyEnvironment");
7
+ jest.mock('./identifyEnvironment');
8
+ const identifyEnvironmentMock = identifyEnvironment_1.identifyEnvironment;
9
+ describe('formatMetadataForEnvironment', () => {
10
+ it('should not stringify the contents - but should stringify the metadata - if in the local environment', () => {
11
+ identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.LOCAL);
12
+ const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
13
+ const logContents = {
14
+ level: _1.LogLevel.INFO,
15
+ timestamp: new Date().toISOString(),
16
+ message: 'hello world!',
17
+ metadata,
18
+ };
19
+ const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
20
+ expect(formatted).toEqual({
21
+ ...logContents,
22
+ metadata: JSON.stringify(metadata),
23
+ });
24
+ });
25
+ it('should stringify the contents in the aws lambda environment', () => {
26
+ identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.AWS_LAMBDA);
27
+ const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
28
+ const logContents = {
29
+ level: _1.LogLevel.INFO,
30
+ timestamp: new Date().toISOString(),
31
+ message: 'hello world!',
32
+ metadata,
33
+ };
34
+ const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
35
+ expect(formatted).toEqual(JSON.stringify(logContents));
36
+ });
37
+ it('should not stringify the contents nor the metadata if in web browser environment', () => {
38
+ identifyEnvironmentMock.mockReturnValue(constants_1.SupportedEnvironment.WEB_BROWSER);
39
+ const metadata = { name: 'bob', likes: ['oranges', 'apples'] };
40
+ const logContents = {
41
+ level: _1.LogLevel.INFO,
42
+ timestamp: new Date().toISOString(),
43
+ message: 'hello world!',
44
+ metadata,
45
+ };
46
+ const formatted = (0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)(logContents);
47
+ expect(formatted).toEqual(logContents);
48
+ });
49
+ });
50
+ //# sourceMappingURL=formatLogContentsForEnvironment.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatLogContentsForEnvironment.test.js","sourceRoot":"","sources":["../src/formatLogContentsForEnvironment.test.ts"],"names":[],"mappings":";;AAAA,wBAA6B;AAC7B,2CAAmD;AACnD,uFAAoF;AACpF,+DAA4D;AAE5D,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;AACnC,MAAM,uBAAuB,GAAG,yCAAgC,CAAC;AAEjE,QAAQ,CAAC,8BAA8B,EAAE,GAAG,EAAE;IAC5C,EAAE,CAAC,qGAAqG,EAAE,GAAG,EAAE;QAC7G,uBAAuB,CAAC,eAAe,CAAC,gCAAoB,CAAC,KAAK,CAAC,CAAC;QACpE,MAAM,QAAQ,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC;QAC/D,MAAM,WAAW,GAAG;YAClB,KAAK,EAAE,WAAQ,CAAC,IAAI;YACpB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,OAAO,EAAE,cAAc;YACvB,QAAQ;SACT,CAAC;QACF,MAAM,SAAS,GAAG,IAAA,iEAA+B,EAAC,WAAW,CAAC,CAAC;QAC/D,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC;YACxB,GAAG,WAAW;YACd,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;SACnC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,6DAA6D,EAAE,GAAG,EAAE;QACrE,uBAAuB,CAAC,eAAe,CAAC,gCAAoB,CAAC,UAAU,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC;QAC/D,MAAM,WAAW,GAAG;YAClB,KAAK,EAAE,WAAQ,CAAC,IAAI;YACpB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,OAAO,EAAE,cAAc;YACvB,QAAQ;SACT,CAAC;QACF,MAAM,SAAS,GAAG,IAAA,iEAA+B,EAAC,WAAW,CAAC,CAAC;QAC/D,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,kFAAkF,EAAE,GAAG,EAAE;QAC1F,uBAAuB,CAAC,eAAe,CAAC,gCAAoB,CAAC,WAAW,CAAC,CAAC;QAC1E,MAAM,QAAQ,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC;QAC/D,MAAM,WAAW,GAAG;YAClB,KAAK,EAAE,WAAQ,CAAC,IAAI;YACpB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,OAAO,EAAE,cAAc;YACvB,QAAQ;SACT,CAAC;QACF,MAAM,SAAS,GAAG,IAAA,iEAA+B,EAAC,WAAW,CAAC,CAAC;QAC/D,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { LogLevel } from './constants';
2
+ export type LogMethod = (message: string, metadata: any) => void;
3
+ export declare const generateLogMethod: ({ level, minimalLogLevel, }: {
4
+ level: LogLevel;
5
+ minimalLogLevel: LogLevel;
6
+ }) => (message: string, metadata?: object) => void;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateLogMethod = void 0;
4
+ const constants_1 = require("./constants");
5
+ const formatLogContentsForEnvironment_1 = require("./formatLogContentsForEnvironment");
6
+ /*
7
+ define priority order of log levels and make it easy to ask questions about
8
+ */
9
+ const logLevelPriorityOrder = [
10
+ constants_1.LogLevel.ERROR,
11
+ constants_1.LogLevel.WARN,
12
+ constants_1.LogLevel.INFO,
13
+ constants_1.LogLevel.DEBUG,
14
+ ];
15
+ const aIsEqualOrMoreImportantThanB = ({ a, b }) => logLevelPriorityOrder.indexOf(a) - logLevelPriorityOrder.indexOf(b) <= 0;
16
+ const generateLogMethod = ({ level, minimalLogLevel, }) => {
17
+ return (message, metadata) => {
18
+ if (aIsEqualOrMoreImportantThanB({ a: level, b: minimalLogLevel })) {
19
+ // determine the console level (i.e., use warn if we can to make the logs stand out more)
20
+ const consoleMethod = aIsEqualOrMoreImportantThanB({
21
+ a: level,
22
+ b: constants_1.LogLevel.WARN,
23
+ })
24
+ ? console.warn
25
+ : console.log; // tslint:disable-line no-console
26
+ // output the message to console, which will get picked up by cloudwatch when deployed lambda is invoked
27
+ consoleMethod((0, formatLogContentsForEnvironment_1.formatLogContentsForEnvironment)({
28
+ level,
29
+ timestamp: new Date().toISOString(),
30
+ message,
31
+ metadata,
32
+ }));
33
+ }
34
+ };
35
+ };
36
+ exports.generateLogMethod = generateLogMethod;
37
+ //# sourceMappingURL=generateLogMethod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateLogMethod.js","sourceRoot":"","sources":["../src/generateLogMethod.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AACvC,uFAAoF;AAEpF;;EAEE;AACF,MAAM,qBAAqB,GAAG;IAC5B,oBAAQ,CAAC,KAAK;IACd,oBAAQ,CAAC,IAAI;IACb,oBAAQ,CAAC,IAAI;IACb,oBAAQ,CAAC,KAAK;CACf,CAAC;AACF,MAAM,4BAA4B,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAgC,EAAE,EAAE,CAC9E,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAUpE,MAAM,iBAAiB,GAAG,CAAC,EAChC,KAAK,EACL,eAAe,GAIhB,EAAE,EAAE;IACH,OAAO,CAAC,OAAe,EAAE,QAAiB,EAAE,EAAE;QAC5C,IAAI,4BAA4B,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,CAAC;YACnE,yFAAyF;YACzF,MAAM,aAAa,GAAG,4BAA4B,CAAC;gBACjD,CAAC,EAAE,KAAK;gBACR,CAAC,EAAE,oBAAQ,CAAC,IAAI;aACjB,CAAC;gBACA,CAAC,CAAC,OAAO,CAAC,IAAI;gBACd,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,iCAAiC;YAElD,wGAAwG;YACxG,aAAa,CACX,IAAA,iEAA+B,EAAC;gBAC9B,KAAK;gBACL,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;gBACnC,OAAO;gBACP,QAAQ;aACT,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AA5BW,QAAA,iBAAiB,qBA4B5B"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const generateLogMethod_1 = require("./generateLogMethod");
5
+ describe('generateLogMethod', () => {
6
+ beforeEach(() => jest.clearAllMocks());
7
+ it('should generate a method that outputs to console.log if below warn level', () => {
8
+ const spy = jest.spyOn(console, 'log');
9
+ const logError = (0, generateLogMethod_1.generateLogMethod)({
10
+ level: constants_1.LogLevel.INFO,
11
+ minimalLogLevel: constants_1.LogLevel.DEBUG,
12
+ });
13
+ logError('testMessage');
14
+ expect(spy).toHaveBeenCalledTimes(1);
15
+ });
16
+ it('should generate a method that outputs to console.warn if at or above warn level', () => {
17
+ const spy = jest.spyOn(console, 'warn');
18
+ const logError = (0, generateLogMethod_1.generateLogMethod)({
19
+ level: constants_1.LogLevel.ERROR,
20
+ minimalLogLevel: constants_1.LogLevel.DEBUG,
21
+ });
22
+ logError('testMessage');
23
+ expect(spy).toHaveBeenCalledTimes(1);
24
+ });
25
+ it('should generate a method that outputs timestamp, level, message, and metadata', () => {
26
+ const spy = jest.spyOn(console, 'warn');
27
+ const logError = (0, generateLogMethod_1.generateLogMethod)({
28
+ level: constants_1.LogLevel.ERROR,
29
+ minimalLogLevel: constants_1.LogLevel.DEBUG,
30
+ });
31
+ logError('testMessage', { nested: { object: true } });
32
+ expect(spy).toHaveBeenCalledTimes(1);
33
+ expect(spy).toHaveBeenCalledWith(expect.objectContaining({
34
+ level: constants_1.LogLevel.ERROR,
35
+ message: 'testMessage',
36
+ metadata: JSON.stringify({ nested: { object: true } }),
37
+ }));
38
+ expect(spy.mock.calls[0][0]).toHaveProperty('timestamp');
39
+ });
40
+ it('should not output anything if level is below minimum', () => {
41
+ const spy = jest.spyOn(console, 'log');
42
+ const logDebug = (0, generateLogMethod_1.generateLogMethod)({
43
+ level: constants_1.LogLevel.DEBUG,
44
+ minimalLogLevel: constants_1.LogLevel.WARN,
45
+ });
46
+ logDebug('testMessage');
47
+ expect(spy).not.toHaveBeenCalled();
48
+ });
49
+ });
50
+ //# sourceMappingURL=generateLogMethod.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateLogMethod.test.js","sourceRoot":"","sources":["../src/generateLogMethod.test.ts"],"names":[],"mappings":";;AAAA,2CAAuC;AACvC,2DAAwD;AAExD,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;IACvC,EAAE,CAAC,0EAA0E,EAAE,GAAG,EAAE;QAClF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACvC,MAAM,QAAQ,GAAG,IAAA,qCAAiB,EAAC;YACjC,KAAK,EAAE,oBAAQ,CAAC,IAAI;YACpB,eAAe,EAAE,oBAAQ,CAAC,KAAK;SAChC,CAAC,CAAC;QACH,QAAQ,CAAC,aAAa,CAAC,CAAC;QACxB,MAAM,CAAC,GAAG,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,iFAAiF,EAAE,GAAG,EAAE;QACzF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACxC,MAAM,QAAQ,GAAG,IAAA,qCAAiB,EAAC;YACjC,KAAK,EAAE,oBAAQ,CAAC,KAAK;YACrB,eAAe,EAAE,oBAAQ,CAAC,KAAK;SAChC,CAAC,CAAC;QACH,QAAQ,CAAC,aAAa,CAAC,CAAC;QACxB,MAAM,CAAC,GAAG,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,+EAA+E,EAAE,GAAG,EAAE;QACvF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACxC,MAAM,QAAQ,GAAG,IAAA,qCAAiB,EAAC;YACjC,KAAK,EAAE,oBAAQ,CAAC,KAAK;YACrB,eAAe,EAAE,oBAAQ,CAAC,KAAK;SAChC,CAAC,CAAC;QACH,QAAQ,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;QACtD,MAAM,CAAC,GAAG,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;QACrC,MAAM,CAAC,GAAG,CAAC,CAAC,oBAAoB,CAC9B,MAAM,CAAC,gBAAgB,CAAC;YACtB,KAAK,EAAE,oBAAQ,CAAC,KAAK;YACrB,OAAO,EAAE,aAAa;YACtB,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;SACvD,CAAC,CACH,CAAC;QACF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAC9D,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACvC,MAAM,QAAQ,GAAG,IAAA,qCAAiB,EAAC;YACjC,KAAK,EAAE,oBAAQ,CAAC,KAAK;YACrB,eAAe,EAAE,oBAAQ,CAAC,IAAI;SAC/B,CAAC,CAAC;QACH,QAAQ,CAAC,aAAa,CAAC,CAAC;QACxB,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;IACrC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { LogLevel } from './constants';
2
+ import { LogMethod } from './generateLogMethod';
3
+ export interface LogMethods {
4
+ /**
5
+ * `error` level logs are used to indicate a critical and urgent failure that requires immediate resolution
6
+ * - these logs are often associated with someone on-call being notified immediately, regardless of the time or day
7
+ * - when choosing to log something with a log level of "error", you are saying that someone should be woken up in the middle of the night if this occurs
8
+ */
9
+ error: LogMethod;
10
+ /**
11
+ * `warn` level logs are used to indicate that something is going wrong, but can wait to be resolved until a convenient time
12
+ * - these logs are often associated with someone following up on them during business hours
13
+ * - when choosing to log something with a log level of "warn", you are saying that someone should look at this as soon as reasonably possible
14
+ */
15
+ warn: LogMethod;
16
+ /**
17
+ * `info` level logs are used to indicate an interesting event that should be kept track of
18
+ * - these logs are often associated with health metrics, dashboard statistics, or custom log queries for investigations or reporting
19
+ * - when choosing to log something with a log level of "info", you are saying that someone will be interested in this information indefinitely
20
+ */
21
+ info: LogMethod;
22
+ /**
23
+ * `debug` level logs are used to output information that can aid users in tracking down bugs or confirming that things are working as expected
24
+ * - these logs are often associated with common actions that happen within code, that may only be relevant when debugging your applications
25
+ * - when choosing to log something with a log level of "debug", you are saying that someone will only be interested in this information when debugging
26
+ */
27
+ debug: LogMethod;
28
+ }
29
+ /**
30
+ * define how to generate the log methods
31
+ * - allows you to specify the minimal log level to use for your application
32
+ * - defaults to recommended levels for the environment
33
+ */
34
+ export declare const generateLogMethods: ({ minimalLogLevel, }?: {
35
+ minimalLogLevel?: LogLevel;
36
+ }) => LogMethods;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateLogMethods = void 0;
4
+ const constants_1 = require("./constants");
5
+ const generateLogMethod_1 = require("./generateLogMethod");
6
+ const getRecommendedMinimalLogLevelForEnvironment_1 = require("./getRecommendedMinimalLogLevelForEnvironment");
7
+ /**
8
+ * define how to generate the log methods
9
+ * - allows you to specify the minimal log level to use for your application
10
+ * - defaults to recommended levels for the environment
11
+ */
12
+ const generateLogMethods = ({ minimalLogLevel = (0, getRecommendedMinimalLogLevelForEnvironment_1.getRecommendedMinimalLogLevelForEnvironment)(), } = {}) => {
13
+ // generate the methods
14
+ return {
15
+ error: (0, generateLogMethod_1.generateLogMethod)({ level: constants_1.LogLevel.ERROR, minimalLogLevel }),
16
+ warn: (0, generateLogMethod_1.generateLogMethod)({ level: constants_1.LogLevel.WARN, minimalLogLevel }),
17
+ info: (0, generateLogMethod_1.generateLogMethod)({ level: constants_1.LogLevel.INFO, minimalLogLevel }),
18
+ debug: (0, generateLogMethod_1.generateLogMethod)({ level: constants_1.LogLevel.DEBUG, minimalLogLevel }),
19
+ };
20
+ };
21
+ exports.generateLogMethods = generateLogMethods;
22
+ //# sourceMappingURL=generateLogMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateLogMethods.js","sourceRoot":"","sources":["../src/generateLogMethods.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AACvC,2DAAmE;AACnE,+GAA4G;AAgC5G;;;;GAIG;AACI,MAAM,kBAAkB,GAAG,CAAC,EACjC,eAAe,GAAG,IAAA,yFAA2C,GAAE,MAC7B,EAAE,EAAc,EAAE;IACpD,uBAAuB;IACvB,OAAO;QACL,KAAK,EAAE,IAAA,qCAAiB,EAAC,EAAE,KAAK,EAAE,oBAAQ,CAAC,KAAK,EAAE,eAAe,EAAE,CAAC;QACpE,IAAI,EAAE,IAAA,qCAAiB,EAAC,EAAE,KAAK,EAAE,oBAAQ,CAAC,IAAI,EAAE,eAAe,EAAE,CAAC;QAClE,IAAI,EAAE,IAAA,qCAAiB,EAAC,EAAE,KAAK,EAAE,oBAAQ,CAAC,IAAI,EAAE,eAAe,EAAE,CAAC;QAClE,KAAK,EAAE,IAAA,qCAAiB,EAAC,EAAE,KAAK,EAAE,oBAAQ,CAAC,KAAK,EAAE,eAAe,EAAE,CAAC;KACrE,CAAC;AACJ,CAAC,CAAC;AAVW,QAAA,kBAAkB,sBAU7B"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const generateLogMethods_1 = require("./generateLogMethods");
4
+ describe('generateLogMethods', () => {
5
+ it('should create the log methods', () => {
6
+ const log = (0, generateLogMethods_1.generateLogMethods)();
7
+ expect(log).toHaveProperty('error');
8
+ expect(log).toHaveProperty('warn');
9
+ expect(log).toHaveProperty('info');
10
+ expect(log).toHaveProperty('debug');
11
+ });
12
+ });
13
+ //# sourceMappingURL=generateLogMethods.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateLogMethods.test.js","sourceRoot":"","sources":["../src/generateLogMethods.test.ts"],"names":[],"mappings":";;AAAA,6DAA0D;AAE1D,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;IAClC,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACvC,MAAM,GAAG,GAAG,IAAA,uCAAkB,GAAE,CAAC;QACjC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACpC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { LogLevel } from './constants';
2
+ export declare const getRecommendedMinimalLogLevelForEnvironment: () => LogLevel;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRecommendedMinimalLogLevelForEnvironment = void 0;
4
+ const constants_1 = require("./constants");
5
+ const identifyEnvironment_1 = require("./identifyEnvironment");
6
+ const getLogLevelFromEnvVar = () => {
7
+ // if LOG_LEVEL was specified, use that
8
+ const logLevelEnvVar = process.env.LOG_LEVEL || null; // cast falsy values to null
9
+ if (logLevelEnvVar) {
10
+ // if the log level is valid, use it
11
+ if ((0, constants_1.isOfLogLevel)(logLevelEnvVar))
12
+ return logLevelEnvVar;
13
+ // otherwise, warn and continue to attempt other options
14
+ console.warn(`environmental variable LOG_LEVEL was set to an invalid value: '${logLevelEnvVar}'. using the default instead`);
15
+ }
16
+ // if LOG_DEBUG was specified as true, use that (it's a common intuitive alias)
17
+ const logDebugEnvVar = process.env.LOG_DEBUG || null;
18
+ if (logDebugEnvVar === 'true')
19
+ return constants_1.LogLevel.DEBUG;
20
+ // todo: consider supporting other LOG_${level} options
21
+ // otherwise, null
22
+ return null;
23
+ };
24
+ const getRecommendedMinimalLogLevelForEnvironment = () => {
25
+ // if the LOG_LEVEL env var is defined, then use what is specified by that
26
+ const logLevelFromEnvVar = getLogLevelFromEnvVar();
27
+ if (logLevelFromEnvVar)
28
+ return logLevelFromEnvVar;
29
+ // identify the env we're in
30
+ const env = (0, identifyEnvironment_1.identifyEnvironment)();
31
+ // if we're in aws lambda env, then default to DEBUG - since cloudwatch costs are cheap and extra visibility is worth it in most cases
32
+ if (env === constants_1.SupportedEnvironment.AWS_LAMBDA)
33
+ return constants_1.LogLevel.DEBUG;
34
+ // otherwise, default to INFO - to balance signal -vs- noise
35
+ return constants_1.LogLevel.INFO;
36
+ };
37
+ exports.getRecommendedMinimalLogLevelForEnvironment = getRecommendedMinimalLogLevelForEnvironment;
38
+ //# sourceMappingURL=getRecommendedMinimalLogLevelForEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRecommendedMinimalLogLevelForEnvironment.js","sourceRoot":"","sources":["../src/getRecommendedMinimalLogLevelForEnvironment.ts"],"names":[],"mappings":";;;AAAA,2CAA2E;AAC3E,+DAA4D;AAE5D,MAAM,qBAAqB,GAAG,GAAoB,EAAE;IAClD,uCAAuC;IACvC,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC,4BAA4B;IAClF,IAAI,cAAc,EAAE,CAAC;QACnB,oCAAoC;QACpC,IAAI,IAAA,wBAAY,EAAC,cAAc,CAAC;YAAE,OAAO,cAAc,CAAC;QAExD,wDAAwD;QACxD,OAAO,CAAC,IAAI,CACV,kEAAkE,cAAc,8BAA8B,CAC/G,CAAC;IACJ,CAAC;IAED,+EAA+E;IAC/E,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,IAAI,CAAC;IACrD,IAAI,cAAc,KAAK,MAAM;QAAE,OAAO,oBAAQ,CAAC,KAAK,CAAC;IAErD,uDAAuD;IAEvD,kBAAkB;IAClB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEK,MAAM,2CAA2C,GAAG,GAAa,EAAE;IACxE,0EAA0E;IAC1E,MAAM,kBAAkB,GAAG,qBAAqB,EAAE,CAAC;IACnD,IAAI,kBAAkB;QAAE,OAAO,kBAAkB,CAAC;IAElD,4BAA4B;IAC5B,MAAM,GAAG,GAAG,IAAA,yCAAmB,GAAE,CAAC;IAElC,sIAAsI;IACtI,IAAI,GAAG,KAAK,gCAAoB,CAAC,UAAU;QAAE,OAAO,oBAAQ,CAAC,KAAK,CAAC;IAEnE,4DAA4D;IAC5D,OAAO,oBAAQ,CAAC,IAAI,CAAC;AACvB,CAAC,CAAC;AAbW,QAAA,2CAA2C,+CAatD"}
@@ -0,0 +1,6 @@
1
+ import { SupportedEnvironment } from './constants';
2
+ /**
3
+ * identifies the supported environment that this is being run in, as best as possible
4
+ * - defaults to LOCAL environment, as it has the most common settings and is not distinguishable otherwise
5
+ */
6
+ export declare const identifyEnvironment: () => SupportedEnvironment;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.identifyEnvironment = void 0;
4
+ const constants_1 = require("./constants");
5
+ /**
6
+ * identifies the supported environment that this is being run in, as best as possible
7
+ * - defaults to LOCAL environment, as it has the most common settings and is not distinguishable otherwise
8
+ */
9
+ const identifyEnvironment = () => {
10
+ // check if its an aws-lambda runtime environment
11
+ const isAwsLambdaEnvironment = !!process.env.AWS_LAMBDA_FUNCTION_NAME; // if this env var is defined, then its aws lambda env
12
+ if (isAwsLambdaEnvironment)
13
+ return constants_1.SupportedEnvironment.AWS_LAMBDA;
14
+ // check if its a browser environment
15
+ const isWebBrowserEnvironment = typeof window !== 'undefined' && typeof window.document !== 'undefined'; // if both are defined, then its a browser
16
+ if (isWebBrowserEnvironment)
17
+ return constants_1.SupportedEnvironment.WEB_BROWSER;
18
+ // default to local env
19
+ return constants_1.SupportedEnvironment.LOCAL;
20
+ };
21
+ exports.identifyEnvironment = identifyEnvironment;
22
+ //# sourceMappingURL=identifyEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifyEnvironment.js","sourceRoot":"","sources":["../src/identifyEnvironment.ts"],"names":[],"mappings":";;;AAAA,2CAAmD;AAEnD;;;GAGG;AACI,MAAM,mBAAmB,GAAG,GAAyB,EAAE;IAC5D,iDAAiD;IACjD,MAAM,sBAAsB,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC,sDAAsD;IAC7H,IAAI,sBAAsB;QAAE,OAAO,gCAAoB,CAAC,UAAU,CAAC;IAEnE,qCAAqC;IACrC,MAAM,uBAAuB,GAC3B,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,QAAQ,KAAK,WAAW,CAAC,CAAC,0CAA0C;IACrH,IAAI,uBAAuB;QAAE,OAAO,gCAAoB,CAAC,WAAW,CAAC;IAErE,uBAAuB;IACvB,OAAO,gCAAoB,CAAC,KAAK,CAAC;AACpC,CAAC,CAAC;AAZW,QAAA,mBAAmB,uBAY9B"}
@@ -0,0 +1,3 @@
1
+ export { LogLevel } from './constants';
2
+ export { LogMethod } from './generateLogMethod';
3
+ export { generateLogMethods, LogMethods } from './generateLogMethods';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateLogMethods = exports.LogLevel = void 0;
4
+ var constants_1 = require("./constants");
5
+ Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return constants_1.LogLevel; } });
6
+ var generateLogMethods_1 = require("./generateLogMethods");
7
+ Object.defineProperty(exports, "generateLogMethods", { enumerable: true, get: function () { return generateLogMethods_1.generateLogMethods; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAAuC;AAA9B,qGAAA,QAAQ,OAAA;AAEjB,2DAAsE;AAA7D,wHAAA,kBAAkB,OAAA"}
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "simple-log-methods",
3
+ "author": "ehmpathy",
4
+ "description": "a simple and opinionated logging library. plays well with aws lambda + cloudwatch.",
5
+ "version": "0.5.0",
6
+ "repository": "ehmpathy/simple-log-methods",
7
+ "homepage": "https://github.com/ehmpathy/simple-log-methods",
8
+ "keywords": [
9
+ "log",
10
+ "logs",
11
+ "level",
12
+ "simple",
13
+ "filter",
14
+ "aws",
15
+ "lambda",
16
+ "cloudwatch",
17
+ "requestId"
18
+ ],
19
+ "bugs": "https://github.com/ehmpathy/simple-log-methods/issues",
20
+ "license": "MIT",
21
+ "main": "dist/index.js",
22
+ "engines": {
23
+ "node": ">=8.0.0"
24
+ },
25
+ "files": [
26
+ "/dist"
27
+ ],
28
+ "scripts": {
29
+ "build:ts": "tsc -p ./tsconfig.build.json",
30
+ "commit:with-cli": "npx cz",
31
+ "fix:format:prettier": "prettier --write '**/*.ts' --config ./prettier.config.js",
32
+ "fix:format": "npm run fix:format:prettier",
33
+ "fix:lint": "eslint -c ./.eslintrc.js src/**/*.ts --fix",
34
+ "build:clean": "rm dist/ -rf",
35
+ "build:compile": "tsc -p ./tsconfig.build.json",
36
+ "build": "npm run build:clean && npm run build:compile",
37
+ "test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose",
38
+ "test:types": "tsc -p ./tsconfig.build.json --noEmit",
39
+ "test:format:prettier": "prettier --parser typescript --check 'src/**/*.ts' --config ./prettier.config.js",
40
+ "test:format": "npm run test:format:prettier",
41
+ "test:lint:deps": "npx depcheck -c ./depcheckrc.yml",
42
+ "test:lint:eslint": "eslint -c ./.eslintrc.js src/**/*.ts",
43
+ "test:lint": "npm run test:lint:eslint && npm run test:lint:deps",
44
+ "test:unit": "jest -c ./jest.unit.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main')",
45
+ "test:integration": "jest -c ./jest.integration.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main')",
46
+ "test:acceptance:locally": "npm run build && LOCALLY=true jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
47
+ "test": "npm run test:commits && npm run test:types && npm run test:format && npm run test:lint && npm run test:unit && npm run test:integration && npm run test:acceptance:locally",
48
+ "test:acceptance": "npm run build && jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
49
+ "prepush": "npm run test && npm run build",
50
+ "prepublish": "npm run build",
51
+ "preversion": "npm run prepush",
52
+ "postversion": "git push origin HEAD --tags --no-verify",
53
+ "postinstall": "[ -d .git ] && npm run prepare:husky || exit 0",
54
+ "prepare:husky": "npx husky install && chmod ug+x .husky/*"
55
+ },
56
+ "dependencies": {
57
+ "type-fns": "^1.16.0"
58
+ },
59
+ "devDependencies": {
60
+ "@commitlint/cli": "19.3.0",
61
+ "@commitlint/config-conventional": "13.1.0",
62
+ "@trivago/prettier-plugin-sort-imports": "4.3.0",
63
+ "@tsconfig/node-lts-strictest": "18.12.1",
64
+ "@types/jest": "29.2.4",
65
+ "@typescript-eslint/eslint-plugin": "7.8.0",
66
+ "@typescript-eslint/parser": "7.8.0",
67
+ "core-js": "3.26.1",
68
+ "cz-conventional-changelog": "3.3.0",
69
+ "declapract": "^0.12.0",
70
+ "declapract-typescript-ehmpathy": "^0.39.5",
71
+ "depcheck": "1.4.3",
72
+ "eslint": "8.56.0",
73
+ "eslint-config-airbnb-typescript": "18.0.0",
74
+ "eslint-config-prettier": "8.5.0",
75
+ "eslint-plugin-import": "2.26.0",
76
+ "eslint-plugin-prettier": "4.2.1",
77
+ "husky": "8.0.3",
78
+ "jest": "29.3.1",
79
+ "prettier": "2.8.1",
80
+ "test-fns": "1.4.2",
81
+ "ts-jest": "29.1.3",
82
+ "ts-node": "10.9.2",
83
+ "typescript": "5.4.5"
84
+ },
85
+ "config": {
86
+ "commitizen": {
87
+ "path": "./node_modules/cz-conventional-changelog"
88
+ }
89
+ }
90
+ }
package/readme.md ADDED
@@ -0,0 +1,45 @@
1
+ # simple-log-methods
2
+
3
+ a simple and opinionated logging library. plays well with aws lambda + cloudwatch.
4
+
5
+ # features
6
+
7
+ - distinguish different priorities of logs with standard levels
8
+ - i.e., `log.debug`, `log.info`, `log.warn`, and `log.error`
9
+ - filter which levels of log to emit
10
+ - i.e., choose based on environment whether to emit all logs or skip some of the logs
11
+ - utilizes `console.warn` and `console.log` under the hood
12
+ - which ensures that the aws cloudwatch requestId is present on each log message automatically
13
+ - formats log metadata based on environment
14
+ - i.e., stringify in local environments to reduce visual noise
15
+ - i.e., don't stringify in aws-lambda environment for optimal cloudwatch parsing
16
+
17
+
18
+ # installation
19
+
20
+ ```
21
+ npm install --save simple-log-methods
22
+ ```
23
+
24
+ # usage
25
+
26
+ ### init
27
+ ```ts
28
+ // e.g., in `src/utils/log.ts
29
+ import { generateLogMethods } from 'simple-log-methods';
30
+
31
+ export const log = generateLogMethods();
32
+ ```
33
+
34
+ ### use in code
35
+ ```ts
36
+ import { log } from '../utils/log';
37
+
38
+ log.error(`the sky is falling and we're loosing money!`, metadata); // use `.error` when you want someone to respond immediately, even if its 4am
39
+
40
+ log.warn(`this shouldn't be happening and should be looked at asap`, metadata); // use `.warn` when you want someone to look at it asap, but not wake up in the middle of the night
41
+
42
+ log.info(`we either want to or should keep track of this`, metadata); // use `.info` for anything we may be interested in knowing about
43
+
44
+ log.debug(`this will help debug if things go wrong`, metadata); // use this for any information that could help debug when things go wrong (e.g., request/response data)
45
+ ```