Merge pull request #10470 from Budibase/fix/cli-further-fixes

CLI - fully disable pino logger
This commit is contained in:
Michael Drury 2023-05-02 19:01:34 +01:00 committed by GitHub
commit 8624fff9d0
4 changed files with 155 additions and 169 deletions

View File

@ -154,6 +154,7 @@ const environment = {
? process.env.ENABLE_SSO_MAINTENANCE_MODE ? process.env.ENABLE_SSO_MAINTENANCE_MODE
: false, : false,
VERSION: findVersion(), VERSION: findVersion(),
DISABLE_PINO_LOGGER: process.env.DISABLE_PINO_LOGGER,
_set(key: any, value: any) { _set(key: any, value: any) {
process.env[key] = value process.env[key] = value
// @ts-ignore // @ts-ignore

View File

@ -1,5 +1,5 @@
export * as correlation from "./correlation/correlation" export * as correlation from "./correlation/correlation"
export { logger, disableLogger } from "./pino/logger" export { logger } from "./pino/logger"
export * from "./alerts" export * from "./alerts"
// turn off or on context logging i.e. tenantId, appId etc // turn off or on context logging i.e. tenantId, appId etc

View File

@ -5,184 +5,169 @@ import * as correlation from "../correlation"
import { IdentityType } from "@budibase/types" import { IdentityType } from "@budibase/types"
import { LOG_CONTEXT } from "../index" import { LOG_CONTEXT } from "../index"
// CORE LOGGERS - for disabling
const BUILT_INS = {
log: console.log,
error: console.error,
info: console.info,
warn: console.warn,
trace: console.trace,
debug: console.debug,
}
// LOGGER // LOGGER
const pinoOptions: LoggerOptions = { let pinoInstance: pino.Logger | undefined
level: env.LOG_LEVEL, if (!env.DISABLE_PINO_LOGGER) {
formatters: { const pinoOptions: LoggerOptions = {
level: label => { level: env.LOG_LEVEL,
return { level: label.toUpperCase() } formatters: {
}, level: label => {
bindings: () => { return { level: label.toUpperCase() }
return {} },
}, bindings: () => {
}, return {}
timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`, },
}
if (env.isDev()) {
pinoOptions.transport = {
target: "pino-pretty",
options: {
singleLine: true,
}, },
timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
} }
}
export const logger = pino(pinoOptions) if (env.isDev()) {
pinoOptions.transport = {
export function disableLogger() { target: "pino-pretty",
console.log = BUILT_INS.log options: {
console.error = BUILT_INS.error singleLine: true,
console.info = BUILT_INS.info },
console.warn = BUILT_INS.warn
console.trace = BUILT_INS.trace
console.debug = BUILT_INS.debug
}
// CONSOLE OVERRIDES
interface MergingObject {
objects?: any[]
tenantId?: string
appId?: string
identityId?: string
identityType?: IdentityType
correlationId?: string
err?: Error
}
function isPlainObject(obj: any) {
return typeof obj === "object" && obj !== null && !(obj instanceof Error)
}
function isError(obj: any) {
return obj instanceof Error
}
function isMessage(obj: any) {
return typeof obj === "string"
}
/**
* Backwards compatibility between console logging statements
* and pino logging requirements.
*/
function getLogParams(args: any[]): [MergingObject, string] {
let error = undefined
let objects: any[] = []
let message = ""
args.forEach(arg => {
if (isMessage(arg)) {
message = `${message} ${arg}`.trimStart()
}
if (isPlainObject(arg)) {
objects.push(arg)
}
if (isError(arg)) {
error = arg
}
})
const identity = getIdentity()
let contextObject = {}
if (LOG_CONTEXT) {
contextObject = {
tenantId: getTenantId(),
appId: getAppId(),
identityId: identity?._id,
identityType: identity?.type,
correlationId: correlation.getId(),
} }
} }
const mergingObject = { pinoInstance = pino(pinoOptions)
objects: objects.length ? objects : undefined,
err: error, // CONSOLE OVERRIDES
...contextObject,
interface MergingObject {
objects?: any[]
tenantId?: string
appId?: string
identityId?: string
identityType?: IdentityType
correlationId?: string
err?: Error
} }
return [mergingObject, message] function isPlainObject(obj: any) {
} return typeof obj === "object" && obj !== null && !(obj instanceof Error)
console.log = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
logger.info(obj, msg)
}
console.info = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
logger.info(obj, msg)
}
console.warn = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
logger.warn(obj, msg)
}
console.error = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
logger.error(obj, msg)
}
/**
* custom trace impl - this resembles the node trace behaviour rather
* than traditional trace logging
* @param arg
*/
console.trace = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
if (!obj.err) {
// to get stack trace
obj.err = new Error()
} }
logger.trace(obj, msg)
}
console.debug = (...arg: any) => { function isError(obj: any) {
const [obj, msg] = getLogParams(arg) return obj instanceof Error
logger.debug(obj, msg) }
}
function isMessage(obj: any) {
// CONTEXT return typeof obj === "string"
}
const getTenantId = () => {
let tenantId /**
try { * Backwards compatibility between console logging statements
tenantId = context.getTenantId() * and pino logging requirements.
} catch (e: any) { */
// do nothing function getLogParams(args: any[]): [MergingObject, string] {
let error = undefined
let objects: any[] = []
let message = ""
args.forEach(arg => {
if (isMessage(arg)) {
message = `${message} ${arg}`.trimStart()
}
if (isPlainObject(arg)) {
objects.push(arg)
}
if (isError(arg)) {
error = arg
}
})
const identity = getIdentity()
let contextObject = {}
if (LOG_CONTEXT) {
contextObject = {
tenantId: getTenantId(),
appId: getAppId(),
identityId: identity?._id,
identityType: identity?.type,
correlationId: correlation.getId(),
}
}
const mergingObject = {
objects: objects.length ? objects : undefined,
err: error,
...contextObject,
}
return [mergingObject, message]
}
console.log = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
pinoInstance?.info(obj, msg)
}
console.info = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
pinoInstance?.info(obj, msg)
}
console.warn = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
pinoInstance?.warn(obj, msg)
}
console.error = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
pinoInstance?.error(obj, msg)
}
/**
* custom trace impl - this resembles the node trace behaviour rather
* than traditional trace logging
* @param arg
*/
console.trace = (...arg: any[]) => {
const [obj, msg] = getLogParams(arg)
if (!obj.err) {
// to get stack trace
obj.err = new Error()
}
pinoInstance?.trace(obj, msg)
}
console.debug = (...arg: any) => {
const [obj, msg] = getLogParams(arg)
pinoInstance?.debug(obj, msg)
}
// CONTEXT
const getTenantId = () => {
let tenantId
try {
tenantId = context.getTenantId()
} catch (e: any) {
// do nothing
}
return tenantId
}
const getAppId = () => {
let appId
try {
appId = context.getAppId()
} catch (e) {
// do nothing
}
return appId
}
const getIdentity = () => {
let identity
try {
identity = context.getIdentity()
} catch (e) {
// do nothing
}
return identity
} }
return tenantId
} }
const getAppId = () => { export const logger = pinoInstance
let appId
try {
appId = context.getAppId()
} catch (e) {
// do nothing
}
return appId
}
const getIdentity = () => {
let identity
try {
identity = context.getIdentity()
} catch (e) {
// do nothing
}
return identity
}

View File

@ -1,8 +1,8 @@
#!/usr/bin/env node #!/usr/bin/env node
process.env.DISABLE_PINO_LOGGER = "1"
import "./prebuilds" import "./prebuilds"
import "./environment" import "./environment"
import { env, logging } from "@budibase/backend-core" import { env } from "@budibase/backend-core"
logging.disableLogger()
import { getCommands } from "./options" import { getCommands } from "./options"
import { Command } from "commander" import { Command } from "commander"
import { getHelpDescription } from "./utils" import { getHelpDescription } from "./utils"