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