@budibase/backend-core 2.8.11 → 2.8.12-alpha.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@budibase/backend-core",
3
- "version": "2.8.11",
3
+ "version": "2.8.12-alpha.0",
4
4
  "description": "Budibase backend core libraries used in server and worker",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "dependencies": {
23
23
  "@budibase/nano": "10.1.2",
24
24
  "@budibase/pouchdb-replication-stream": "1.2.10",
25
- "@budibase/types": "2.8.11",
25
+ "@budibase/types": "2.8.12-alpha.0",
26
26
  "@shopify/jest-koa-mocks": "5.0.1",
27
27
  "@techpass/passport-openidconnect": "0.3.2",
28
28
  "aws-cloudfront-sign": "2.2.0",
@@ -51,6 +51,7 @@
51
51
  "pouchdb": "7.3.0",
52
52
  "pouchdb-find": "7.2.2",
53
53
  "redlock": "4.2.0",
54
+ "rotating-file-stream": "3.1.0",
54
55
  "sanitize-s3-objectkey": "0.0.1",
55
56
  "semver": "7.3.7",
56
57
  "tar-fs": "2.1.1",
@@ -101,5 +102,5 @@
101
102
  }
102
103
  }
103
104
  },
104
- "gitHead": "8d0194b6f7bb76a41c244450f0c30f7548c6a0d5"
105
+ "gitHead": "2aeebd290d78e13ab253daa2a28a5101e443fbc8"
105
106
  }
@@ -47,7 +47,10 @@ function httpLogging() {
47
47
  return process.env.HTTP_LOGGING
48
48
  }
49
49
 
