Skip to content

Commit

Permalink
fix(Handlers): Only add default handler if no other
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 3, 2019
1 parent aa7c56d commit a462fc5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
getLogger,
addHandler,
getLogger,
LogData,
LogLevel,
removeHandler,
removeAllHandlers,
removeHandlers,
replaceHandlers
} from './index'

Expand Down Expand Up @@ -98,7 +98,7 @@ test('adding and removing handlers', () => {
log.info('')
expect(events.length).toBe(8)

removeAllHandlers()
removeHandlers()
log.info('')
expect(events.length).toBe(8)

Expand Down
9 changes: 5 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function removeHandler(handler?: LogHandler) {
/**
* Remove all handlers.
*/
export function removeAllHandlers() {
export function removeHandlers() {
process.removeAllListeners(LOG_EVENT_NAME)
}

Expand All @@ -97,7 +97,7 @@ export function removeAllHandlers() {
* to the console.
*/
export function replaceHandlers(handler: LogHandler) {
removeAllHandlers()
removeHandlers()
addHandler(handler)
}

Expand Down Expand Up @@ -139,8 +139,9 @@ export function defaultHandler(data: LogData) {
console.error(entry)
}

// Always enable the default handler
addHandler(defaultHandler)
// Enable the default handler if there no other handler
// already enabled e.g. by another package using `logga`
if (!process.listenerCount(LOG_EVENT_NAME)) addHandler(defaultHandler)

/**
* Get a logger for the specific application or package.
Expand Down

0 comments on commit a462fc5

Please # to comment.