From cd1273981d42c11ecf42e79927e52b005e600f37 Mon Sep 17 00:00:00 2001 From: softsimon Date: Thu, 15 Apr 2021 16:04:09 +0400 Subject: [PATCH 1/2] Syslog is now configurable with minimum priority and facility option. fixes #135 --- backend/mempool-config.sample.json | 7 +++++++ backend/src/config.ts | 16 ++++++++++++++++ backend/src/logger.ts | 18 +++++++----------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index c444929b5..1582929aa 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -39,6 +39,13 @@ "USERNAME": "mempool", "PASSWORD": "mempool" }, + "SYSLOG": { + "ENABLED": true, + "HOST": "127.0.0.1", + "PORT": 514, + "MIN_PRIORITY": "info", + "FACILITY": "local7" + }, "STATISTICS": { "ENABLED": true, "TX_PER_SECOND_SAMPLE_PERIOD": 150 diff --git a/backend/src/config.ts b/backend/src/config.ts index ace63394c..d5828f922 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -41,6 +41,13 @@ interface IConfig { USERNAME: string; PASSWORD: string; }; + SYSLOG: { + ENABLED: boolean; + HOST: string; + PORT: number; + MIN_PRIORITY: 'emerg' | 'alert' | 'crit' | 'err' |'warn' | 'notice' | 'info' | 'debug'; + FACILITY: string; + }; STATISTICS: { ENABLED: boolean; TX_PER_SECOND_SAMPLE_PERIOD: number; @@ -96,6 +103,13 @@ const defaults: IConfig = { 'USERNAME': 'mempool', 'PASSWORD': 'mempool' }, + 'SYSLOG': { + 'ENABLED': true, + 'HOST': '127.0.0.1', + 'PORT': 514, + 'MIN_PRIORITY': 'info', + 'FACILITY': 'local7' + }, 'STATISTICS': { 'ENABLED': true, 'TX_PER_SECOND_SAMPLE_PERIOD': 150 @@ -117,6 +131,7 @@ class Config implements IConfig { CORE_RPC: IConfig['CORE_RPC']; CORE_RPC_MINFEE: IConfig['CORE_RPC_MINFEE']; DATABASE: IConfig['DATABASE']; + SYSLOG: IConfig['SYSLOG']; STATISTICS: IConfig['STATISTICS']; BISQ_BLOCKS: IConfig['BISQ_BLOCKS']; BISQ_MARKETS: IConfig['BISQ_MARKETS']; @@ -129,6 +144,7 @@ class Config implements IConfig { this.CORE_RPC = configs.CORE_RPC; this.CORE_RPC_MINFEE = configs.CORE_RPC_MINFEE; this.DATABASE = configs.DATABASE; + this.SYSLOG = configs.SYSLOG; this.STATISTICS = configs.STATISTICS; this.BISQ_BLOCKS = configs.BISQ_BLOCKS; this.BISQ_MARKETS = configs.BISQ_MARKETS; diff --git a/backend/src/logger.ts b/backend/src/logger.ts index 41aaf5c8f..8c7548d24 100644 --- a/backend/src/logger.ts +++ b/backend/src/logger.ts @@ -50,17 +50,11 @@ class Logger { public debug: ((msg: string) => void); private name = 'mempool'; - private fac: any; - private loghost: string; - private logport: number; private client: dgram.Socket; private network: string; - constructor(fac) { + constructor() { let prio; - this.fac = fac != null ? fac : Logger.facilities.local0; - this.loghost = '127.0.0.1'; - this.logport = 514; for (prio in Logger.priorities) { if (true) { this.addprio(prio); @@ -97,10 +91,12 @@ class Logger { } const network = this.network ? ' <' + this.network + '>' : ''; prionum = Logger.priorities[priority] || Logger.priorities.info; - syslogmsg = `<${(this.fac * 8 + prionum)}> ${this.name}[${process.pid}]: ${priority.toUpperCase()}${network} ${msg}`; consolemsg = `${this.ts()} [${process.pid}] ${priority.toUpperCase()}:${network} ${msg}`; - this.syslog(syslogmsg); + if (config.SYSLOG.ENABLED && Logger.priorities[priority] >= Logger.priorities[config.SYSLOG.MIN_PRIORITY]) { + syslogmsg = `<${(Logger.facilities[config.SYSLOG.FACILITY] * 8 + prionum)}> ${this.name}[${process.pid}]: ${priority.toUpperCase()}${network} ${msg}`; + this.syslog(syslogmsg); + } if (priority === 'warning') { priority = 'warn'; } @@ -116,7 +112,7 @@ class Logger { private syslog(msg) { let msgbuf; msgbuf = Buffer.from(msg); - this.client.send(msgbuf, 0, msgbuf.length, this.logport, this.loghost, function(err, bytes) { + this.client.send(msgbuf, 0, msgbuf.length, config.SYSLOG.PORT, config.SYSLOG.HOST, function(err, bytes) { if (err) { console.log(err); } @@ -146,4 +142,4 @@ class Logger { } } -export default new Logger(Logger.facilities.local7); +export default new Logger(); From d1b53f4c3ae88a9023ed01aebde451433a6e1991 Mon Sep 17 00:00:00 2001 From: softsimon Date: Thu, 15 Apr 2021 21:16:56 +0400 Subject: [PATCH 2/2] Syslog priority comparison fix. --- backend/src/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/logger.ts b/backend/src/logger.ts index 8c7548d24..f14af0104 100644 --- a/backend/src/logger.ts +++ b/backend/src/logger.ts @@ -93,7 +93,7 @@ class Logger { prionum = Logger.priorities[priority] || Logger.priorities.info; consolemsg = `${this.ts()} [${process.pid}] ${priority.toUpperCase()}:${network} ${msg}`; - if (config.SYSLOG.ENABLED && Logger.priorities[priority] >= Logger.priorities[config.SYSLOG.MIN_PRIORITY]) { + if (config.SYSLOG.ENABLED && Logger.priorities[priority] <= Logger.priorities[config.SYSLOG.MIN_PRIORITY]) { syslogmsg = `<${(Logger.facilities[config.SYSLOG.FACILITY] * 8 + prionum)}> ${this.name}[${process.pid}]: ${priority.toUpperCase()}${network} ${msg}`; this.syslog(syslogmsg); }