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