]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/logger.ts
improve description of the HTTP video import feature
[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 { CONFIG } from '../initializers'
6
7 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
8
9 // Create the directory if it does not exist
10 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
11
12 function loggerReplacer (key: string, value: any) {
13 if (value instanceof Error) {
14 const error = {}
15
16 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ])
17
18 return error
19 }
20
21 return value
22 }
23
24 const consoleLoggerFormat = winston.format.printf(info => {
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)
32 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
33 else additionalInfos = ' ' + additionalInfos
34
35 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
36 })
37
38 const jsonLoggerFormat = winston.format.printf(info => {
39 return JSON.stringify(info, loggerReplacer)
40 })
41
42 const timestampFormatter = winston.format.timestamp({
43 format: 'YYYY-MM-DD HH:mm:ss.SSS'
44 })
45 const labelFormatter = winston.format.label({
46 label
47 })
48
49 const logger = winston.createLogger({
50 level: CONFIG.LOG.LEVEL,
51 format: winston.format.combine(
52 labelFormatter,
53 winston.format.splat()
54 ),
55 transports: [
56 new winston.transports.File({
57 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
58 handleExceptions: true,
59 maxsize: 1024 * 1024 * 12,
60 maxFiles: 5,
61 format: winston.format.combine(
62 winston.format.timestamp(),
63 jsonLoggerFormat
64 )
65 }),
66 new winston.transports.Console({
67 handleExceptions: true,
68 format: winston.format.combine(
69 timestampFormatter,
70 winston.format.colorize(),
71 consoleLoggerFormat
72 )
73 })
74 ],
75 exitOnError: true
76 })
77
78 function bunyanLogFactory (level: string) {
79 return function () {
80 let meta = null
81 let args: any[] = []
82 args.concat(arguments)
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 }
97 const 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 }
105 // ---------------------------------------------------------------------------
106
107 export {
108 timestampFormatter,
109 labelFormatter,
110 consoleLoggerFormat,
111 jsonLoggerFormat,
112 logger,
113 bunyanLogger
114 }