From 8a4f90263a636014d73b11b9c2d8e402b7b70af5 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Wed, 1 Feb 2023 23:02:33 -0700 Subject: [PATCH] Split out web logging to it's own logger/files/configuration --- core/config_default.js | 11 +++++++ core/logger.js | 16 ++++++---- core/servers/content/web.js | 6 ++-- .../servers/content/web_handlers/nodeinfo2.js | 2 +- core/web_log.js | 29 +++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 core/web_log.js diff --git a/core/config_default.js b/core/config_default.js index e2752603..87b586c2 100644 --- a/core/config_default.js +++ b/core/config_default.js @@ -302,6 +302,17 @@ module.exports = () => { staticRoot: paths.join(__dirname, './../www'), + // Logging block works the same way the system logger does + logging: { + rotatingFile: { + level: 'info', + type: 'rotating-file', + fileName: 'enigma-bbs.web.log', + period: '1d', + count: 3, + }, + }, + handlers: { systemGeneral: { enabled: true, diff --git a/core/logger.js b/core/logger.js index a7806683..37f4f319 100644 --- a/core/logger.js +++ b/core/logger.js @@ -27,6 +27,16 @@ module.exports = class Log { logStreams.push(Config.logging.rotatingFile); } + const serializers = Log.standardSerializers(); + + this.log = bunyan.createLogger({ + name: 'ENiGMA½ BBS', + streams: logStreams, + serializers: serializers, + }); + } + + static standardSerializers() { const serializers = { err: bunyan.stdSerializers.err, // handle 'err' fields with stack/etc. }; @@ -36,11 +46,7 @@ module.exports = class Log { serializers[keyName] = fd => Log.hideSensitive(fd); }); - this.log = bunyan.createLogger({ - name: 'ENiGMA½ BBS', - streams: logStreams, - serializers: serializers, - }); + return serializers; } static checkLogPath(logPath) { diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 713b7a4a..8042ec47 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -15,6 +15,7 @@ const paths = require('path'); const mimeTypes = require('mime-types'); const forEachSeries = require('async/forEachSeries'); const findSeries = require('async/findSeries'); +const WebLog = require('../../web_log.js'); const ModuleInfo = (exports.moduleInfo = { name: 'Web', @@ -74,7 +75,8 @@ exports.getModule = class WebServerModule extends ServerModule { constructor() { super(); - this.log = Log.child({ server: 'Web' }); + //this.log = Log.child({ server: 'Web' }); + this.log = WebLog.createWebLog(); const config = Config(); this.enableHttp = config.contentServers.web.http.enabled || false; @@ -276,7 +278,7 @@ exports.getModule = class WebServerModule extends ServerModule { } routeRequest(req, resp) { - this.log.trace({ url: req.url, method: req.method }, 'Request'); + this.log.trace({ req }, 'Request'); let route = _.find(this.routes, r => r.matchesRequest(req)); diff --git a/core/servers/content/web_handlers/nodeinfo2.js b/core/servers/content/web_handlers/nodeinfo2.js index e376c75a..4be7f85c 100644 --- a/core/servers/content/web_handlers/nodeinfo2.js +++ b/core/servers/content/web_handlers/nodeinfo2.js @@ -45,7 +45,7 @@ exports.getModule = class NodeInfo2WebHadnler extends WebHandlerModule { } _nodeInfo2Handler(req, resp) { - this.log.info({ url: req.url }, 'Serving NodeInfo2 request'); + this.log.info('Serving NodeInfo2 request'); this._getNodeInfo(nodeInfo => { const body = JSON.stringify(nodeInfo); diff --git a/core/web_log.js b/core/web_log.js new file mode 100644 index 00000000..d463a324 --- /dev/null +++ b/core/web_log.js @@ -0,0 +1,29 @@ +const Logger = require('./logger'); +const Config = require('./config').get; + +// deps +const paths = require('path'); +const bunyan = require('bunyan'); +const { get } = require('lodash'); + +module.exports = class WebLog { + static createWebLog() { + const config = Config(); + const logPath = config.paths.logs; + const rotatingFile = get(config, 'contentServers.web.logging.rotatingFile'); + + rotatingFile.path = paths.join(logPath, rotatingFile.fileName); + + const serializers = Logger.standardSerializers(); + serializers.req = bunyan.stdSerializers.req; + serializers.res = bunyan.stdSerializers.res; + + const webLog = bunyan.createLogger({ + name: 'ENiGMA½ BBS[Web]', + streams: [rotatingFile], + serializers, + }); + + return webLog; + } +};