Skip to content

Commit

Permalink
feat: add log4js api logger config
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed Mar 28, 2023
1 parent af5d59d commit 3ed65f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/log4js.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"type": "pattern",
"pattern": "%[[%d] [%p] [file: %f] [line: %l]%] %c - %m"
}
},
"api": {
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%[[%d] [%p] [file: %f] [line: %l]%] %c - [%X{apiName}] [path: %X{originalUrl}] [IP: %X{ip}] [%m]%n"
}
}
},
"categories": {
Expand Down Expand Up @@ -35,6 +42,13 @@
],
"level": "DEBUG",
"enableCallStack": true
},
"api": {
"appenders": [
"api"
],
"level": "DEBUG",
"enableCallStack": true
}
}
}
8 changes: 8 additions & 0 deletions utils/logs/api-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ApiLogger {
this.apiName = apiName;

let ip = this.request.headers["x-forwarded-for"] || this.request.socket.remoteAddress;
this.ip = ip;
this.prefixMessage = `[${this.apiName}] [path: ${this.request.originalUrl}] [IP: ${ip}]`;
this.logger = getLogger("api");
}

/**
Expand Down Expand Up @@ -61,6 +63,12 @@ class ApiLogger {

raccoonLogger.error(errorMessage);
}

addTokenValue() {
this.logger.addContext("apiName", this.apiName);
this.logger.addContext("originalUrl", this.request.originalUrl);
this.logger.addContext("ip", this.ip);
}
}

module.exports.ApiLogger = ApiLogger;

0 comments on commit 3ed65f5

Please sign in to comment.