@naturalcycles/backend-lib 4.6.0 → 4.7.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.
@@ -10,4 +10,8 @@ export declare const gaeLogger: CommonLogger;
10
10
  * (otherwise req.log should be used).
11
11
  */
12
12
  export declare const devLogger: CommonLogger;
13
+ /**
14
+ * Same as devLogger, but without colors (e.g to not confuse Sentry).
15
+ */
16
+ export declare const ciLogger: CommonLogger;
13
17
  export declare function appEngineLogMiddleware(): BackendRequestHandler;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.appEngineLogMiddleware = exports.devLogger = exports.gaeLogger = void 0;
3
+ exports.appEngineLogMiddleware = exports.ciLogger = exports.devLogger = exports.gaeLogger = void 0;
4
4
  const util_1 = require("util");
5
5
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
@@ -26,6 +26,14 @@ exports.devLogger = {
26
26
  warn: (...args) => logToDev(null, args),
27
27
  error: (...args) => logToDev(null, args),
28
28
  };
29
+ /**
30
+ * Same as devLogger, but without colors (e.g to not confuse Sentry).
31
+ */
32
+ exports.ciLogger = {
33
+ log: (...args) => logToCI(args),
34
+ warn: (...args) => logToCI(args),
35
+ error: (...args) => logToCI(args),
36
+ };
29
37
  // Documented here: https://cloud.google.com/logging/docs/structured-logging
30
38
  function logToAppEngine(meta, args) {
31
39
  console.log(JSON.stringify({
@@ -40,6 +48,13 @@ function logToDev(requestId, args) {
40
48
  ...args.map(a => (0, nodejs_lib_1.inspectAny)(a, { includeErrorStack: true, colors: true })),
41
49
  ].join(' '));
42
50
  }
51
+ /**
52
+ * Same as logToDev, but without request and without colors.
53
+ * This is to not confuse e.g Sentry when it picks up messages with colors
54
+ */
55
+ function logToCI(args) {
56
+ console.log(args.map(a => (0, nodejs_lib_1.inspectAny)(a, { includeErrorStack: true, colors: false })).join(' '));
57
+ }
43
58
  function appEngineLogMiddleware() {
44
59
  if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
45
60
  // Local machine, return "simple" logToDev middleware with request numbering
@@ -4,8 +4,9 @@ exports.requestLogger = exports.getRequestLogger = exports.getRequest = exports.
4
4
  const async_hooks_1 = require("async_hooks");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const appEngineLogMiddleware_1 = require("./appEngineLogMiddleware");
7
- const { GAE_INSTANCE } = process.env;
7
+ const { GAE_INSTANCE, CI } = process.env;
8
8
  const isGAE = !!GAE_INSTANCE;
9
+ const isCI = !!CI;
9
10
  // Singleton, for simplicity
10
11
  // Create it lazily (on demand)
11
12
  const storage = (0, js_lib_1._lazyValue)(() => new async_hooks_1.AsyncLocalStorage());
@@ -28,7 +29,7 @@ exports.getRequest = getRequest;
28
29
  * @experimental
29
30
  */
30
31
  function getRequestLogger() {
31
- return storage().getStore()?.req || (isGAE ? appEngineLogMiddleware_1.gaeLogger : appEngineLogMiddleware_1.devLogger);
32
+ return storage().getStore()?.req || (isGAE ? appEngineLogMiddleware_1.gaeLogger : isCI ? appEngineLogMiddleware_1.ciLogger : appEngineLogMiddleware_1.devLogger);
32
33
  }
33
34
  exports.getRequestLogger = getRequestLogger;
34
35
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
@@ -30,6 +30,15 @@ export const devLogger: CommonLogger = {
30
30
  error: (...args) => logToDev(null, args),
31
31
  }
32
32
 
33
+ /**
34
+ * Same as devLogger, but without colors (e.g to not confuse Sentry).
35
+ */
36
+ export const ciLogger: CommonLogger = {
37
+ log: (...args) => logToCI(args),
38
+ warn: (...args) => logToCI(args),
39
+ error: (...args) => logToCI(args),
40
+ }
41
+
33
42
  // Documented here: https://cloud.google.com/logging/docs/structured-logging
34
43
  function logToAppEngine(meta: AnyObject, args: any[]): void {
35
44
  console.log(
@@ -50,6 +59,14 @@ function logToDev(requestId: string | null, args: any[]): void {
50
59
  )
51
60
  }
52
61
 
62
+ /**
63
+ * Same as logToDev, but without request and without colors.
64
+ * This is to not confuse e.g Sentry when it picks up messages with colors
65
+ */
66
+ function logToCI(args: any[]): void {
67
+ console.log(args.map(a => inspectAny(a, { includeErrorStack: true, colors: false })).join(' '))
68
+ }
69
+
53
70
  export function appEngineLogMiddleware(): BackendRequestHandler {
54
71
  if (!isGAE || !GOOGLE_CLOUD_PROJECT) {
55
72
  // Local machine, return "simple" logToDev middleware with request numbering
@@ -1,10 +1,11 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks'
2
2
  import { _lazyValue, CommonLogger } from '@naturalcycles/js-lib'
3
3
  import { BackendRequest, BackendRequestHandler } from './server.model'
4
- import { gaeLogger, devLogger } from './appEngineLogMiddleware'
4
+ import { gaeLogger, devLogger, ciLogger } from './appEngineLogMiddleware'
5
5
 
6
- const { GAE_INSTANCE } = process.env
6
+ const { GAE_INSTANCE, CI } = process.env
7
7
  const isGAE = !!GAE_INSTANCE
8
+ const isCI = !!CI
8
9
 
9
10
  export interface RequestLocalStorage {
10
11
  req: BackendRequest
@@ -34,7 +35,7 @@ export function getRequest(): BackendRequest | undefined {
34
35
  * @experimental
35
36
  */
36
37
  export function getRequestLogger(): CommonLogger {
37
- return storage().getStore()?.req || (isGAE ? gaeLogger : devLogger)
38
+ return storage().getStore()?.req || (isGAE ? gaeLogger : isCI ? ciLogger : devLogger)
38
39
  }
39
40
 
40
41
  /**