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 { CONFIG } from '../initializers'
7 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
9 // Create the directory if it does not exist
10 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
12 function loggerReplacer (key: string, value: any) {
13 if (value instanceof Error) {
16 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ])
24 const consoleLoggerFormat = winston.format.printf(info => {
25 let additionalInfos = JSON.stringify(info.meta || info.err, loggerReplacer, 2)
26 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
27 else additionalInfos = ' ' + additionalInfos
29 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
32 const jsonLoggerFormat = winston.format.printf(info => {
33 return JSON.stringify(info, loggerReplacer)
36 const timestampFormatter = winston.format.timestamp({
37 format: 'YYYY-MM-DD HH:mm:ss.SSS'
39 const labelFormatter = winston.format.label({
43 const logger = winston.createLogger({
44 level: CONFIG.LOG.LEVEL,
45 format: winston.format.combine(
47 winston.format.splat()
50 new winston.transports.File({
51 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
52 handleExceptions: true,
53 maxsize: 1024 * 1024 * 30,
55 format: winston.format.combine(
56 winston.format.timestamp(),
60 new winston.transports.Console({
61 handleExceptions: true,
62 format: winston.format.combine(
64 winston.format.colorize(),
72 function bunyanLogFactory (level: string) {
76 args.concat(arguments)
78 if (arguments[ 0 ] instanceof Error) {
79 meta = arguments[ 0 ].toString()
80 args = Array.prototype.slice.call(arguments, 1)
82 } else if (typeof (args[ 0 ]) !== 'string') {
84 args = Array.prototype.slice.call(arguments, 1)
88 logger[ level ].apply(logger, args)
91 const bunyanLogger = {
92 trace: bunyanLogFactory('debug'),
93 debug: bunyanLogFactory('debug'),
94 info: bunyanLogFactory('info'),
95 warn: bunyanLogFactory('warn'),
96 error: bunyanLogFactory('error'),
97 fatal: bunyanLogFactory('error')
99 // ---------------------------------------------------------------------------