1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-03 09:49:20 +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 { LOG_FILENAME } from '../initializers/constants.js'
import { FileTransportOptions } from 'winston/lib/winston/transports/index.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 label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
const consoleLoggerFormat = format.printf(info => { const consoleLoggerFormat = format.printf(info => {
@ -35,44 +32,51 @@ const consoleLoggerFormat = format.printf(info => {
return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` 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()) return JSON.stringify(info, removeCyclicValues())
}) })
const timestampFormatter = format.timestamp({ export const labelFormatter = (suffix?: string) => {
format: 'YYYY-MM-DD HH:mm:ss.SSS'
})
const labelFormatter = (suffix?: string) => {
return format.label({ return format.label({
label: suffix ? `${label} ${suffix}` : label label: suffix ? `${label} ${suffix}` : label
}) })
} }
const fileLoggerOptions: FileTransportOptions = { export function buildLogger (options: {
filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), labelSuffix?: string
handleExceptions: true, handleExceptions?: boolean // default false
format: format.combine( }) {
format.timestamp(), const { labelSuffix, handleExceptions = false } = options
jsonLoggerFormat
)
}
if (CONFIG.LOG.ROTATION.ENABLED) { const formatters = [
fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE format.timestamp({
fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES format: 'YYYY-MM-DD HH:mm:ss.SSS'
} })
]
function buildLogger (labelSuffix?: string) { if (doesConsoleSupportColor()) formatters.push(format.colorize())
const formatters = [ timestampFormatter ]
if (consoleSupportsColor) formatters.push(format.colorize())
formatters.push(consoleLoggerFormat) formatters.push(consoleLoggerFormat)
const consoleTransport = new transports.Console({ const consoleTransport = new transports.Console({
handleExceptions: true, handleExceptions,
format: format.combine(...formatters) format: format.combine(...formatters)
}) })
const fileLoggerOptions: FileTransportOptions = {
filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
handleExceptions,
format: format.combine(
format.timestamp(),
jsonLoggerFormat
)
}
if (CONFIG.LOG.ROTATION.ENABLED) {
fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
}
return createLogger({ return createLogger({
level: process.env.LOGGER_LEVEL ?? CONFIG.LOG.LEVEL, level: process.env.LOGGER_LEVEL ?? CONFIG.LOG.LEVEL,
defaultMeta: { 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) { function bunyanLogFactory (level: string) {
return function (...params: any[]) { return function (...params: any[]) {
@ -121,22 +138,13 @@ function bunyanLogFactory (level: string) {
} }
} }
const bunyanLogger = { // ---------------------------------------------------------------------------
level: () => {}, // Logger tags helpers
trace: bunyanLogFactory('debug'),
debug: bunyanLogFactory('debug'),
verbose: bunyanLogFactory('debug'),
info: bunyanLogFactory('info'),
warn: bunyanLogFactory('warn'),
error: bunyanLogFactory('error'),
fatal: bunyanLogFactory('error')
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
type LoggerTags = { tags: (string | number)[] } export type LoggerTags = { tags: (string | number)[] }
type LoggerTagsFn = (...tags: (string | number)[]) => LoggerTags export type LoggerTagsFn = (...tags: (string | number)[]) => LoggerTags
function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn { export function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn {
return (...tags: (string | number)[]) => { return (...tags: (string | number)[]) => {
return { tags: defaultTags.concat(tags) } 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 promises = []
const out: { file: string, mtime: number }[] = [] const out: { file: string, mtime: number }[] = []
@ -165,21 +173,7 @@ async function mtimeSortFilesDesc (files: string[], basePath: string) {
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Private
export {
buildLogger,
bunyanLogger,
consoleLoggerFormat,
jsonLoggerFormat,
labelFormatter,
logger,
loggerTagsFactory,
mtimeSortFilesDesc,
timestampFormatter,
type LoggerTags,
type LoggerTagsFn
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function removeCyclicValues () { function removeCyclicValues () {
@ -222,3 +216,8 @@ function getAdditionalInfo (info: any) {
return omit(info, toOmit) 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) { function buildPluginLogger (npmName: string) {
return buildLogger(npmName) return buildLogger({ labelSuffix: npmName })
} }
function buildDatabaseHelpers () { function buildDatabaseHelpers () {

View file

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