X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Flogger.ts;h=29e06860dbf9ca0604805dc94bfeab2e75aa1d46;hb=295106516277581ba4967199fa5580264a90ae2c;hp=9553f70e80d60986149c7eb8726ea6d2e7c84f31;hpb=610d0be13b3d01f653ef269271dd667a57c85ef2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 9553f70e8..29e06860d 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts @@ -1,10 +1,11 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ import { mkdirpSync } from 'fs-extra' +import { omit } from 'lodash' import * as path from 'path' +import { format as sqlFormat } from 'sql-formatter' import * as winston from 'winston' import { FileTransportOptions } from 'winston/lib/winston/transports' import { CONFIG } from '../initializers/config' -import { omit } from 'lodash' import { LOG_FILENAME } from '../initializers/constants' const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT @@ -18,12 +19,22 @@ function getLoggerReplacer () { // 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 = {} @@ -37,13 +48,26 @@ function getLoggerReplacer () { } const consoleLoggerFormat = winston.format.printf(info => { - const obj = omit(info, 'label', 'timestamp', 'level', 'message') + const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ] + + const obj = omit(info, ...toOmit) let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' else additionalInfos = ' ' + additionalInfos + if (info.sql) { + if (CONFIG.LOG.PRETTIFY_SQL) { + additionalInfos += '\n' + sqlFormat(info.sql, { + language: 'sql', + indent: ' ' + }) + } else { + additionalInfos += ' - ' + info.sql + } + } + return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` }) @@ -126,14 +150,25 @@ const bunyanLogger = { error: bunyanLogFactory('error'), fatal: bunyanLogFactory('error') } + +type LoggerTagsFn = (...tags: string[]) => { tags: string[] } +function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn { + return (...tags: string[]) => { + return { tags: defaultTags.concat(tags) } + } +} + // --------------------------------------------------------------------------- export { + LoggerTagsFn, + buildLogger, timestampFormatter, labelFormatter, consoleLoggerFormat, jsonLoggerFormat, logger, + loggerTagsFactory, bunyanLogger }