Handle thumbnail update
[oweals/peertube.git] / server / helpers / logger.ts
index 3c35e41e066952622d820e0f3b66170edd7c29a4..bcd4885af961a9a1a087ab98806fc46bb158da36 100644 (file)
@@ -1,48 +1,77 @@
 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
-import mkdirp = require('mkdirp')
-import path = require('path')
-import winston = require('winston')
-
-// Do not use barrel (dependencies issues)
-import { CONFIG } from '../initializers/constants'
+import * as mkdirp from 'mkdirp'
+import * as path from 'path'
+import * as winston from 'winston'
+import { CONFIG } from '../initializers'
 
 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
 
 // Create the directory if it does not exist
 mkdirp.sync(CONFIG.STORAGE.LOG_DIR)
 
-const logger = new winston.Logger({
+// Use object for better performances (~ O(1))
+const excludedKeys = {
+  level: true,
+  message: true,
+  splat: true,
+  timestamp: true,
+  label: true
+}
+function keysExcluder (key, value) {
+  return excludedKeys[key] === true ? undefined : value
+}
+
+const loggerFormat = winston.format.printf(info => {
+  let additionalInfos = JSON.stringify(info, keysExcluder, 2)
+  if (additionalInfos === '{}') additionalInfos = ''
+  else additionalInfos = ' ' + additionalInfos
+
+  if (info.message && info.message.stack !== undefined) info.message = info.message.stack
+  return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
+})
+
+const timestampFormatter = winston.format.timestamp({
+  format: 'YYYY-MM-dd HH:mm:ss.SSS'
+})
+const labelFormatter = winston.format.label({
+  label
+})
+
+const logger = new winston.createLogger({
+  level: CONFIG.LOG.LEVEL,
   transports: [
     new winston.transports.File({
-      level: 'debug',
-      filename: path.join(CONFIG.STORAGE.LOG_DIR, 'all-logs.log'),
+      filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube.log'),
       handleExceptions: true,
-      json: true,
       maxsize: 5242880,
       maxFiles: 5,
-      colorize: false,
-      prettyPrint: true
+      format: winston.format.combine(
+        timestampFormatter,
+        labelFormatter,
+        winston.format.splat(),
+        winston.format.json()
+      )
     }),
     new winston.transports.Console({
-      level: 'debug',
-      label: label,
       handleExceptions: true,
       humanReadableUnhandledException: true,
-      json: false,
-      colorize: true,
-      prettyPrint: true
+      format: winston.format.combine(
+        timestampFormatter,
+        winston.format.splat(),
+        labelFormatter,
+        winston.format.colorize(),
+        loggerFormat
+      )
     })
   ],
   exitOnError: true
 })
 
-// TODO: useful?
-// logger.stream = {
-//   write: function (message) {
-//     logger.info(message)
-//   }
-// }
-
 // ---------------------------------------------------------------------------
 
-export { logger }
+export {
+  timestampFormatter,
+  labelFormatter,
+  loggerFormat,
+  logger
+}