mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
feat: Modify logger to log to file that supports fail2ban (#284)
This commit is contained in:
parent
5a27ac0f03
commit
fe5fe5fab7
12 changed files with 427 additions and 7844 deletions
42
server/utils/logger.js
Normal file
42
server/utils/logger.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
const winston = require('winston');
|
||||
|
||||
/**
|
||||
* The default timestamp used by the logger.
|
||||
* Format example: "2022-08-18 6:30:02"
|
||||
*/
|
||||
const defaultLogTimestampFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
|
||||
const logfile = `${process.cwd()}/logs/planka.log`;
|
||||
|
||||
/**
|
||||
* Log level for both console and file log sinks.
|
||||
*
|
||||
* Refer {@link https://github.com/winstonjs/winston#logging here}
|
||||
* for more information on Winston log levels.
|
||||
*/
|
||||
const logLevel = process.env.NODE_ENV === 'production' ? 'info' : 'debug';
|
||||
|
||||
const logFormat = winston.format.combine(
|
||||
winston.format.uncolorize(),
|
||||
winston.format.timestamp({ format: defaultLogTimestampFormat }),
|
||||
winston.format.printf((log) => `${log.timestamp} [${log.level[0].toUpperCase()}] ${log.message}`),
|
||||
);
|
||||
|
||||
// eslint-disable-next-line new-cap
|
||||
const customLogger = new winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.File({
|
||||
level: logLevel,
|
||||
format: logFormat,
|
||||
filename: logfile,
|
||||
}),
|
||||
new winston.transports.Console({
|
||||
level: logLevel,
|
||||
format: logFormat,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
customLogger,
|
||||
};
|
27
server/utils/remoteAddress.js
Normal file
27
server/utils/remoteAddress.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* The IP address of the client that just made a request to this application, whether
|
||||
* or not the TRUST_PROXY env variable is true and if endpoint accessed through a proxy.
|
||||
* @param {Request} request The endpoint Request object
|
||||
* @returns The IP address of the client that just made a request
|
||||
*/
|
||||
const getRemoteAddress = (request) => {
|
||||
let remoteAddress = request.ip;
|
||||
|
||||
// Assert if "X-Forwarded-For" header contains any addresses
|
||||
if (process.env.TRUST_PROXY && !_.isEmpty(request.ips)) {
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
remoteAddress = request.ips[0];
|
||||
}
|
||||
|
||||
// Convert address from IPV6 to IPV4 if client device is IPV4.
|
||||
const defaultIPV6Regex = /^::ffff:((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/g;
|
||||
if (remoteAddress.match(defaultIPV6Regex)) {
|
||||
remoteAddress = remoteAddress.replace('::ffff:', '');
|
||||
}
|
||||
|
||||
return remoteAddress;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getRemoteAddress,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue