Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support different loggers #224

Closed
wants to merge 4 commits into from
Closed

Conversation

lundibundi
Copy link
Member

@lundibundi lundibundi commented Jun 17, 2017

Enables users to specify logger they want to use with our library. Also adds simple proxy to console.

As discussed in #102 I extract logger fuctionality to a different PR.

Also I'd like to add a few tests for console logger, but I don't know how to better implement it, as I don't think that either redirecting console output during the test or just checking stdout is a good solution, can someone suggest a solution?

lib/logger.js Outdated
const set = l => logger = l;
const get = () => logger;

const isDebug = () => logger && logger.debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find these names misleading, something like hasDebug etc would be better, IMO. I'd expect isDebug, isVerbose and alike to report the current logging level, which is not the case with these checks because these methods will be present in virtually every logger regardless of the logging level.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the actual reason I named them as such, I was mislead by issue in winston that actually suggested to check level this way.
I haven't found a good way to get debug level out of winston beside .level which gives a string, that's disappointing. Lucky enough bunyan returns a number which is a lot better. I guess we will allow user to provide a callback to check current log level and I'll write proxies for winston and bunyan to simplify their usage.

'Run `npm install` in order to build it, otherwise you will get ' +
'poor performance.'
);
if (logger.isWarn()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't possible to replace the logger before this statement will be executed, so it will always use the default logger.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aqrln yeah, missed that. Should we set console as default logger or always log to console here?

@lundibundi lundibundi force-pushed the support-different-loggers branch from 2f5aa8f to 2f5b62d Compare June 22, 2017 10:58
@nechaido
Copy link
Member

I'd like the logger to have method's like: warn, error, log, debug. Logger should decide for itself to log something or not, that way we'll have no need to write this kind of statements:
if (logger.isDebug()) logPacket(packet);.

@lundibundi
Copy link
Member Author

@nechaido It already has those methods and decides by itself whether he should log or drop the message. The need in methods like isDebug() and such arises from the cost that preparation for logging has, for example in this PR I have to check jstp message type and copy if necessary while removing some fields to avoid leak of private data to logs (for handshake packet). Checking isDebug() is much cheaper than that. And also I don't think that implementing callback to check current logging level is that hard.

@nechaido
Copy link
Member

@lundibundi this could be achieved by passing a function to logger which will do the preparation and then return the string to be logged.

@belochub
Copy link
Member

belochub commented Jun 23, 2017

@lundibundi, can't you make a wrapper (adapter) in our library which will check the logger level before the preparation, to avoid boilerplate checking like the one @nechaido mentioned?

@lundibundi
Copy link
Member Author

lundibundi commented Jun 23, 2017

@belochub I can do that but I see no reason for that complexity, whether we write
if (logger.isDebug()) logPacket(packet); or something like
logger.log('debug', logPacket.bind(null, packet))
More so the latter will probably be less efficient and takes more work to implement and support.

@nechaido
Copy link
Member

@lundibundi and what about:

logger.debug(() => preparePacket(packet));

Looks ok for me and doesn't do any unnecessary preparation in release mod, only call to empty function.

Forgot about creating a function to be passed.

@belochub
Copy link
Member

@lundibundi, what you said in #224 (comment) wasn't at all what I proposed to do.

@belochub
Copy link
Member

@lundibundi, what I proposed will make

if (logger.isDebug()) logPacket(packet);

look like

logger.get().debug(packet);

or even

logger.debug(packet);

@lundibundi
Copy link
Member Author

@belochub oh, sorry, I thought that you extended @nechaido idea. So you want a logger specifically to log jstp messages, that will check log level and if object is passed treat it as jstp message and log it same way logPacket does and if string is passed log it as is, is my understanding correct?

@belochub
Copy link
Member

@lundibundi, yeah, something like that.

@lundibundi
Copy link
Member Author

@belochub but its only usage are those 3 calls in connection.js and I'm not sure if it is plausible to implement it solely for those usages? Also I can move isDebug check into logPacket so there'll be only one check for isDebug() (in code), they only reason I left it outside was to avoid unnecessary call to logPacket.

@aqrln
Copy link
Member

aqrln commented Jul 7, 2017

@lundibundi ping

@aqrln aqrln changed the title Support different loggers lib: support different loggers Jul 7, 2017
@lundibundi
Copy link
Member Author

@aqrln I'm waiting for the decision of our team, whether this is a good solution or not. We even got the slack vote which has apparently been flooded by github bot.

@belochub
Copy link
Member

Closed in favor of #252 (see #250 for more information).

@belochub belochub closed this Jul 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants