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 return excludedKeys[key] === true ? undefined : value
24 const consoleLoggerFormat = winston.format.printf(info => {
25 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
26 if (additionalInfos === '{}') additionalInfos = ''
27 else additionalInfos = ' ' + additionalInfos
29 if (info.message && info.message.stack !== undefined) info.message = info.message.stack
30 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
33 const jsonLoggerFormat = winston.format.printf(info => {
34 if (info.message && info.message.stack !== undefined) info.message = info.message.stack
36 return JSON.stringify(info)
39 const timestampFormatter = winston.format.timestamp({
40 format: 'YYYY-MM-dd HH:mm:ss.SSS'
42 const labelFormatter = winston.format.label({
46 const logger = new winston.createLogger({
47 level: CONFIG.LOG.LEVEL,
49 new winston.transports.File({
50 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
51 handleExceptions: true,
54 format: winston.format.combine(
57 winston.format.splat(),
61 new winston.transports.Console({
62 handleExceptions: true,
63 humanReadableUnhandledException: true,
64 format: winston.format.combine(
66 winston.format.splat(),
68 winston.format.colorize(),
76 // ---------------------------------------------------------------------------