]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
Replacing the err being thrown by a gracefull exit.process(1)
[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) {
21 return excludedKeys[key] === true ? undefined : value
22}
23
276d03ed 24const consoleLoggerFormat = winston.format.printf(info => {
23e27dd5
C
25 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
26 if (additionalInfos === '{}') additionalInfos = ''
2af4fa4d 27 else additionalInfos = ' ' + additionalInfos
23e27dd5 28
1e9d7b60 29 if (info.message && info.message.stack !== undefined) info.message = info.message.stack
2af4fa4d 30 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
31})
32
276d03ed
C
33const jsonLoggerFormat = winston.format.printf(info => {
34 if (info.message && info.message.stack !== undefined) info.message = info.message.stack
35
36 return JSON.stringify(info)
37})
38
23e27dd5 39const timestampFormatter = winston.format.timestamp({
0647f472 40 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5
C
41})
42const labelFormatter = winston.format.label({
43 label
44})
45
46const logger = new winston.createLogger({
47 level: CONFIG.LOG.LEVEL,
9f10b292
C
48 transports: [
49 new winston.transports.File({
23e27dd5 50 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
9f10b292 51 handleExceptions: true,
9f10b292
C
52 maxsize: 5242880,
53 maxFiles: 5,
23e27dd5 54 format: winston.format.combine(
0647f472 55 winston.format.timestamp(),
23e27dd5
C
56 labelFormatter,
57 winston.format.splat(),
276d03ed 58 jsonLoggerFormat
23e27dd5 59 )
9f10b292
C
60 }),
61 new winston.transports.Console({
e8cb4409 62 handleExceptions: true,
9f10b292 63 humanReadableUnhandledException: true,
23e27dd5
C
64 format: winston.format.combine(
65 timestampFormatter,
66 winston.format.splat(),
67 labelFormatter,
68 winston.format.colorize(),
276d03ed 69 consoleLoggerFormat
23e27dd5 70 )
9f10b292
C
71 })
72 ],
73 exitOnError: true
74})
8c308c2b 75
05e67d62
C
76function bunyanLogFactory (level: string) {
77 return function () {
78 let meta = null
79 let args = [].concat(arguments)
80
81 if (arguments[ 0 ] instanceof Error) {
82 meta = arguments[ 0 ].toString()
83 args = Array.prototype.slice.call(arguments, 1)
84 args.push(meta)
85 } else if (typeof (args[ 0 ]) !== 'string') {
86 meta = arguments[ 0 ]
87 args = Array.prototype.slice.call(arguments, 1)
88 args.push(meta)
89 }
90
91 logger[ level ].apply(logger, args)
92 }
93}
94const bunyanLogger = {
95 trace: bunyanLogFactory('debug'),
96 debug: bunyanLogFactory('debug'),
97 info: bunyanLogFactory('info'),
98 warn: bunyanLogFactory('warn'),
99 error: bunyanLogFactory('error'),
100 fatal: bunyanLogFactory('error')
101}
102
9f10b292 103// ---------------------------------------------------------------------------
c45f7f84 104
23e27dd5
C
105export {
106 timestampFormatter,
107 labelFormatter,
276d03ed 108 consoleLoggerFormat,
05e67d62
C
109 logger,
110 bunyanLogger
23e27dd5 111}