Skip to content

Commit

Permalink
Enhance Loggin
Browse files Browse the repository at this point in the history
This will enhance the logging of error messages in catch blocks (the javascript Error object).

Due to a problem on wiston, when logging an error object, the winston just prints the messages, and all important details (like error line, etc,) is hidden.
Details: winstonjs/winston#1338

For example, if I'm developing a module or something fails with an Error, this is what is printed in the wiki.js log:


2022-09-16T02:31:41.383Z [MASTER] info: =======================================
2022-09-16T02:31:41.385Z [MASTER] info: = Wiki.js 2.5.286 =====================
2022-09-16T02:31:41.385Z [MASTER] info: =======================================
2022-09-16T02:31:41.386Z [MASTER] info: Initializing...
2022-09-16T02:31:42.334Z [MASTER] info: Using database driver pg for postgres [ OK ]
2022-09-16T02:31:42.337Z [MASTER] info: Connecting to database...
2022-09-16T02:31:42.501Z [MASTER] info: Database Connection Successful [ OK ]
2022-09-16T02:31:43.507Z [MASTER] warn: Mail is not setup! Please set the configuration in the administration area!
2022-09-16T02:31:43.675Z [MASTER] info: Loading GraphQL Schema...
2022-09-16T02:31:45.155Z [MASTER] info: GraphQL Schema: [ OK ]
2022-09-16T02:31:45.437Z [MASTER] error: SOME_ERROR is not defined




After this fix, the error is printed with more info:


Loading configuration from T:\powerdocs\wikijs\config.yml... OK
2022-09-16T02:32:08.650Z [MASTER] info: =======================================
2022-09-16T02:32:08.652Z [MASTER] info: = Wiki.js 2.5.286 =====================
2022-09-16T02:32:08.653Z [MASTER] info: =======================================
2022-09-16T02:32:08.653Z [MASTER] info: Initializing...
2022-09-16T02:32:09.627Z [MASTER] info: Using database driver pg for postgres [ OK ]
2022-09-16T02:32:09.632Z [MASTER] info: Connecting to database...
2022-09-16T02:32:09.897Z [MASTER] info: Database Connection Successful [ OK ]
2022-09-16T02:32:10.932Z [MASTER] warn: Mail is not setup! Please set the configuration in the administration area!
2022-09-16T02:32:11.116Z [MASTER] info: Loading GraphQL Schema...
2022-09-16T02:32:12.949Z [MASTER] info: GraphQL Schema: [ OK ]
2022-09-16T02:32:13.276Z [MASTER] error: ReferenceError: SOME_ERROR is not defined
    at module.exports (T:\wikijs\server\master.js:172:3)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.bootMaster (T:\wikijs\server\core\kernel.js:60:9)
  • Loading branch information
rrg92 committed Sep 16, 2022
1 parent 665284b commit 895a556
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions server/core/logger.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
// const _ = require('lodash')
const winston = require('winston')


/* global WIKI */

let WikiErrorFormat = winston.format((info)=>{

var errorObject = null;

if(info instanceof Error){
errorObject = info;
}
else if(info.message instanceof Error){
errorObject = info.message;
}

info._wiki_error = errorObject;

return info;
})


let WikiLoggerPrintf = (info) =>{
var msg = info.message;

if(info._wiki_error){
msg = info._wiki_error.stack;
}



return `${info.timestamp} [${info.label}] ${info.level}: ${msg}`;

}



module.exports = {
loggers: {},
init(uid) {
const loggerFormats = [
WikiErrorFormat(),
winston.format.label({ label: uid }),
winston.format.timestamp()
,winston.format.errors({stack: true })
]

if (WIKI.config.logFormat === 'json') {
loggerFormats.push(winston.format.json())
} else {
loggerFormats.push(winston.format.colorize())
loggerFormats.push(winston.format.printf(info => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`))
loggerFormats.push(winston.format.printf(WikiLoggerPrintf))
}

const logger = winston.createLogger({
level: WIKI.config.logLevel,
format: winston.format.combine(...loggerFormats)
level: WIKI.config.logLevel
,format: winston.format.combine(...loggerFormats)
})

// Init Console (default)
Expand Down

0 comments on commit 895a556

Please sign in to comment.