Skip to content

Commit

Permalink
Adds constructor option displayName: Boolean
Browse files Browse the repository at this point in the history
Adds constructor option `name: String`
Adds method `Logger.getName()`
Adds unit tests for Logger.name
Updates README and CHANGELOG
  • Loading branch information
jalik committed Apr 2, 2018
1 parent c2d9ac6 commit 2e82062
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.0.4
- Adds constructor option `displayName: Boolean`
- Adds constructor option `name: String`
- Adds method `Logger.getName()`

## v1.0.3
- Exports `Logger` using ES6 default export

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const Logger = new Logger({
warning: true
},
// Display context in the console
displayContext: true
displayContext: false,
// Display logger name in the console
displayMessage: false,
// Give a name to this logger
name: 'main'
});
```

Expand Down
56 changes: 42 additions & 14 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Types from './types';

class Logger {
constructor(options) {
// Set default options
this.options = extendRecursively({
active: true,
console: {
Expand All @@ -39,6 +40,8 @@ class Logger {
warning: true,
},
displayContext: false,
displayName: false,
name: null,
}, options);

// Create observer
Expand All @@ -50,29 +53,37 @@ class Logger {
}

// Add polyfill methods to the console object
// eslint-disable-next-line
// eslint-disable-next-line no-console
if (typeof console.log === 'function') {
// eslint-disable-next-line
// eslint-disable-next-line no-console
if (typeof console.debug !== 'function') {
// eslint-disable-next-line
// eslint-disable-next-line no-console
console.debug = console.log;
}
// eslint-disable-next-line
// eslint-disable-next-line no-console
if (typeof console.error !== 'function') {
// eslint-disable-next-line
// eslint-disable-next-line no-console
console.error = console.log;
}
// eslint-disable-next-line
// eslint-disable-next-line no-console
if (typeof console.info !== 'function') {
// eslint-disable-next-line
// eslint-disable-next-line no-console
console.info = console.log;
}
// eslint-disable-next-line
// eslint-disable-next-line no-console
if (typeof console.warn !== 'function') {
// eslint-disable-next-line
// eslint-disable-next-line no-console
console.warn = console.log;
}
}

// Check logger name
if (typeof this.options.name === 'undefined' || this.options.name === null) {
// Generate a name
this.name = `logger-${Date.now()}`;
} else {
this.name = this.options.name;
}
}

/**
Expand All @@ -90,29 +101,33 @@ class Logger {
* @param context
*/
error(messageOrError, context) {
// eslint-disable-next-line
const ctx = context || {};
let msg = messageOrError;

if (messageOrError instanceof Error) {
// eslint-disable-next-line
ctx.error = {};

const attributes = ['name', 'message', 'reason', 'stack', 'type'];

for (let i = 0; i < attributes.length; i += 1) {
if (attributes[i] in messageOrError) {
// eslint-disable-next-line
ctx.error[attributes[i]] = messageOrError[attributes[i]];
}
}
// eslint-disable-next-line
const { message } = messageOrError;
msg = message;
}
return this.log(msg, Types.error, ctx);
}

/**
* Returns the logger name
* @return {string|null}
*/
getName() {
return this.name;
}

/**
* Logs an information message
* @param message
Expand All @@ -138,7 +153,15 @@ class Logger {
*/
log(message, type, context) {
if (this.isActive()) {
const args = [message];
const args = [];

// Display logger name in console
if (this.options.displayName === true) {
args.push(this.name);
}

// Display message in console
args.push(message);

// Display context in console
if (typeof context !== 'undefined' && this.options.displayContext === true) {
Expand All @@ -149,24 +172,28 @@ class Logger {
switch (type) {
case Types.debug:
if (this.options.console.debug === true) {
// eslint-disable-next-line no-console
console.log.apply(this, args);
}
break;

case Types.error:
if (this.options.console.error === true) {
// eslint-disable-next-line no-console
console.error.apply(this, args);
}
break;

case Types.info:
if (this.options.console.info === true) {
// eslint-disable-next-line no-console
console.info.apply(this, args);
}
break;

case Types.warning:
if (this.options.console.warning === true) {
// eslint-disable-next-line no-console
console.warn.apply(this, args);
}
break;
Expand All @@ -176,6 +203,7 @@ class Logger {
&& this.options.console[type] === true)
|| (typeof this.options.console[type] !== 'boolean'
&& this.options.console.other === true)) {
// eslint-disable-next-line no-console
console.log.apply(this, args);
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ describe('Logger', () => {
});
});

describe('new Logger({name: "main"})', () => {
it('should create a logger with the name "main"', () => {
const logger = new Logger({ name: 'main' });
expect(logger.getName()).toEqual('main');
});
});

describe('new Logger({name: null})', () => {
it('should create a logger with a generated name', () => {
const logger = new Logger({ name: null });
expect(typeof logger.getName()).toEqual('string');
expect(logger.getName().length > 0).toEqual(true);
});
});

describe('new Logger({active: true})', () => {
it('should create an active logger', () => {
const logger = new Logger({ active: true });
Expand Down

0 comments on commit 2e82062

Please sign in to comment.