]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/logger.ts
Use source as target in translations
[github/Chocobozzz/PeerTube.git] / server / helpers / logger.ts
1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 import { mkdirpSync } from 'fs-extra'
3 import * as path from 'path'
4 import * as winston from 'winston'
5 import { FileTransportOptions } from 'winston/lib/winston/transports'
6 import { CONFIG } from '../initializers/config'
7 import { omit } from 'lodash'
8
9 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
10
11 // Create the directory if it does not exist
12 // FIXME: use async
13 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
14
15 function getLoggerReplacer () {
16 const seen = new WeakSet()
17
18 // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
19 return (key: string, value: any) => {
20 if (typeof value === 'object' && value !== null) {
21 if (seen.has(value)) return
22
23 seen.add(value)
24 }
25
26 if (value instanceof Error) {
27 const error = {}
28
29 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ])
30
31 return error
32 }
33
34 return value
35 }
36 }
37
38 const consoleLoggerFormat = winston.format.printf(info => {
39 const obj = omit(info, 'label', 'timestamp', 'level', 'message')
40
41 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
42
43 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
44 else additionalInfos = ' ' + additionalInfos
45
46 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
47 })
48
49 const jsonLoggerFormat = winston.format.printf(info => {
50 return JSON.stringify(info, getLoggerReplacer())
51 })
52
53 const timestampFormatter = winston.format.timestamp({
54 format: 'YYYY-MM-DD HH:mm:ss.SSS'
55 })
56 const labelFormatter = winston.format.label({
57 label
58 })
59
60 const fileLoggerOptions: FileTransportOptions = {
61 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
62 handleExceptions: true,
63 format: winston.format.combine(
64 winston.format.timestamp(),
65 jsonLoggerFormat
66 )
67 }
68
69 if (CONFIG.LOG.ROTATION) {
70 fileLoggerOptions.maxsize = 1024 * 1024 * 12
71 fileLoggerOptions.maxFiles = 20
72 }
73
74 const logger = winston.createLogger({
75 level: CONFIG.LOG.LEVEL,
76 format: winston.format.combine(
77 labelFormatter,
78 winston.format.splat()
79 ),
80 transports: [
81 new winston.transports.File(fileLoggerOptions),
82 new winston.transports.Console({
83 handleExceptions: true,
84 format: winston.format.combine(
85 timestampFormatter,
86 winston.format.colorize(),
87 consoleLoggerFormat
88 )
89 })
90 ],
91 exitOnError: true
92 })
93
94 function bunyanLogFactory (level: string) {
95 return function () {
96 let meta = null
97 let args: any[] = []
98 args.concat(arguments)
99
100 if (arguments[ 0 ] instanceof Error) {
101 meta = arguments[ 0 ].toString()
102 args = Array.prototype.slice.call(arguments, 1)
103 args.push(meta)
104 } else if (typeof (args[ 0 ]) !== 'string') {
105 meta = arguments[ 0 ]
106 args = Array.prototype.slice.call(arguments, 1)
107 args.push(meta)
108 }
109
110 logger[ level ].apply(logger, args)
111 }
112 }
113 const bunyanLogger = {
114 trace: bunyanLogFactory('debug'),
115 debug: bunyanLogFactory('debug'),
116 info: bunyanLogFactory('info'),
117 warn: bunyanLogFactory('warn'),
118 error: bunyanLogFactory('error'),
119 fatal: bunyanLogFactory('error')
120 }
121 // ---------------------------------------------------------------------------
122
123 export {
124 timestampFormatter,
125 labelFormatter,
126 consoleLoggerFormat,
127 jsonLoggerFormat,
128 logger,
129 bunyanLogger
130 }