]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
remove isAdministration
[github/Chocobozzz/PeerTube.git] / server / helpers / logger.ts
CommitLineData
9f10b292 1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
c9d5c64f 2import { mkdirpSync } from 'fs-extra'
4d4e5cd4
C
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
c9d5c64f 10mkdirpSync(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 => {
328e607d
C
25 const obj = {
26 meta: info.meta,
27 err: info.err,
28 sql: info.sql
29 }
30
31 let additionalInfos = JSON.stringify(obj, loggerReplacer, 2)
e20015d7 32 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
2af4fa4d 33 else additionalInfos = ' ' + additionalInfos
23e27dd5 34
2af4fa4d 35 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
36})
37
e20015d7
C
38const jsonLoggerFormat = winston.format.printf(info => {
39 return JSON.stringify(info, loggerReplacer)
276d03ed
C
40})
41
23e27dd5 42const timestampFormatter = winston.format.timestamp({
0647f472 43 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5
C
44})
45const labelFormatter = winston.format.label({
46 label
47})
48
85b4d9c5 49const logger = winston.createLogger({
23e27dd5 50 level: CONFIG.LOG.LEVEL,
e20015d7
C
51 format: winston.format.combine(
52 labelFormatter,
53 winston.format.splat()
54 ),
9f10b292
C
55 transports: [
56 new winston.transports.File({
23e27dd5 57 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
9f10b292 58 handleExceptions: true,
7361c401 59 maxsize: 1024 * 1024 * 12,
9f10b292 60 maxFiles: 5,
23e27dd5 61 format: winston.format.combine(
0647f472 62 winston.format.timestamp(),
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,
23e27dd5 70 winston.format.colorize(),
276d03ed 71 consoleLoggerFormat
23e27dd5 72 )
9f10b292
C
73 })
74 ],
75 exitOnError: true
76})
8c308c2b 77
05e67d62
C
78function bunyanLogFactory (level: string) {
79 return function () {
80 let meta = null
c1e791ba
RK
81 let args: any[] = []
82 args.concat(arguments)
05e67d62
C
83
84 if (arguments[ 0 ] instanceof Error) {
85 meta = arguments[ 0 ].toString()
86 args = Array.prototype.slice.call(arguments, 1)
87 args.push(meta)
88 } else if (typeof (args[ 0 ]) !== 'string') {
89 meta = arguments[ 0 ]
90 args = Array.prototype.slice.call(arguments, 1)
91 args.push(meta)
92 }
93
94 logger[ level ].apply(logger, args)
95 }
96}
97const 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')
104}
9f10b292 105// ---------------------------------------------------------------------------
c45f7f84 106
23e27dd5
C
107export {
108 timestampFormatter,
109 labelFormatter,
276d03ed 110 consoleLoggerFormat,
59390818 111 jsonLoggerFormat,
05e67d62
C
112 logger,
113 bunyanLogger
23e27dd5 114}