Respect max values
This commit is contained in:
parent
6e2a8e0ab9
commit
dc73e57ba8
|
@ -168,7 +168,7 @@ const environment = {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
environment[key] = value
|
environment[key] = value
|
||||||
},
|
},
|
||||||
ROLLING_LOG_MAX_SIZE: process.env.ROLLING_LOG_MAX_SIZE || "100M",
|
ROLLING_LOG_MAX_SIZE: process.env.ROLLING_LOG_MAX_SIZE || "20M",
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean up any environment variable edge cases
|
// clean up any environment variable edge cases
|
||||||
|
|
|
@ -14,11 +14,47 @@ function getFullPath(fileName: string) {
|
||||||
return path.join(logsPath, fileName)
|
return path.join(logsPath, fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSingleFileMaxSizeInfo(totalMaxSize: string) {
|
||||||
|
const regex = /(\d+)([A-Za-z])/
|
||||||
|
const match = totalMaxSize?.match(regex)
|
||||||
|
if (!match) {
|
||||||
|
console.warn(`totalMaxSize does not have a valid value`, {
|
||||||
|
totalMaxSize,
|
||||||
|
})
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const size = +match[1]
|
||||||
|
const unit = match[2]
|
||||||
|
if (size === 1) {
|
||||||
|
switch (unit) {
|
||||||
|
case "B":
|
||||||
|
return { size: `${size}B`, totalHistoryFiles: 1 }
|
||||||
|
case "K":
|
||||||
|
return { size: `${(size * 1000) / 2}B`, totalHistoryFiles: 1 }
|
||||||
|
case "M":
|
||||||
|
return { size: `${(size * 1000) / 2}K`, totalHistoryFiles: 1 }
|
||||||
|
case "G":
|
||||||
|
return { size: `${(size * 1000) / 2}M`, totalHistoryFiles: 1 }
|
||||||
|
default:
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size % 2 === 0) {
|
||||||
|
return { size: `${size / 2}${unit}`, totalHistoryFiles: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
|
return { size: `1${unit}`, totalHistoryFiles: size }
|
||||||
|
}
|
||||||
|
|
||||||
export function localFileDestination() {
|
export function localFileDestination() {
|
||||||
|
const fileInfo = getSingleFileMaxSizeInfo(env.ROLLING_LOG_MAX_SIZE)
|
||||||
const outFile = rfs.createStream(logsFileName, {
|
const outFile = rfs.createStream(logsFileName, {
|
||||||
size: env.ROLLING_LOG_MAX_SIZE,
|
// As we have a rolling size, we want to half the max size
|
||||||
|
size: fileInfo?.size,
|
||||||
path: logsPath,
|
path: logsPath,
|
||||||
maxFiles: 1,
|
maxFiles: fileInfo?.totalHistoryFiles || 1,
|
||||||
immutable: true,
|
immutable: true,
|
||||||
history: budibaseLogsHistoryFileName,
|
history: budibaseLogsHistoryFileName,
|
||||||
initialRotation: false,
|
initialRotation: false,
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
import { getSingleFileMaxSizeInfo } from "../system"
|
||||||
|
|
||||||
|
describe("system", () => {
|
||||||
|
describe("getSingleFileMaxSizeInfo", () => {
|
||||||
|
it.each([
|
||||||
|
["100B", "50B"],
|
||||||
|
["200K", "100K"],
|
||||||
|
["20M", "10M"],
|
||||||
|
["4G", "2G"],
|
||||||
|
])(
|
||||||
|
"Halving even number (%s) returns halved size and 1 history file (%s)",
|
||||||
|
(totalValue, expectedMaxSize) => {
|
||||||
|
const result = getSingleFileMaxSizeInfo(totalValue)
|
||||||
|
expect(result).toEqual({
|
||||||
|
size: expectedMaxSize,
|
||||||
|
totalHistoryFiles: 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["5B", "1B", 5],
|
||||||
|
["17K", "1K", 17],
|
||||||
|
["21M", "1M", 21],
|
||||||
|
["3G", "1G", 3],
|
||||||
|
])(
|
||||||
|
"Halving an odd number (%s) returns as many files as size (%s)",
|
||||||
|
(totalValue, expectedMaxSize, totalHistoryFiles) => {
|
||||||
|
const result = getSingleFileMaxSizeInfo(totalValue)
|
||||||
|
expect(result).toEqual({
|
||||||
|
size: expectedMaxSize,
|
||||||
|
totalHistoryFiles,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["1B", "1B"],
|
||||||
|
["1K", "500B"],
|
||||||
|
["1M", "500K"],
|
||||||
|
["1G", "500M"],
|
||||||
|
])(
|
||||||
|
"Halving '%s' returns halved unit (%s)",
|
||||||
|
(totalValue, expectedMaxSize) => {
|
||||||
|
const result = getSingleFileMaxSizeInfo(totalValue)
|
||||||
|
expect(result).toEqual({
|
||||||
|
size: expectedMaxSize,
|
||||||
|
totalHistoryFiles: 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each([[undefined], [""], ["50"], ["wrongvalue"]])(
|
||||||
|
"Halving wrongly formatted value ('%s') returns undefined",
|
||||||
|
totalValue => {
|
||||||
|
const result = getSingleFileMaxSizeInfo(totalValue!)
|
||||||
|
expect(result).toBeUndefined()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue