]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
Upgrade dependencies
[github/Chocobozzz/PeerTube.git] / server / helpers / logger.ts
CommitLineData
9f10b292 1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
4d4e5cd4
C
2import * as mkdirp from 'mkdirp'
3import * as path from 'path'
4import * as winston from 'winston'
3fd3ab2d 5import { CONFIG } from '../initializers'
8c308c2b 6
65fcc311 7const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
320d6275
C
8
9// Create the directory if it does not exist
65fcc311 10mkdirp.sync(CONFIG.STORAGE.LOG_DIR)
320d6275 11
23e27dd5
C
12// Use object for better performances (~ O(1))
13const excludedKeys = {
14 level: true,
15 message: true,
16 splat: true,
17 timestamp: true,
18 label: true
19}
20function keysExcluder (key, value) {
d5b7d911
C
21 if (excludedKeys[key] === true) return undefined
22
23 if (key === 'err') return value.stack
24
25 return value
23e27dd5
C
26}
27
276d03ed 28const consoleLoggerFormat = winston.format.printf(info => {
23e27dd5
C
29 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
30 if (additionalInfos === '{}') additionalInfos = ''
2af4fa4d 31 else additionalInfos = ' ' + additionalInfos
23e27dd5 32
2af4fa4d 33 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
34})
35
d5b7d911
C
36const jsonLoggerFormat = winston.format.printf(infoArg => {
37 let info = infoArg.err
38 ? Object.assign({}, infoArg, { err: infoArg.err.stack })
39 : infoArg
40
276d03ed
C
41 return JSON.stringify(info)
42})
43
23e27dd5 44const timestampFormatter = winston.format.timestamp({
0647f472 45 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5
C
46})
47const labelFormatter = winston.format.label({
48 label
49})
50
85b4d9c5 51const logger = winston.createLogger({
23e27dd5 52 level: CONFIG.LOG.LEVEL,
9f10b292
C
53 transports: [
54 new winston.transports.File({
23e27dd5 55 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
9f10b292 56 handleExceptions: true,
9f10b292
C
57 maxsize: 5242880,
58 maxFiles: 5,
23e27dd5 59 format: winston.format.combine(
0647f472 60 winston.format.timestamp(),
23e27dd5
C
61 labelFormatter,
62 winston.format.splat(),
276d03ed 63 jsonLoggerFormat
23e27dd5 64 )
9f10b292
C
65 }),
66 new winston.transports.Console({
e8cb4409 67 handleExceptions: true,
23e27dd5
C
68 format: winston.format.combine(
69 timestampFormatter,
70 winston.format.splat(),
71 labelFormatter,
72 winston.format.colorize(),
276d03ed 73 consoleLoggerFormat
23e27dd5 74 )
9f10b292
C
75 })
76 ],
77 exitOnError: true
78})
8c308c2b 79
05e67d62
C
80function bunyanLogFactory (level: string) {
81 return function () {
82 let meta = null
83 let args = [].concat(arguments)
84
85 if (arguments[ 0 ] instanceof Error) {
86 meta = arguments[ 0 ].toString()
87 args = Array.prototype.slice.call(arguments, 1)
88 args.push(meta)
89 } else if (typeof (args[ 0 ]) !== 'string') {
90 meta = arguments[ 0 ]
91 args = Array.prototype.slice.call(arguments, 1)
92 args.push(meta)
93 }
94
95 logger[ level ].apply(logger, args)
96 }
97}
98const bunyanLogger = {
99 trace: bunyanLogFactory('debug'),
100 debug: bunyanLogFactory('debug'),
101 info: bunyanLogFactory('info'),
102 warn: bunyanLogFactory('warn'),
103 error: bunyanLogFactory('error'),
104 fatal: bunyanLogFactory('error')
105}
106
9f10b292 107// ---------------------------------------------------------------------------
c45f7f84 108
23e27dd5
C
109export {
110 timestampFormatter,
111 labelFormatter,
276d03ed 112 consoleLoggerFormat,
05e67d62
C
113 logger,
114 bunyanLogger
23e27dd5 115}