diff --git a/server/core/helpers/logger.ts b/server/core/helpers/logger.ts index 07c67eff6..4b554ecfa 100644 --- a/server/core/helpers/logger.ts +++ b/server/core/helpers/logger.ts @@ -1,15 +1,17 @@ -import { isatty } from 'tty' import { context, trace } from '@opentelemetry/api' import { omit } from '@peertube/peertube-core-utils' +import { isTestOrDevInstance } from '@peertube/peertube-node-utils' import { stat } from 'fs/promises' import { join } from 'path' import { format as sqlFormat } from 'sql-formatter' +import { isatty } from 'tty' import { createLogger, format, transports } from 'winston' import { FileTransportOptions } from 'winston/lib/winston/transports' import { CONFIG } from '../initializers/config.js' import { LOG_FILENAME } from '../initializers/constants.js' -const isCompatibleTerminal = isatty && isatty(1) && process.env.TERM && process.env.TERM !== 'dumb' +const consoleSupportsColor = isTestOrDevInstance() || + (isatty(1) && process.env.TERM && process.env.TERM !== 'dumb') const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT @@ -61,29 +63,28 @@ if (CONFIG.LOG.ROTATION.ENABLED) { } function buildLogger (labelSuffix?: string) { - let consoleTransport = new transports.Console({ + const formatters = [ timestampFormatter ] + + if (consoleSupportsColor) formatters.push(format.colorize()) + formatters.push(consoleLoggerFormat) + + const consoleTransport = new transports.Console({ handleExceptions: true, - format: format.combine( - timestampFormatter, - format.colorize(), - consoleLoggerFormat - ) + format: format.combine(...formatters) }) - if (!isCompatibleTerminal) { - consoleTransport = new transports.Console({ - handleExceptions: true, - format: format.combine( - timestampFormatter, - consoleLoggerFormat - ) - }) - } + return createLogger({ level: process.env.LOGGER_LEVEL ?? CONFIG.LOG.LEVEL, defaultMeta: { - get traceId () { return trace.getSpanContext(context.active())?.traceId }, - get spanId () { return trace.getSpanContext(context.active())?.spanId }, - get traceFlags () { return trace.getSpanContext(context.active())?.traceFlags } + get traceId () { + return trace.getSpanContext(context.active())?.traceId + }, + get spanId () { + return trace.getSpanContext(context.active())?.spanId + }, + get traceFlags () { + return trace.getSpanContext(context.active())?.traceFlags + } }, format: format.combine( labelFormatter(labelSuffix), @@ -121,7 +122,7 @@ function bunyanLogFactory (level: string) { } const bunyanLogger = { - level: () => { }, + level: () => {}, trace: bunyanLogFactory('debug'), debug: bunyanLogFactory('debug'), verbose: bunyanLogFactory('debug'), @@ -166,10 +167,17 @@ async function mtimeSortFilesDesc (files: string[], basePath: string) { // --------------------------------------------------------------------------- export { - - buildLogger, bunyanLogger, consoleLoggerFormat, - jsonLoggerFormat, labelFormatter, logger, - loggerTagsFactory, mtimeSortFilesDesc, timestampFormatter, type LoggerTags, type LoggerTagsFn + buildLogger, + bunyanLogger, + consoleLoggerFormat, + jsonLoggerFormat, + labelFormatter, + logger, + loggerTagsFactory, + mtimeSortFilesDesc, + timestampFormatter, + type LoggerTags, + type LoggerTagsFn } // --------------------------------------------------------------------------- @@ -198,7 +206,9 @@ function removeCyclicValues () { if (value instanceof Error) { const error = {} - Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] }) + Object.getOwnPropertyNames(value).forEach(key => { + error[key] = value[key] + }) return error }