Skip to content

Commit

Permalink
Now providing Enapso Logger Factory for easy logger management, also …
Browse files Browse the repository at this point in the history
…global.
  • Loading branch information
alexander-schulze committed Feb 6, 2020
1 parent 06173c2 commit a1ba088
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
32 changes: 16 additions & 16 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// Innotrade Enapso Logger
// (C) Copyright 2019 Innotrade GmbH, Herzogenrath, NRW, Germany
// (C) Copyright 2019-2020 Innotrade GmbH, Herzogenrath, NRW, Germany
// Author: Alexander Schulze

// a new console object for enhanced logging to the console

const { EnapsoLogger } = require('../index');
global.enlogger = new EnapsoLogger();
enlogger.setLevel(EnapsoLogger.ALL);
const { EnapsoLogger, EnapsoLoggerFactory } = require('../index');
EnapsoLoggerFactory.createGlobalLogger('enLogger');
enLogger.setLevel(EnapsoLogger.ALL);

function demo() {
enlogger.setLevel(EnapsoLogger.ALL);
enLogger.setLevel(EnapsoLogger.ALL);

enlogger.log("This is a standard log line, just for compatibility reasons");
enlogger.debug("This is a debug message");
enlogger.info("This is an information");
enlogger.warn("This is a warning");
enlogger.error("This is an error message");
enlogger.fatal("This is a fatal message");
enLogger.log('This is a standard log line, just for compatibility reasons');
enLogger.debug('This is a debug message');
enLogger.info('This is an information');
enLogger.warn('This is a warning');
enLogger.error('This is an error message');
enLogger.fatal('This is a fatal message');

enlogger.info(enlogger.separatorLine());
enlogger.setActive(false);
enlogger.info("This message will NOT be shown");
enlogger.setActive(true);
enlogger.info("This message will be shown again");
enLogger.info(enLogger.separatorLine());
enLogger.setActive(false);
enLogger.info('This message will NOT be shown');
enLogger.setActive(true);
enLogger.info('This message will be shown again');
}

demo();
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

// a new console object for enhanced logging to the console

const { EnapsoLogger } = require('./lib/enapso-logger');
const { EnapsoLogger, EnapsoLoggerFactory, enlogger } = require('./lib/enapso-logger');

module.exports = {
EnapsoLogger
EnapsoLogger, EnapsoLoggerFactory,
enlogger
}
35 changes: 34 additions & 1 deletion lib/enapso-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,39 @@ class EnapsoLogger {

}

class EnapsoLoggerFactory {

static logger = {};

static getLogger(alias) {
alias = alias || '$default';
let logger = this.logger[alias];
if (logger) return logger;
logger = new EnapsoLogger();
this.logger[alias] = logger;
return logger;
}

static createGlobalLogger(alias) {
if (!alias) {
throw "No alias for global logger!";
}
let logger = global[alias];
if (logger) return logger;
logger = new EnapsoLogger();
global[alias] = logger;
return logger;
}

}

const enLoggerFactory = new EnapsoLoggerFactory();

module.exports = {
EnapsoLogger
// without name space
EnapsoLogger, EnapsoLoggerFactory,
// with namespace
enlogger: {
EnapsoLogger, EnapsoLoggerFactory
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@innotrade/enapso-logger",
"version": "1.0.0",
"version": "1.1.0",
"description": "Enapso Logger",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a1ba088

Please sign in to comment.