Skip to content

Commit f6a66e6

Browse files
misteroneillgkatsev
authored andcommitted
feat: Add a default, plugin-specific logger to advanced plugins (#6693)
1 parent fdd807b commit f6a66e6

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

docs/guides/plugins.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ console.log(version); // 1.0.1
244244

245245
Note that the [plugin generator](https://github.com/videojs/generator-videojs-plugin) already takes care of adding a version number for you.
246246

247+
#### Logging
248+
249+
By default, each advanced plugin instance has its own `log` property much like `videojs` and `Player` instances do. The log messages will be prefixed with the player's ID and the plugin's name:
250+
251+
```js
252+
player.examplePlugin().log('hello world!');
253+
```
254+
255+
The above will log the following:
256+
257+
VIDEOJS: $PLAYER_ID: examplePlugin: hello world!
258+
259+
The `log` function will also have all the methods/properties of the default `videojs.log`; such as, `error()`, `warn()`, `level()`, etc.
260+
261+
> **NOTE:** This method is added in the constructor and it _will not_ override any predefined `log` property of the plugin's prototype.
262+
247263
### Advanced Example Advanced Plugin
248264

249265
What follows is a complete ES6 advanced plugin that logs a custom message when the player's state changes between playing and pause. It uses all the described advanced features:

src/js/plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ class Plugin {
203203

204204
this.player = player;
205205

206+
if (!this.log) {
207+
this.log = this.player.log.createLogger(this.name);
208+
}
209+
206210
// Make this object evented, but remove the added `trigger` method so we
207211
// use the prototype version instead.
208212
evented(this);

test/unit/plugin-advanced.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ QUnit.test('setup', function(assert) {
7171
);
7272
});
7373

74+
QUnit.test('log is added by default', function(assert) {
75+
const instance = this.player.mock();
76+
77+
assert.strictEqual(typeof instance.log, 'function', 'log is a function');
78+
assert.strictEqual(typeof instance.log.debug, 'function', 'log.debug is a function');
79+
assert.strictEqual(typeof instance.log.error, 'function', 'log.error is a function');
80+
assert.strictEqual(typeof instance.log.history, 'function', 'log.history is a function');
81+
assert.strictEqual(typeof instance.log.levels, 'object', 'log.levels is a object');
82+
assert.strictEqual(typeof instance.log.warn, 'function', 'log.warn is a function');
83+
});
84+
85+
QUnit.test('log will not clobber pre-existing log property', function(assert) {
86+
class MockLogPlugin extends Plugin {
87+
log() {}
88+
}
89+
90+
MockLogPlugin.VERSION = '1.0.0';
91+
Plugin.registerPlugin('mockLog', MockLogPlugin);
92+
93+
const instance = this.player.mockLog();
94+
95+
assert.strictEqual(typeof instance.log, 'function', 'log is a function');
96+
assert.strictEqual(instance.log, MockLogPlugin.prototype.log, 'log was not overridden');
97+
});
98+
7499
QUnit.test('all "pluginsetup" events', function(assert) {
75100
const setupSpy = sinon.spy();
76101
const events = [

0 commit comments

Comments
 (0)