From 738a3cbc51bca3a1ad2ea0808c3c75c821da809f Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sun, 29 Dec 2019 13:22:23 -0600 Subject: [PATCH] Improved the logging configuration --- .dockerignore | 2 +- .gitignore | 4 +++- bot/bot.js | 34 +++++++++++++++++----------------- bot/logging.js | 15 ++++++++++----- combined.log | 29 +++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 24 deletions(-) diff --git a/.dockerignore b/.dockerignore index f49b408..0b2e08c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,2 @@ data/config.json -data/*.log \ No newline at end of file +log/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 41cef64..cf70976 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,9 @@ node_modules/ # Config files data/config.json -data/*.log + +# Log files +log/ # Mac Files .DS_Store \ No newline at end of file diff --git a/bot/bot.js b/bot/bot.js index e026e93..dd44b4d 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -2,7 +2,7 @@ let fs = require('fs'); let sdk = require('matrix-js-sdk'); let message = require('./message.js'); let utility = require('./utility.js'); -let logging = require('./logging'); +let { logger } = require('./logging'); class Bot { constructor(config, buildInfo) { @@ -15,15 +15,15 @@ class Bot { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { - logging.logger.debug("Notifying %s", roomId, "of startup"); + logger.debug("Notifying %s of startup", roomId); promises.push(this.client.sendMessage( roomId, message.createBasic("Started with version: " + this.buildInfo) ).then(function () { - logging.logger.debug("Notified %s", roomId, "of startup"); + logger.debug("Notified %s of startup", roomId); })); }); } else { - logging.logger.warn("Attempting to send startup message while disconnected"); + logger.warn("Attempting to send startup message while disconnected"); } return Promise.all(promises); } @@ -32,15 +32,15 @@ class Bot { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { - logging.logger.debug("Notifying %s", roomId, "of Shutdown"); + logger.debug("Notifying %s of shutdown", roomId); promises.push(this.client.sendMessage( roomId, message.createBasic("Shutting down") ).then(function () { - logging.logger.debug("Notified %s", roomId, "of shutdown"); + logger.debug("Notified %s of shutdown", roomId); })); }); } else { - logging.logger.warn("Attempting to send shutdown message while disconnected"); + logger.warn("Attempting to send shutdown message while disconnected"); } return Promise.all(promises); } @@ -53,7 +53,7 @@ function getBuildInfo(buildInfoPath) { if (err.code === 'ENOENT') { return "UNKNOWN_" + utility.toISODateString(new Date()); } else { - logging.logger.error("Unexpected Error! " + err); + logger.error("Unexpected Error!", err); } } } @@ -66,15 +66,15 @@ function sanitizeConfig(config) { function create(configFile) { let config = require(configFile); - logging.logger.info("Running with config:"); - logging.logger.debug(sanitizeConfig(config)); + logger.info("Running with config:"); + logger.debug("%o", sanitizeConfig(config)); let buildInfo = getBuildInfo("../build.info") - logging.logger.info("Running version:", buildInfo); + logger.info("Running version: %s", buildInfo); return new Bot(config, buildInfo); } function init(bot) { - logging.logger.info("Creating Matrix Client") + logger.info("Creating Matrix Client") bot.client = sdk.createClient({ baseUrl: bot.config.baseUrl, accessToken: bot.config.accessToken, @@ -94,7 +94,7 @@ function init(bot) { if (member.membership === "invite" && bot.config.admin.indexOf(ember.userId) >= 0) { bot.client.joinRoom(member.roomId).done(function () { - logging.logger.info("Auto-joined %s", member.roomId); + logger.info("Auto-joined %s", member.roomId); }); } }); @@ -105,7 +105,7 @@ function init(bot) { process.on(signature, async () => { await bot.sendStatusShutdown() .then(function () { - logging.logger.info("Gracefully stopping Matrix SDK Client") + logger.info("Gracefully stopping Matrix SDK Client") bot.client.stopClient(); }); process.exit(0); @@ -113,16 +113,16 @@ function init(bot) { }); process.on('exit', async function () { - logging.logger.info("Shutting Down"); + logger.info("Shutting Down"); }); return bot; } function run(bot) { - // logging.logger.info("Initializing Crypto"); + // logger.info("Initializing Crypto"); // await bot.client.initCrypto(); - logging.logger.info("Starting Matrix SDK Client"); + logger.info("Starting Matrix SDK Client"); bot.client.startClient(); } diff --git a/bot/logging.js b/bot/logging.js index 64faeef..0c5c87b 100644 --- a/bot/logging.js +++ b/bot/logging.js @@ -2,17 +2,22 @@ let winston = require('winston'); let logger = winston.createLogger({ level: 'info', - format: winston.format.json(), - defaultMeta: { service: 'user-service' }, + format: winston.format.combine( + winston.format.splat(), + winston.format.json() + ), + defaultMeta: { service: 'baphomet-js' }, transports: [ - new winston.transports.File({ filename: 'error.log', level: 'error' }), - new winston.transports.File({ filename: 'combined.log' }) + new winston.transports.File({ filename: 'log/error.log', level: 'error' }), + new winston.transports.File({ filename: 'log/combined.log' }) ] }); if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ - format: winston.format.simple() + format: winston.format.combine( + winston.format.simple() + ) })); } diff --git a/combined.log b/combined.log index 1bfc68d..e754a7d 100644 --- a/combined.log +++ b/combined.log @@ -5,3 +5,32 @@ {"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} {"service":"user-service","level":"debug","message":"Notifying %s"} {"service":"user-service","level":"debug","message":"Notified %s"} +{"service":"user-service","level":"debug","message":"Notifying %s"} +{"service":"user-service","level":"debug","message":"Notified %s"} +{"message":"Running with config:","level":"info","service":"user-service"} +{"message":{"baseUrl":"https://matrix.nulloctet.com","userId":"@baphomet-dev:nulloctet.com","accessToken":"******","admins":["@warrick:nulloctet.com"],"anyCanInvite":false,"statusRooms":["!ayFeLNSFDrerMMelCg:nulloctet.com"]},"level":"debug","service":"user-service"} +{"service":"user-service","level":"info","message":"Running version:"} +{"message":"Creating Matrix Client","level":"info","service":"user-service"} +{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} +{"service":"user-service","level":"debug","message":"Notifying %s of startup"} +{"service":"user-service","level":"debug","message":"Notified %s of startup"} +{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} +{"service":"user-service","level":"debug","message":"Notified %s of shutdown"} +{"message":"Running with config:","level":"info","service":"user-service"} +{"message":{"baseUrl":"https://matrix.nulloctet.com","userId":"@baphomet-dev:nulloctet.com","accessToken":"******","admins":["@warrick:nulloctet.com"],"anyCanInvite":false,"statusRooms":["!ayFeLNSFDrerMMelCg:nulloctet.com"]},"level":"debug","service":"user-service"} +{"service":"user-service","level":"info","message":"Running version:"} +{"message":"Creating Matrix Client","level":"info","service":"user-service"} +{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} +{"service":"user-service","level":"debug","message":"Notifying %s of startup"} +{"service":"user-service","level":"debug","message":"Notified %s of startup"} +{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} +{"service":"user-service","level":"debug","message":"Notified %s of shutdown"} +{"message":"Running with config:","level":"info","service":"user-service"} +{"service":"user-service","level":"debug","message":"%o"} +{"service":"user-service","level":"info","message":"Running version: %s"} +{"message":"Creating Matrix Client","level":"info","service":"user-service"} +{"message":"Starting Matrix SDK Client","level":"info","service":"user-service"} +{"service":"user-service","level":"debug","message":"Notifying %s of startup"} +{"service":"user-service","level":"debug","message":"Notified %s of startup"} +{"service":"user-service","level":"debug","message":"Notifying %s of shutdown"} +{"service":"user-service","level":"debug","message":"Notified %s of shutdown"}