Skip to content

Commit ff11e26

Browse files
committed
Clean up logging for Now Playing
1 parent f004aa4 commit ff11e26

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/backend/scrobblers/AbstractScrobbleClient.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default abstract class AbstractScrobbleClient extends AbstractComponent i
9595
nowPlayingLastPlay?: PlayObject;
9696
nowPlayingQueue: NowPlayingQueue = new Map();
9797
nowPlayingTaskInterval: number = 5000;
98+
npLogger: Logger;
9899

99100
declare config: CommonClientConfig;
100101

@@ -106,6 +107,7 @@ export default abstract class AbstractScrobbleClient extends AbstractComponent i
106107
this.type = type;
107108
this.name = name;
108109
this.logger = childLogger(logger, this.getIdentifier());
110+
this.npLogger = childLogger(this.logger, 'Now Playing');
109111
this.notifier = notifier;
110112
this.emitter = emitter;
111113

@@ -186,20 +188,20 @@ export default abstract class AbstractScrobbleClient extends AbstractComponent i
186188
if('nowPlaying' in options) {
187189
const nowOpts = options as NowPlayingOptions;
188190
this.nowPlayingEnabled = nowOpts.nowPlaying === true || Array.isArray(nowOpts.nowPlaying);
189-
this.logger.debug({labels: ['Now Playing']}, `${this.nowPlayingEnabled ? 'Enabled' : 'Disabled'} by 'nowPlaying' config`);
191+
this.npLogger.debug(`${this.nowPlayingEnabled ? 'Enabled' : 'Disabled'} by 'nowPlaying' config`);
190192
} else if (npEnv !== undefined) {
191193
this.nowPlayingEnabled = parseBool(npEnv);
192-
this.logger.debug({labels: ['Now Playing']}, `${this.nowPlayingEnabled ? 'Enabled' : 'Disabled'} by global ENV`);
194+
this.npLogger.debug(`${this.nowPlayingEnabled ? 'Enabled' : 'Disabled'} by global ENV`);
193195
} else {
194196
this.nowPlayingEnabled = true;
195-
this.logger.debug({labels: ['Now Playing']}, `Enabled by default config`);
197+
this.npLogger.debug(`Enabled by default config`);
196198
}
197199
}
198200

199201
this.initializeNowPlayingFilter();
200202
this.initializeNowPlayingSchedule();
201203
} else {
202-
this.logger.debug({labels: ['Now Playing']}, 'Unsupported feature, disabled.');
204+
this.npLogger.debug('Unsupported feature, disabled.');
203205
}
204206
}
205207

@@ -208,7 +210,7 @@ export default abstract class AbstractScrobbleClient extends AbstractComponent i
208210
const t = new AsyncTask('Playing Now', (): Promise<any> => {
209211
return this.processingPlayingNow();
210212
}, (err: Error) => {
211-
this.logger.error(new Error('Unexpected error while processing Now Playing queue', {cause: err}));
213+
this.npLogger.error(new Error('Unexpected error while processing Now Playing queue', {cause: err}));
212214
});
213215

214216
this.scheduler.removeById('pn_task');
@@ -921,7 +923,7 @@ ${closestMatch.breakdowns.join('\n')}`, {leaf: ['Dupe Check']});
921923
queuePlayingNow = (data: PlayObject, source: SourceIdentifier) => {
922924
const sourceId = `${source.name}-${source.type}`;
923925
if(isDebugMode()) {
924-
this.logger.debug(`Queueing ${buildTrackString(data, {include: ['artist', 'track', 'platform']})} from ${sourceId}`);
926+
this.npLogger.debug(`Queueing ${buildTrackString(data, {include: ['artist', 'track', 'platform']})} from ${sourceId}`);
925927
}
926928
const platformPlays = this.nowPlayingQueue.get(sourceId) ?? new Map();
927929
platformPlays.set(genGroupIdStrFromPlay(data), {play: data, source});
@@ -937,10 +939,10 @@ ${closestMatch.breakdowns.join('\n')}`, {leaf: ['Dupe Check']});
937939
if(this.shouldUpdatePlayingNow(play)) {
938940
try {
939941
await this.doPlayingNow(play);
940-
this.logger.debug(`Now Playing updated.`);
942+
this.npLogger.debug(`Now Playing updated.`);
941943
this.emitEvent('nowPlayingUpdated', play);
942944
} catch (e) {
943-
this.logger.warn(new Error('Error occurred while trying to update Now Playing, will ignore', {cause: e}));
945+
this.npLogger.warn(new Error('Error occurred while trying to update upstream Client, will ignore', {cause: e}));
944946
}
945947
this.nowPlayingLastPlay = play;
946948
this.nowPlayingLastUpdated = dayjs();
@@ -953,7 +955,9 @@ ${closestMatch.breakdowns.join('\n')}`, {leaf: ['Dupe Check']});
953955

954956
shouldUpdatePlayingNow = (data: PlayObject): boolean => {
955957
if(this.nowPlayingLastPlay === undefined || this.nowPlayingLastUpdated === undefined) {
956-
this.logger.debug(`Now Playing has not yet been set! Should update Now Playing`);
958+
if(isDebugMode()) {
959+
this.npLogger.debug(`Now Playing has not yet been set! Should update`);
960+
}
957961
return true;
958962
}
959963

@@ -962,18 +966,22 @@ ${closestMatch.breakdowns.join('\n')}`, {leaf: ['Dupe Check']});
962966
// update if play *has* changed and time since last update is greater than min interval
963967
// this prevents spamming scrobbler API with updates if user is skipping tracks and source updates frequently
964968
if(!playObjDataMatch(data, this.nowPlayingLastPlay) && this.nowPlayingThresholds[0] < lastUpdateDiff) {
965-
this.logger.debug(`New Play differs from previous Now Playing and time since update > ${lastUpdateDiff}s, should update Now Playing`);
969+
if(isDebugMode()) {
970+
this.npLogger.debug(`New Play differs from previous Now Playing and time since update > ${lastUpdateDiff}s, should update`);
971+
}
966972
return true;
967973
}
968974
// update if play *has not* changed but last update is greater than max interval
969975
// this keeps scrobbler Now Playing fresh ("active" indicator) in the event play is long
970976
if(playObjDataMatch(data, this.nowPlayingLastPlay) && this.nowPlayingThresholds[1] < lastUpdateDiff) {
971-
this.logger.debug(`Now Playing has not been updated in > ${lastUpdateDiff}s, should update Now Playing`);
977+
if(isDebugMode()) {
978+
this.npLogger.debug(`Now Playing has not been updated in > ${lastUpdateDiff}s, should update`);
979+
}
972980
return true;
973981
}
974982

975983
if(isDebugMode()) {
976-
this.logger.debug(`Updated Now Playing ${playObjDataMatch(data, this.nowPlayingLastPlay) ? 'matches' : 'does not match'} and was last updated ${lastUpdateDiff}s ago, not updating Now Playing`);
984+
this.npLogger.debug(`Updated Now Playing ${playObjDataMatch(data, this.nowPlayingLastPlay) ? 'matches' : 'does not match'} and was last updated ${lastUpdateDiff}s ago, not updating`);
977985
}
978986
return false;
979987
}

0 commit comments

Comments
 (0)