X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Flogger.ts;h=0548dfd5ba4a6e1e4c4993ee21e95f366df0cf86;hb=1e743faafeed89af13ee9dd3d62c1ceb696779cd;hp=b8ae28b3f12235d69b2d8c26430fcfb0f09bb6ca;hpb=134cf2bce96a8c5aefd55154e884964975d8cf23;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index b8ae28b3f..0548dfd5b 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,6 +19,8 @@ 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 @@ -37,13 +40,23 @@ function getLoggerReplacer () { } const consoleLoggerFormat = winston.format.printf(info => { - const obj = omit(info, 'label', 'timestamp', 'level', 'message') + const toOmit = [ 'label', 'timestamp', 'level', 'message' ] + if (CONFIG.LOG.PRETTIFY_SQL) toOmit.push('sql') + + const obj = omit(info, ...toOmit) let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' else additionalInfos = ' ' + additionalInfos + if (CONFIG.LOG.PRETTIFY_SQL && info.sql) { + additionalInfos += '\n' + sqlFormat(info.sql, { + language: 'sql', + ident: ' ' + }) + } + return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` }) @@ -54,9 +67,11 @@ const jsonLoggerFormat = winston.format.printf(info => { const timestampFormatter = winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }) -const labelFormatter = winston.format.label({ - label -}) +const labelFormatter = (suffix?: string) => { + return winston.format.label({ + label: suffix ? `${label} ${suffix}` : label + }) +} const fileLoggerOptions: FileTransportOptions = { filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), @@ -72,25 +87,29 @@ if (CONFIG.LOG.ROTATION.ENABLED) { fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES } -const logger = winston.createLogger({ - level: CONFIG.LOG.LEVEL, - format: winston.format.combine( - labelFormatter, - winston.format.splat() - ), - transports: [ - new winston.transports.File(fileLoggerOptions), - new winston.transports.Console({ - handleExceptions: true, - format: winston.format.combine( - timestampFormatter, - winston.format.colorize(), - consoleLoggerFormat - ) - }) - ], - exitOnError: true -}) +const logger = buildLogger() + +function buildLogger (labelSuffix?: string) { + return winston.createLogger({ + level: CONFIG.LOG.LEVEL, + format: winston.format.combine( + labelFormatter(labelSuffix), + winston.format.splat() + ), + transports: [ + new winston.transports.File(fileLoggerOptions), + new winston.transports.Console({ + handleExceptions: true, + format: winston.format.combine( + timestampFormatter, + winston.format.colorize(), + consoleLoggerFormat + ) + }) + ], + exitOnError: true + }) +} function bunyanLogFactory (level: string) { return function () { @@ -123,6 +142,7 @@ const bunyanLogger = { // --------------------------------------------------------------------------- export { + buildLogger, timestampFormatter, labelFormatter, consoleLoggerFormat,