1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Force colors on dev/test mode

Concurrently program is used and not detected as a tty
This commit is contained in:
Chocobozzz 2025-04-17 14:28:40 +02:00 committed by Chocobozzz
parent 8de04c14e9
commit 8f72e470f4

View file

@ -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
}