Skip to content

Commit

Permalink
refactor: rename logger factory to log factory; refactor framework bo…
Browse files Browse the repository at this point in the history
…otstrap file
  • Loading branch information
simplymichael committed Jun 19, 2024
1 parent a7e0012 commit ef2f901
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/framework/bootstrap/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const debug = require("../lib/debug");


module.exports = function setupServices(config, providers) {
for(let i = 0; i < providers.length; i++) {
const Provider = providers[i];
Expand All @@ -19,10 +22,14 @@ module.exports = function setupServices(config, providers) {
);
}

debug(`Registering provider '${className}'`);

/*
* Bind the dependencies of the service(s) that the provider provides
* to the service container.
*/
provider.register();

debug(`Provider '${className}' registration complete.`);
}
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const winston = require("winston");
require("winston-daily-rotate-file");


module.exports = class LoggerFactory {
module.exports = class LogFactory {
/**
* @param {Object} options
* @param {String} [options.label]
Expand Down Expand Up @@ -82,6 +82,8 @@ module.exports = class LoggerFactory {
}
}

const logLevels = levels || winston.config.npm.levels;
const levelKeys = Object.keys(logLevels);
const formats = winston.format.combine(
winston.format.label({ label, message: true }),
winston.format.errors({ stack: true }),
Expand All @@ -98,6 +100,7 @@ module.exports = class LoggerFactory {
transports,
exceptionHandlers,
rejectionHandlers,
level: levelKeys[levelKeys.length - 1], // Make all the levels available to the client.
defaultMeta: { service: label ?? "FrameworkLogger" },
exitOnError: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const util = require("node:util");
const sinon = require("sinon");
const winston = require("winston");
const { chai } = require("../../lib/test-helper");
const LoggerFactory = require(".");
const LogFactory = require(".");


const thisDir = path.resolve(__dirname, ".").replace(/\\/g, "/");
Expand Down Expand Up @@ -62,14 +62,14 @@ module.exports = {

describe(".createLogger(options)", function() {
it("should return a logger object", function() {
const logger = LoggerFactory.createLogger();
const logger = LogFactory.createLogger();

expect(logger).to.be.an("object");
expect(logger).to.have.property("log").to.be.a("function");
});

it("should use the 'console' as the default transport", function() {
const logger = LoggerFactory.createLogger();
const logger = LogFactory.createLogger();
const { sinonSpy, restore } = spyOnConsoleOutput();

// Call the logger.log, which internally calls console._stdout.write
Expand All @@ -85,7 +85,7 @@ module.exports = {
});

it("should use the label 'FrameworkLogger' as the default 'label' for the logger", function() {
const logger = LoggerFactory.createLogger();
const logger = LogFactory.createLogger();
const { sinonSpy, restore } = spyOnConsoleOutput();

// Call the logger.log, which internally calls console._stdout.write
Expand All @@ -102,7 +102,7 @@ module.exports = {

it("should let the user specify a custom 'label' for the logger", function() {
const label = "LogService";
const logger = LoggerFactory.createLogger({ label });
const logger = LogFactory.createLogger({ label });
const { sinonSpy, restore } = spyOnConsoleOutput();

// Call the logger.log, which internally calls console._stdout.write
Expand All @@ -118,7 +118,7 @@ module.exports = {
});

it("should use npm log levels as the default log priority protocol", function() {
const logger = LoggerFactory.createLogger();
const logger = LogFactory.createLogger();
const logLevels = ["error", "warn", "info", "http", "verbose", "debug", "silly"];

for(const level of logLevels) {
Expand All @@ -132,7 +132,7 @@ module.exports = {
warning: 4, notice: 5, info: 6, debug: 7
};

const logger = LoggerFactory.createLogger({ levels: syslogLevels });
const logger = LogFactory.createLogger({ levels: syslogLevels });
const logLevels = Object.keys(syslogLevels);

for(const level of logLevels) {
Expand All @@ -141,7 +141,7 @@ module.exports = {
});

it("should let the user disable console logging", function() {
let logger = LoggerFactory.createLogger({ disableConsoleLogs: true });
let logger = LogFactory.createLogger({ disableConsoleLogs: true });
const { sinonSpy, restore } = spyOnConsoleOutput("stderr");

// Call the logger.log, which internally calls console._stdout.write
Expand All @@ -163,18 +163,18 @@ module.exports = {

fs.rmSync(scopedLogDir, { recursive: true, force: true });

LoggerFactory.createLogger();
LogFactory.createLogger();

expect(fs.existsSync(scopedLogDir)).to.be.false;

LoggerFactory.createLogger({ logToFile: true, logDir: scopedLogDir });
LogFactory.createLogger({ logToFile: true, logDir: scopedLogDir });

expect(fs.existsSync(scopedLogDir)).to.be.true;
});

it("should throw if the 'logToFile' option is set to true with no 'logDir' option specified", function() {
const thrower = () => {
LoggerFactory.createLogger({ logToFile: true });
LogFactory.createLogger({ logToFile: true });
};

expect(thrower).to.throw("The 'logToFile' option requires a 'logDir' options to be specified");
Expand All @@ -188,7 +188,7 @@ module.exports = {

expect(fs.existsSync(filepath)).to.equal(false);

LoggerFactory.createLogger({
LogFactory.createLogger({
transports: [new winston.transports.File({ filename: filepath })]
});

Expand All @@ -202,7 +202,7 @@ module.exports = {

/*it("should let the user specify if they want to log uncaught exceptions", function(done) {
this.timeout(5000);
const logger = LoggerFactory.createLogger({ logExceptions: true });
const logger = LogFactory.createLogger({ logExceptions: true });
const { sinonSpy, restore } = spyOnConsoleOutput("stdout");
process.on("uncaughtException", function() {
Expand Down
2 changes: 1 addition & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const request = require("supertest");
const { create } = require("./framework/application");

const { StatusCodes, StatusTexts } = require("./framework/component/http");
const { chai } = require("./framework/lib/test-helper");
const { chai } = require("./lib/test-helper");

module.exports = {
routes() {
Expand Down

0 comments on commit ef2f901

Please sign in to comment.