50
- function findVersion() {
50
+ function getPackageJsonFields(): {
51
+ VERSION: string
52
+ SERVICE_NAME: string
53
+ } {
51
54
  function findFileInAncestors(
52
55
  fileName: string,
53
56
  currentDir: string
@@ -69,10 +72,14 @@ function findVersion() {
69
72
  try {
70
73
  const packageJsonFile = findFileInAncestors("package.json", process.cwd())
71
74
  const content = readFileSync(packageJsonFile!, "utf-8")
72
- return JSON.parse(content).version
75
+ const parsedContent = JSON.parse(content)
76
+ return {
77
+ VERSION: parsedContent.version,
78
+ SERVICE_NAME: parsedContent.name,
79
+ }
73
80
  } catch {
74
81
  // throwing an error here is confusing/causes backend-core to be hard to import
75
- return undefined
82
+ return { VERSION: "", SERVICE_NAME: "" }
76
83
  }
77
84
  }
78
85
 
@@ -154,13 +161,14 @@ const environment = {
154
161
  ENABLE_SSO_MAINTENANCE_MODE: selfHosted
155
162
  ? process.env.ENABLE_SSO_MAINTENANCE_MODE
156
163
  : false,
157
- VERSION: findVersion(),
164
+ ...getPackageJsonFields(),
158
165
  DISABLE_PINO_LOGGER: process.env.DISABLE_PINO_LOGGER,
159
166
  _set(key: any, value: any) {
160
167
  process.env[key] = value
161
168
  // @ts-ignore
162
169
  environment[key] = value
163
170
  },
171
+ ROLLING_LOG_MAX_SIZE: process.env.ROLLING_LOG_MAX_SIZE || "10M",
164
172
  }
165
173
 
166
174
  // clean up any environment variable edge cases
@@ -1,6 +1,7 @@
1
1
  export * as correlation from "./correlation/correlation"
2
2
  export { logger } from "./pino/logger"
3
3
  export * from "./alerts"
4
+ export * as system from "./system"
4
5
 
5
6
  // turn off or on context logging i.e. tenantId, appId etc
6
7
  export let LOG_CONTEXT = true
@@ -1,10 +1,15 @@
1
- import env from "../../environment"
2
1
  import pino, { LoggerOptions } from "pino"
2
+ import pinoPretty from "pino-pretty"
3
+
4
+ import { IdentityType } from "@budibase/types"
5
+
6
+ import env from "../../environment"
3
7
  import * as context from "../../context"
4
8
  import * as correlation from "../correlation"
5
- import { IdentityType } from "@budibase/types"
6
9
  import { LOG_CONTEXT } from "../index"
7
10
 
11
+ import { localFileDestination } from "../system"
12
+
8
13
  // LOGGER
9
14
 
10
15
  let pinoInstance: pino.Logger | undefined
@@ -16,22 +21,27 @@ if (!env.DISABLE_PINO_LOGGER) {
16
21
  return { level: label.toUpperCase() }
17
22
  },
18
23
  bindings: () => {
19
- return {}
24
+ return {
25
+ service: env.SERVICE_NAME,
26
+ }
20
27
  },
21
28
  },
22
29
  timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
23
30
  }
24
31
 
32
+ const destinations: pino.DestinationStream[] = []
33
+
25
34
  if (env.isDev()) {
26
- pinoOptions.transport = {
27
- target: "pino-pretty",
28
- options: {
29
- singleLine: true,
30
- },
31
- }
35
+ destinations.push(pinoPretty({ singleLine: true }))
36
+ }
37
+
38
+ if (env.SELF_HOSTED) {
39
+ destinations.push(localFileDestination())
32
40
  }
33
41
 
34
- pinoInstance = pino(pinoOptions)
42
+ pinoInstance = destinations.length
43
+ ? pino(pinoOptions, pino.multistream(destinations))
44
+ : pino(pinoOptions)
35
45
 
36
46
  // CONSOLE OVERRIDES
37
47
 
@@ -0,0 +1,81 @@
1
+ import fs from "fs"
2
+ import path from "path"
3
+ import * as rfs from "rotating-file-stream"
4
+
5
+ import env from "../environment"
6
+ import { budibaseTempDir } from "../objectStore"
7
+
8
+ const logsFileName = `budibase.log`
9
+ const budibaseLogsHistoryFileName = "budibase-logs-history.txt"
10
+
11
+ const logsPath = path.join(budibaseTempDir(), "systemlogs")
12
+
13
+ function getFullPath(fileName: string) {
14
+ return path.join(logsPath, fileName)
15
+ }
16
+
17
+ export function getSingleFileMaxSizeInfo(totalMaxSize: string) {
18
+ const regex = /(\d+)([A-Za-z])/
19
+ const match = totalMaxSize?.match(regex)
20
+ if (!match) {
21
+ console.warn(`totalMaxSize does not have a valid value`, {
22
+ totalMaxSize,
23
+ })
24
+ return undefined
25
+ }
26
+
27
+ const size = +match[1]
28
+ const unit = match[2]
29
+ if (size === 1) {
30
+ switch (unit) {
31
+ case "B":
32
+ return { size: `${size}B`, totalHistoryFiles: 1 }
33
+ case "K":
34
+ return { size: `${(size * 1000) / 2}B`, totalHistoryFiles: 1 }
35
+ case "M":
36
+ return { size: `${(size * 1000) / 2}K`, totalHistoryFiles: 1 }
37
+ case "G":
38
+ return { size: `${(size * 1000) / 2}M`, totalHistoryFiles: 1 }
39
+ default:
40
+ return undefined
41
+ }
42
+ }
43
+
44
+ if (size % 2 === 0) {
45
+ return { size: `${size / 2}${unit}`, totalHistoryFiles: 1 }
46
+ }
47
+
48
+ return { size: `1${unit}`, totalHistoryFiles: size - 1 }
49
+ }
50
+
51
+ export function localFileDestination() {
52
+ const fileInfo = getSingleFileMaxSizeInfo(env.ROLLING_LOG_MAX_SIZE)
53
+ const outFile = rfs.createStream(logsFileName, {
54
+ // As we have a rolling size, we want to half the max size
55
+ size: fileInfo?.size,
56
+ path: logsPath,
57
+ maxFiles: fileInfo?.totalHistoryFiles || 1,
58
+ immutable: true,
59
+ history: budibaseLogsHistoryFileName,
60
+ initialRotation: false,
61
+ })
62
+
63
+ return outFile
64
+ }
65
+
66
+ export function getLogReadStream() {
67
+ const streams = []
68
+ const historyFile = getFullPath(budibaseLogsHistoryFileName)
69
+ if (fs.existsSync(historyFile)) {
70
+ const fileContent = fs.readFileSync(historyFile, "utf-8")
71
+ const historyFiles = fileContent.split("\n")
72
+ for (const historyFile of historyFiles.filter(x => x)) {
73
+ streams.push(fs.readFileSync(historyFile))
74
+ }
75
+ }
76
+
77
+ streams.push(fs.readFileSync(getFullPath(logsFileName)))
78
+
79
+ const combinedContent = Buffer.concat(streams)
80
+ return combinedContent
81
+ }
@@ -0,0 +1,61 @@
1
+ import { getSingleFileMaxSizeInfo } from "../system"
2
+
3
+ describe("system", () => {
4
+ describe("getSingleFileMaxSizeInfo", () => {
5
+ it.each([
6
+ ["100B", "50B"],
7
+ ["200K", "100K"],
8
+ ["20M", "10M"],
9
+ ["4G", "2G"],
10
+ ])(
11
+ "Halving even number (%s) returns halved size and 1 history file (%s)",
12
+ (totalValue, expectedMaxSize) => {
13
+ const result = getSingleFileMaxSizeInfo(totalValue)
14
+ expect(result).toEqual({
15
+ size: expectedMaxSize,
16
+ totalHistoryFiles: 1,
17
+ })
18
+ }
19
+ )
20
+
21
+ it.each([
22
+ ["5B", "1B", 4],
23
+ ["17K", "1K", 16],
24
+ ["21M", "1M", 20],
25
+ ["3G", "1G", 2],
26
+ ])(
27
+ "Halving an odd number (%s) returns as many files as size (-1) (%s)",
28
+ (totalValue, expectedMaxSize, totalHistoryFiles) => {
29
+ const result = getSingleFileMaxSizeInfo(totalValue)
30
+ expect(result).toEqual({
31
+ size: expectedMaxSize,
32
+ totalHistoryFiles,
33
+ })
34
+ }
35
+ )
36
+
37
+ it.each([
38
+ ["1B", "1B"],
39
+ ["1K", "500B"],
40
+ ["1M", "500K"],
41
+ ["1G", "500M"],
42
+ ])(
43
+ "Halving '%s' returns halved unit (%s)",
44
+ (totalValue, expectedMaxSize) => {
45
+ const result = getSingleFileMaxSizeInfo(totalValue)
46
+ expect(result).toEqual({
47
+ size: expectedMaxSize,
48
+ totalHistoryFiles: 1,
49
+ })
50
+ }
51
+ )
52
+
53
+ it.each([[undefined], [""], ["50"], ["wrongvalue"]])(
54
+ "Halving wrongly formatted value ('%s') returns undefined",
55
+ totalValue => {
56
+ const result = getSingleFileMaxSizeInfo(totalValue!)
57
+ expect(result).toBeUndefined()
58
+ }
59
+ )
60
+ })
61
+ })