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 => {
31 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 logger = winston.createLogger({
50 level: CONFIG.LOG.LEVEL,
51 format: winston.format.combine(
53 winston.format.splat()
56 new winston.transports.File({
57 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
58 handleExceptions: true,
59 maxsize: 1024 * 1024 * 12,
61 format: winston.format.combine(
62 winston.format.timestamp(),
66 new winston.transports.Console({
67 handleExceptions: true,
68 format: winston.format.combine(
70 winston.format.colorize(),
78 function bunyanLogFactory (level: string) {
82 args.concat(arguments)
84 if (arguments[ 0 ] instanceof Error) {
85 meta = arguments[ 0 ].toString()
86 args = Array.prototype.slice.call(arguments, 1)
88 } else if (typeof (args[ 0 ]) !== 'string') {
90 args = Array.prototype.slice.call(arguments, 1)
94 logger[ level ].apply(logger, args)
97 const bunyanLogger = {
98 trace: bunyanLogFactory('debug'),
99 debug: bunyanLogFactory('debug'),
100 info: bunyanLogFactory('info'),
101 warn: bunyanLogFactory('warn'),
102 error: bunyanLogFactory('error'),
103 fatal: bunyanLogFactory('error')
105 // ---------------------------------------------------------------------------