1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 import * as mkdirp from 'mkdirp'
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 mkdirp.sync(CONFIG.STORAGE.LOG_DIR)
12 // Use object for better performances (~ O(1))
13 const excludedKeys = {
20 function keysExcluder (key, value) {
21 if (excludedKeys[key] === true) return undefined
23 if (key === 'err') return value.stack
28 const consoleLoggerFormat = winston.format.printf(info => {
29 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
30 if (additionalInfos === '{}') additionalInfos = ''
31 else additionalInfos = ' ' + additionalInfos
33 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
36 const jsonLoggerFormat = winston.format.printf(infoArg => {
37 let info = infoArg.err
38 ? Object.assign({}, infoArg, { err: infoArg.err.stack })
41 return JSON.stringify(info)
44 const timestampFormatter = winston.format.timestamp({
45 format: 'YYYY-MM-DD HH:mm:ss.SSS'
47 const labelFormatter = winston.format.label({
51 const logger = winston.createLogger({
52 level: CONFIG.LOG.LEVEL,
54 new winston.transports.File({
55 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
56 handleExceptions: true,
59 format: winston.format.combine(
60 winston.format.timestamp(),
62 winston.format.splat(),
66 new winston.transports.Console({
67 handleExceptions: true,
68 format: winston.format.combine(
70 winston.format.splat(),
72 winston.format.colorize(),
80 function bunyanLogFactory (level: string) {
84 args.concat(arguments)
86 if (arguments[ 0 ] instanceof Error) {
87 meta = arguments[ 0 ].toString()
88 args = Array.prototype.slice.call(arguments, 1)
90 } else if (typeof (args[ 0 ]) !== 'string') {
92 args = Array.prototype.slice.call(arguments, 1)
96 logger[ level ].apply(logger, args)
99 const bunyanLogger = {
100 trace: bunyanLogFactory('debug'),
101 debug: bunyanLogFactory('debug'),
102 info: bunyanLogFactory('info'),
103 warn: bunyanLogFactory('warn'),
104 error: bunyanLogFactory('error'),
105 fatal: bunyanLogFactory('error')
108 // ---------------------------------------------------------------------------