Browse Source

Improved the logging configuration

pull/10/head
Drew Short 4 years ago
parent
commit
738a3cbc51
  1. 2
      .dockerignore
  2. 4
      .gitignore
  3. 34
      bot/bot.js
  4. 15
      bot/logging.js
  5. 29
      combined.log

2
.dockerignore

@ -1,2 +1,2 @@
data/config.json
data/*.log
log/

4
.gitignore

@ -3,7 +3,9 @@ node_modules/
# Config files
data/config.json
data/*.log
# Log files
log/
# Mac Files
.DS_Store

34
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();
}

15
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()
)
}));
}

29
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"}
Loading…
Cancel
Save