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

feat: createLogger for easier logging in individual modules #5418

Merged
merged 16 commits into from
Sep 28, 2018

Conversation

gkatsev
Copy link
Member

@gkatsev gkatsev commented Sep 7, 2018

This is the most naive implementation of transforming what we have into providing a create logger functionality.
Basically, right now, log.js is changed into a create-logger.js which is a function that given a name returns a log function. log.js now uses create-logger to createLogger('VIDEOJS') which is the default logger.
videojs.createLogger is exposed for users of the library.

Currently, logByType is also exposed directly on the log object but that's probably not wanted.
Other tweaks we can consider: a shared/central history. This will allow us to make it be in one place and we could also provide some search functionality via the "main" logger. For example, searching for all things with the name of VHS or something.

Additionally, we may want to expose createLogger on the log itself rather than on videojs and have the logs inherit each time one is created:

var log = videojs.log.createLogger('foo');
log('hello');
// > VIDEOJS: foo: hello

Copy link
Member

@misteroneill misteroneill left a comment

Choose a reason for hiding this comment

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

I've wanted this for a while.

@misteroneill
Copy link
Member

Currently, logByType is also exposed directly on the log object but that's probably not wanted.

Agree. I think it should be a candidate for deprecation (maybe not in this PR).

shared/central history. This will allow us to make it be in one place and we could also provide some search functionality via the "main" logger. For example, searching for all things with the name of VHS or something.

Love this idea.

The only concern I have with logging history is the potential for memory leaks. For example, one might log a player object, which would immediately create a potential memory leak without them knowing.

A history refactor could be involve solutions to that problem.

  • It could keep a history with only primitive values.
  • It could remove references to players in the history when they are disposed (could be a performance concern, iterating over the whole history, though).

Additionally, we may want to expose createLogger on the log itself rather than on videojs and have the logs inherit each time one is created.

I like the idea of inheritance. The prefixes generate a kind of path allowing using to more easily filter the console.

Player-Scoped Logger

Another useful thing would be player-specific loggers:

player.log('hello');

// > VIDEOJS: ${playerId}: hello

These could then be disposed with the player making some of the memory concerns with history a bit cleaner.

@gkatsev
Copy link
Member Author

gkatsev commented Sep 7, 2018

We should just consider turning off history by default. Maybe even as part of this PR, I'm not sure that anyone is really using it and yeah, could be a memory issue.

Having a player-scoped logger is a cool idea.

@gkatsev
Copy link
Member Author

gkatsev commented Sep 7, 2018

Looks like the only logByType that's used outside is in a test and that can be updated. Then I can just remove it from being exposed publicly.

@gkatsev
Copy link
Member Author

gkatsev commented Sep 7, 2018

logByType has been made module private and the tests were updated for it as well.

Next is shared history, chainable loggers, and player log.

@gkatsev
Copy link
Member Author

gkatsev commented Sep 7, 2018

Adding a player.log was surprisingly easy.
Going to work on refactoring to a shared history but also probably making a Log class instead.

@gkatsev
Copy link
Member Author

gkatsev commented Sep 8, 2018

Added log.history.filter() which takes in a "name" (the one passed to createLogger) and filters by it.

@gkatsev gkatsev changed the title [WIP] feat: createLogger for easier logging in individual modules feat: createLogger for easier logging in individual modules Sep 10, 2018
@gkatsev
Copy link
Member Author

gkatsev commented Sep 10, 2018

Not sure why travis isn't showing up but the two builds have succeeded there: #9475, #9746

@@ -44,6 +49,23 @@ Similar to the `console`, any number of mixed-type values can be passed to `vide
videojs.log('this is a string', {butThis: 'is an object'});
```

### Creating new Loggers

Sometimes, you want to make a new module or plugin and log messages with a label. Kind of how all these logs are prepended with `VIDEOJS:`. You can do that via the `createLog` method. It takes a name and gives you back a log object like `videojs.log`. Here's an example:
Copy link
Member

Choose a reason for hiding this comment

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

createLog should be createLogger


// Bail out if there's no console or if this type is not allowed by the
// current logging level.
if (!fn || !lvl || !lvlRegExp.test(type)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall which versions it is, but there are some IEs that don't allow using apply/call on console methods. Probably not the case in IE11, but might be worth checking.

Copy link
Member Author

Choose a reason for hiding this comment

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

This code wasn't changed from what was around previously.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, then, it's probably safe. I could be showing my age again. 😄

@gkatsev gkatsev merged commit 966eb56 into master Sep 28, 2018
@gkatsev gkatsev deleted the create-logger branch September 28, 2018 20:53
gkatsev added a commit that referenced this pull request Oct 31, 2018
Create a new createLogger module for better debugging. Each logger has its own log level and its own createLogger that will nest logs underneath them. `player.log` is also included, which logs the player id as part of the log statement. The history API also got a filter method.

For example:
```js
var log = videojs.log.createLogger('foo');
log('hello');
// > VIDEOJS: foo: hello
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants