PeerTube/server/core/helpers/logger.ts

202 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-06-21 14:32:25 +02:00
import { context, trace } from '@opentelemetry/api'
import { omit } from '@peertube/peertube-core-utils'
import { stat } from 'fs/promises'
2021-08-27 14:32:44 +02:00
import { join } from 'path'
import { format as sqlFormat } from 'sql-formatter'
2021-08-27 14:32:44 +02:00
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'
2015-06-09 17:41:40 +02:00
2017-05-15 22:22:03 +02:00
const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
2021-08-27 14:32:44 +02:00
const consoleLoggerFormat = format.printf(info => {
2022-07-05 15:43:21 +02:00
let additionalInfos = JSON.stringify(getAdditionalInfo(info), removeCyclicValues(), 2)
2019-04-10 15:26:33 +02:00
2018-07-30 10:59:31 +02:00
if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
2018-01-19 14:47:03 +01:00
else additionalInfos = ' ' + additionalInfos
2018-01-19 13:58:13 +01:00
2021-01-26 10:03:41 +01:00
if (info.sql) {
if (CONFIG.LOG.PRETTIFY_SQL) {
additionalInfos += '\n' + sqlFormat(info.sql, {
2024-10-31 10:49:24 +01:00
language: 'postgresql',
2022-06-21 11:16:38 +02:00
tabWidth: 2
2021-01-26 10:03:41 +01:00
})
} else {
additionalInfos += ' - ' + info.sql
}
}
2018-01-19 14:47:03 +01:00
return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
2018-01-19 13:58:13 +01:00
})
2021-08-27 14:32:44 +02:00
const jsonLoggerFormat = format.printf(info => {
2022-07-05 15:43:21 +02:00
return JSON.stringify(info, removeCyclicValues())
})
2021-08-27 14:32:44 +02:00
const timestampFormatter = format.timestamp({
2018-03-08 18:16:15 +01:00
format: 'YYYY-MM-DD HH:mm:ss.SSS'
2018-01-19 13:58:13 +01:00
})
2020-04-09 09:57:32 +02:00
const labelFormatter = (suffix?: string) => {
2021-08-27 14:32:44 +02:00
return format.label({
2020-04-09 09:57:32 +02:00
label: suffix ? `${label} ${suffix}` : label
})
}
2018-01-19 13:58:13 +01:00
const fileLoggerOptions: FileTransportOptions = {
2021-08-27 14:32:44 +02:00
filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
handleExceptions: true,
2021-08-27 14:32:44 +02:00
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
}
2020-04-09 09:57:32 +02:00
function buildLogger (labelSuffix?: string) {
2021-08-27 14:32:44 +02:00
return createLogger({
level: process.env.LOGGER_LEVEL ?? CONFIG.LOG.LEVEL,
2022-07-05 15:43:21 +02:00
defaultMeta: {
2024-06-21 14:32:25 +02:00
get traceId () { return trace.getSpanContext(context.active())?.traceId },
get spanId () { return trace.getSpanContext(context.active())?.spanId },
get traceFlags () { return trace.getSpanContext(context.active())?.traceFlags }
2022-07-05 15:43:21 +02:00
},
2021-08-27 14:32:44 +02:00
format: format.combine(
2020-04-09 09:57:32 +02:00
labelFormatter(labelSuffix),
2021-08-27 14:32:44 +02:00
format.splat()
2020-04-09 09:57:32 +02:00
),
transports: [
2021-08-27 14:32:44 +02:00
new transports.File(fileLoggerOptions),
new transports.Console({
2020-04-09 09:57:32 +02:00
handleExceptions: true,
2021-08-27 14:32:44 +02:00
format: format.combine(
2020-04-09 09:57:32 +02:00
timestampFormatter,
2021-08-27 14:32:44 +02:00
format.colorize(),
2020-04-09 09:57:32 +02:00
consoleLoggerFormat
)
})
],
exitOnError: true
})
}
2015-06-09 17:41:40 +02:00
2022-07-05 15:43:21 +02:00
const logger = buildLogger()
// ---------------------------------------------------------------------------
2018-03-22 11:32:43 +01:00
function bunyanLogFactory (level: string) {
2021-10-11 14:49:10 +02:00
return function (...params: any[]) {
2018-03-22 11:32:43 +01:00
let meta = null
2021-10-11 14:49:10 +02:00
let args = [].concat(params)
2018-03-22 11:32:43 +01:00
2020-01-31 16:56:52 +01:00
if (arguments[0] instanceof Error) {
meta = arguments[0].toString()
2018-03-22 11:32:43 +01:00
args = Array.prototype.slice.call(arguments, 1)
args.push(meta)
2020-01-31 16:56:52 +01:00
} else if (typeof (args[0]) !== 'string') {
meta = arguments[0]
2018-03-22 11:32:43 +01:00
args = Array.prototype.slice.call(arguments, 1)
args.push(meta)
}
2020-01-31 16:56:52 +01:00
logger[level].apply(logger, args)
2018-03-22 11:32:43 +01:00
}
}
2020-01-31 16:56:52 +01:00
2018-03-22 11:32:43 +01:00
const bunyanLogger = {
2021-10-11 14:49:10 +02:00
level: () => { },
2018-03-22 11:32:43 +01:00
trace: bunyanLogFactory('debug'),
debug: bunyanLogFactory('debug'),
2022-07-05 15:43:21 +02:00
verbose: bunyanLogFactory('debug'),
2018-03-22 11:32:43 +01:00
info: bunyanLogFactory('info'),
warn: bunyanLogFactory('warn'),
error: bunyanLogFactory('error'),
fatal: bunyanLogFactory('error')
}
2022-07-05 15:43:21 +02:00
// ---------------------------------------------------------------------------
2024-02-22 10:12:04 +01:00
type LoggerTags = { tags: (string | number)[] }
type LoggerTagsFn = (...tags: (string | number)[]) => LoggerTags
function loggerTagsFactory (...defaultTags: (string | number)[]): LoggerTagsFn {
return (...tags: (string | number)[]) => {
return { tags: defaultTags.concat(tags) }
}
}
2022-07-05 15:43:21 +02:00
// ---------------------------------------------------------------------------
2021-07-26 15:04:37 +02:00
async function mtimeSortFilesDesc (files: string[], basePath: string) {
const promises = []
const out: { file: string, mtime: number }[] = []
for (const file of files) {
const p = stat(basePath + '/' + file)
.then(stats => {
if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
})
promises.push(p)
}
await Promise.all(promises)
out.sort((a, b) => b.mtime - a.mtime)
return out
}
// ---------------------------------------------------------------------------
2016-01-31 11:23:52 +01:00
2018-01-19 13:58:13 +01:00
export {
buildLogger, bunyanLogger, consoleLoggerFormat,
jsonLoggerFormat, labelFormatter, logger,
loggerTagsFactory, mtimeSortFilesDesc, timestampFormatter, type LoggerTags, type LoggerTagsFn
2018-01-19 13:58:13 +01:00
}
2022-07-05 15:43:21 +02:00
// ---------------------------------------------------------------------------
function removeCyclicValues () {
const seen = new WeakSet()
// Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
return (key: string, value: any) => {
if (key === 'cert') return 'Replaced by the logger to avoid large log message'
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return
seen.add(value)
}
if (value instanceof Set) {
return Array.from(value)
}
if (value instanceof Map) {
return Array.from(value.entries())
}
if (value instanceof Error) {
const error = {}
Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
return error
}
return value
}
}
function getAdditionalInfo (info: any) {
const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ]
2022-08-17 15:25:58 +02:00
return omit(info, toOmit)
2022-07-05 15:43:21 +02:00
}