Skip to content

Commit

Permalink
optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Cap32 committed Aug 3, 2017
1 parent ed8d1a7 commit 2f196b8
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const config = {
};

const nativeConsole = {};
const SymbolEnsureLatest = Symbol('EnsureLatest');

const getLevel = (key, level, defaultLevel) =>
(isObject(level) ? level[key] : level) || defaultLevel
Expand Down Expand Up @@ -168,33 +169,42 @@ const logSystem = (function () {
shouldReload = true;
},
reload() {
if (shouldReload) {
performUpdateDaemon();
performUpdateAppenders();
performUpdateCategories();
performReloadConfigure();
shouldReload = false;
}
performUpdateDaemon();
performUpdateAppenders();
performUpdateCategories();
performReloadConfigure();
shouldReload = false;
},
hasLogger(category) {
return !!loggers[category];
},
getLogger(category = defaultCategory) {
if (loggers[category]) { return loggers[category]; }

const init = () => {
this.reload();
return log4js.getLogger(category);
let origin = null;
let cache = {};

const ensureLatest = () => {
if (this.shouldReload) {
this.reload();
cache = {};
}

if (!origin) {
origin = log4js.getLogger(category);
}

return origin;
};

// TODO: should cache result
const reflect = (name) => {
const logger = init();
return logger[name].bind(logger);
ensureLatest();
if (cache[name]) { return cache[name]; }
return (cache[name] = origin[name].bind(origin));
};

return (loggers[category] = {
init,
[SymbolEnsureLatest]: ensureLatest,
get trace() { return reflect('trace'); },
get debug() { return reflect('debug'); },
get info() { return reflect('info'); },
Expand Down Expand Up @@ -267,7 +277,7 @@ export function setConfig(maybeKey, maybeVal) {
}

export function overrideConsole(logger = logSystem.getLogger(), filter) {
logger = logger.init ? logger.init() : logger;
logger = logger[SymbolEnsureLatest] ? logger[SymbolEnsureLatest]() : logger;

const createReflection = (method) => {
return (...args) => {
Expand Down

0 comments on commit 2f196b8

Please sign in to comment.