-mkdirp.sync(CONFIG.STORAGE.LOG_DIR)
-
-const logger = new winston.Logger({
- transports: [
- new winston.transports.File({
- level: 'debug',
- filename: path.join(CONFIG.STORAGE.LOG_DIR, 'all-logs.log'),
- handleExceptions: true,
- json: true,
- maxsize: 5242880,
- maxFiles: 5,
- colorize: false,
- prettyPrint: true
- }),
- new winston.transports.Console({
- level: 'debug',
- label: label,
- handleExceptions: true,
- humanReadableUnhandledException: true,
- json: false,
- colorize: true,
- prettyPrint: true
- })
- ],
- exitOnError: true
+// FIXME: use async
+mkdirpSync(CONFIG.STORAGE.LOG_DIR)
+
+function getLoggerReplacer () {
+ const seen = new WeakSet()
+
+ // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
+ return (key: string, value: any) => {
+ if (key === 'cert') return 'Replaced by the logger to avoid large log message'
+
+ if (typeof value === 'object' && value !== null) {
+ if (seen.has(value)) return
+
+ seen.add(value)
+ }
+
+ if (value instanceof Set) {
+ return Array.from(value)
+ }
+
+ if (value instanceof Map) {
+ return Array.from(value.entries())
+ }
+
+ if (value instanceof Error) {
+ const error = {}
+
+ Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
+
+ return error
+ }
+
+ return value
+ }
+}
+
+const consoleLoggerFormat = format.printf(info => {
+ const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ]
+
+ const obj = omit(info, ...toOmit)
+
+ let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
+
+ if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
+ else additionalInfos = ' ' + additionalInfos
+
+ if (info.sql) {
+ if (CONFIG.LOG.PRETTIFY_SQL) {
+ additionalInfos += '\n' + sqlFormat(info.sql, {
+ language: 'sql',
+ indent: ' '
+ })
+ } else {
+ additionalInfos += ' - ' + info.sql
+ }
+ }
+
+ return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
+})
+
+const jsonLoggerFormat = format.printf(info => {
+ return JSON.stringify(info, getLoggerReplacer())
+})
+
+const timestampFormatter = format.timestamp({
+ format: 'YYYY-MM-DD HH:mm:ss.SSS'