1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 import { mkdirpSync } from 'fs-extra'
3 import * as path from 'path'
4 import * as winston from 'winston'
5 import { FileTransportOptions } from 'winston/lib/winston/transports'
6 import { CONFIG } from '../initializers/config'
7 import { omit } from 'lodash'
9 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
11 // Create the directory if it does not exist
13 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
15 function loggerReplacer (key: string, value: any) {
16 if (value instanceof Error) {
19 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ])
27 const consoleLoggerFormat = winston.format.printf(info => {
28 const obj = omit(info, 'label', 'timestamp', 'level', 'message')
30 let additionalInfos = JSON.stringify(obj, loggerReplacer, 2)
32 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
33 else additionalInfos = ' ' + additionalInfos
35 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
38 const jsonLoggerFormat = winston.format.printf(info => {
39 return JSON.stringify(info, loggerReplacer)
42 const timestampFormatter = winston.format.timestamp({
43 format: 'YYYY-MM-DD HH:mm:ss.SSS'
45 const labelFormatter = winston.format.label({
49 const fileLoggerOptions: FileTransportOptions = {
51 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
52 handleExceptions: true,
53 format: winston.format.combine(
54 winston.format.timestamp(),
59 if (CONFIG.LOG.ROTATION) {
60 fileLoggerOptions.maxsize = 1024 * 1024 * 12
61 fileLoggerOptions.maxFiles = 20
64 const logger = winston.createLogger({
65 level: CONFIG.LOG.LEVEL,
66 format: winston.format.combine(
68 winston.format.splat()
71 new winston.transports.File(fileLoggerOptions),
72 new winston.transports.Console({
73 handleExceptions: true,
74 format: winston.format.combine(
76 winston.format.colorize(),
84 function bunyanLogFactory (level: string) {
88 args.concat(arguments)
90 if (arguments[ 0 ] instanceof Error) {
91 meta = arguments[ 0 ].toString()
92 args = Array.prototype.slice.call(arguments, 1)
94 } else if (typeof (args[ 0 ]) !== 'string') {
96 args = Array.prototype.slice.call(arguments, 1)
100 logger[ level ].apply(logger, args)
103 const bunyanLogger = {
104 trace: bunyanLogFactory('debug'),
105 debug: bunyanLogFactory('debug'),
106 info: bunyanLogFactory('info'),
107 warn: bunyanLogFactory('warn'),
108 error: bunyanLogFactory('error'),
109 fatal: bunyanLogFactory('error')
111 // ---------------------------------------------------------------------------