]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
Move adding a video view videojs peertube plugin
[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
23e27dd5
C
12// Use object for better performances (~ O(1))
13const excludedKeys = {
14 level: true,
15 message: true,
16 splat: true,
17 timestamp: true,
18 label: true
19}
20function keysExcluder (key, value) {
21 return excludedKeys[key] === true ? undefined : value
22}
23
e3a682a8 24const loggerFormat = winston.format.printf(info => {
23e27dd5
C
25 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
26 if (additionalInfos === '{}') additionalInfos = ''
2af4fa4d 27 else additionalInfos = ' ' + additionalInfos
23e27dd5 28
1e9d7b60 29 if (info.message && info.message.stack !== undefined) info.message = info.message.stack
2af4fa4d 30 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
31})
32
33const timestampFormatter = winston.format.timestamp({
1e9d7b60 34 format: 'YYYY-MM-dd HH:mm:ss.SSS'
23e27dd5
C
35})
36const labelFormatter = winston.format.label({
37 label
38})
39
40const logger = new winston.createLogger({
41 level: CONFIG.LOG.LEVEL,
9f10b292
C
42 transports: [
43 new winston.transports.File({
23e27dd5 44 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
9f10b292 45 handleExceptions: true,
9f10b292
C
46 maxsize: 5242880,
47 maxFiles: 5,
23e27dd5
C
48 format: winston.format.combine(
49 timestampFormatter,
50 labelFormatter,
51 winston.format.splat(),
52 winston.format.json()
53 )
9f10b292
C
54 }),
55 new winston.transports.Console({
9f10b292
C
56 handleExceptions: true,
57 humanReadableUnhandledException: true,
23e27dd5
C
58 format: winston.format.combine(
59 timestampFormatter,
60 winston.format.splat(),
61 labelFormatter,
62 winston.format.colorize(),
63 loggerFormat
64 )
9f10b292
C
65 })
66 ],
67 exitOnError: true
68})
8c308c2b 69
9f10b292 70// ---------------------------------------------------------------------------
c45f7f84 71
23e27dd5
C
72export {
73 timestampFormatter,
74 labelFormatter,
75 loggerFormat,
76 logger
77}