1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 17:59:37 +02:00

Prevent plugins to log exceptions

This commit is contained in:
Chocobozzz 2025-05-13 11:21:06 +02:00
parent 106acd8509
commit a29bf7619d
No known key found for this signature in database
GPG key ID: 583A612D890159BE
3 changed files with 56 additions and 57 deletions

View file

@ -10,9 +10,6 @@ import { CONFIG } from '../initializers/config.js'
import { LOG_FILENAME } from '../initializers/constants.js'
import { FileTransportOptions } from 'winston/lib/winston/transports/index.js'
const consoleSupportsColor = isTestOrDevInstance() ||
(isatty(1) && process.env.TERM && process.env.TERM !== 'dumb')
const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
const consoleLoggerFormat = format.printf(info => {
@ -35,22 +32,40 @@ const consoleLoggerFormat = format.printf(info => {
return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
})
const jsonLoggerFormat = format.printf(info => {
export const jsonLoggerFormat = format.printf(info => {
return JSON.stringify(info, removeCyclicValues())
})
const timestampFormatter = format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss.SSS'
})
const labelFormatter = (suffix?: string) => {
export const labelFormatter = (suffix?: string) => {
return format.label({
label: suffix ? `${label} ${suffix}` : label
})
}
export function buildLogger (options: {
labelSuffix?: string
handleExceptions?: boolean // default false
}) {
const { labelSuffix, handleExceptions = false } = options
const formatters = [
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss.SSS'
})
]
if (doesConsoleSupportColor()) formatters.push(format.colorize())
formatters.push(consoleLoggerFormat)
const consoleTransport = new transports.Console({
handleExceptions,
format: format.combine(...formatters)
})
const fileLoggerOptions: FileTransportOptions = {
filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
handleExceptions: true,
handleExceptions,
format: format.combine(
format.timestamp(),
jsonLoggerFormat
@ -62,17 +77,6 @@ if (CONFIG.LOG.ROTATION.ENABLED) {
fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
}
function buildLogger (labelSuffix?: string) {
const formatters = [ timestampFormatter ]
if (consoleSupportsColor) formatters.push(format.colorize())
formatters.push(consoleLoggerFormat)
const consoleTransport = new transports.Console({
handleExceptions: true,
format: format.combine(...formatters)
})
return createLogger({
level: process.env.LOGGER_LEVEL ?? CONFIG.LOG.LEVEL,
defaultMeta: {
@ -98,9 +102,22 @@ function buildLogger (labelSuffix?: string) {
})
}
const logger = buildLogger()
export const logger = buildLogger({ handleExceptions: true })
// ---------------------------------------------------------------------------
// Bunyan logger adapter for Winston
// ---------------------------------------------------------------------------
export const bunyanLogger = {
level: () => {},
trace: bunyanLogFactory('debug'),
debug: bunyanLogFactory('debug'),
verbose: bunyanLogFactory('debug'),
info: bunyanLogFactory('info'),
warn: bunyanLogFactory('warn'),
error: bunyanLogFactory('error'),
fatal: bunyanLogFactory('error')
}
function bunyanLogFactory (level: string) {
return function (...params: any[]) {
@ -121,22 +138,13 @@ function bunyanLogFactory (level: string) {
}
}
const bunyanLogger = {
level: () => {},
trace: bunyanLogFactory('debug'),
debug: bunyanLogFactory('debug'),
verbose: bunyanLogFactory('debug'),
info: bunyanLogFactory('info'),
warn: bunyanLogFactory('warn'),
error: bunyanLogFactory('error'),
fatal: bunyanLogFactory('error')
}
// ---------------------------------------------------------------------------
// Logger tags helpers
// ---------------------------------------------------------------------------
type LoggerTags = { tags: (string | number)[] }
type LoggerTagsFn = (...tags: (string | number)[]) => LoggerTags
function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn {
export type LoggerTags = { tags: (string | number)[] }
export type LoggerTagsFn = (...tags: (string | number)[]) => LoggerTags
export function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn {
return (...tags: (string | number)[]) => {
return { tags: defaultTags.concat(tags) }
}
@ -144,7 +152,7 @@ function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn {
// ---------------------------------------------------------------------------
async function mtimeSortFilesDesc (files: string[], basePath: string) {
export async function mtimeSortFilesDesc (files: string[], basePath: string) {
const promises = []
const out: { file: string, mtime: number }[] = []
@ -165,21 +173,7 @@ async function mtimeSortFilesDesc (files: string[], basePath: string) {
}
// ---------------------------------------------------------------------------
export {
buildLogger,
bunyanLogger,
consoleLoggerFormat,
jsonLoggerFormat,
labelFormatter,
logger,
loggerTagsFactory,
mtimeSortFilesDesc,
timestampFormatter,
type LoggerTags,
type LoggerTagsFn
}
// Private
// ---------------------------------------------------------------------------
function removeCyclicValues () {
@ -222,3 +216,8 @@ function getAdditionalInfo (info: any) {
return omit(info, toOmit)
}
function doesConsoleSupportColor () {
return isTestOrDevInstance() ||
(isatty(1) && process.env.TERM && process.env.TERM !== 'dumb')
}

View file

@ -61,7 +61,7 @@ export {
// ---------------------------------------------------------------------------
function buildPluginLogger (npmName: string) {
return buildLogger(npmName)
return buildLogger({ labelSuffix: npmName })
}
function buildDatabaseHelpers () {

View file

@ -7,7 +7,7 @@ import { extname } from 'path'
import { authenticate } from '@server/middlewares/auth.js'
import { resumableInitValidator } from '@server/middlewares/validators/resumable-upload.js'
const logger = buildLogger('uploadx')
const logger = buildLogger({ labelSuffix: 'uploadx' })
export const uploadx = new Uploadx({
directory: getResumableUploadPath(),