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

[Proposal] add LogFormat static property #1469

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/api/Static_Properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ import RxPlayer from "rx-player";
RxPlayer.LogLevel = "WARNING";
```

## LogFormat

Allows to configure the format log messages will have.

Can be set to either:

- `"standard"`: Regular log messages will be printed, this is the default format.

- `"full"`: Log messages will be enriched with timestamps and namespaces, which allows
them to be easier to programatically exploit.

You may for example prefer that format when reporting issues to the RxPlayer's
maintainers, so we are able to extract more information from those logs.

### Example

```js
import RxPlayer from "rx-player";
RxPlayer.LogFormat = "full";
```

## ErrorTypes

The different "types" of Error you can get on playback error,
Expand Down
3 changes: 3 additions & 0 deletions doc/reference/API_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ events and so on.
- [`LogLevel`](../api/Static_Properties.md#loglevel): Update the verbosity of the RxPlayer
logger.

- [`LogFormat`](../api/Static_Properties.md#logformat): Update the format of logs produced
by the logger.

- [`ErrorTypes`](../api/Static_Properties.md#errortypes): All Error types that can be
encountered.

Expand Down
26 changes: 20 additions & 6 deletions src/core/main/worker/worker_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { IPlayerError, ITrackType } from "../../../public_types";
import createDashPipelines from "../../../transports/dash";
import arrayFind from "../../../utils/array_find";
import assert, { assertUnreachable } from "../../../utils/assert";
import type { ILoggerLevel } from "../../../utils/logger";
import type { ILogFormat, ILoggerLevel } from "../../../utils/logger";
import { scaleTimestamp } from "../../../utils/monotonic_timestamp";
import objectAssign from "../../../utils/object_assign";
import type { IReadOnlySharedReference } from "../../../utils/reference";
Expand Down Expand Up @@ -91,7 +91,11 @@ export default function initializeWorkerMain() {
assert(!isInitialized);
isInitialized = true;
scaleTimestamp(msg.value);
updateLoggerLevel(msg.value.logLevel, msg.value.sendBackLogs);
updateLoggerLevel(
msg.value.logLevel,
msg.value.logFormat,
msg.value.sendBackLogs,
);
if (msg.value.dashWasmUrl !== undefined && dashWasmParser.isCompatible()) {
dashWasmParser.initialize({ wasmUrl: msg.value.dashWasmUrl }).catch((err) => {
const error = err instanceof Error ? err.toString() : "Unknown Error";
Expand All @@ -115,7 +119,11 @@ export default function initializeWorkerMain() {
break;

case MainThreadMessageType.LogLevelUpdate:
updateLoggerLevel(msg.value.logLevel, msg.value.sendBackLogs);
updateLoggerLevel(
msg.value.logLevel,
msg.value.logFormat,
msg.value.sendBackLogs,
);
break;

case MainThreadMessageType.PrepareContent:
Expand Down Expand Up @@ -883,11 +891,17 @@ function loadOrReloadPreparedContent(
}
}

function updateLoggerLevel(logLevel: ILoggerLevel, sendBackLogs: boolean): void {
function updateLoggerLevel(
logLevel: ILoggerLevel,
logFormat: ILogFormat,
sendBackLogs: boolean,
): void {
if (!sendBackLogs) {
log.setLevel(logLevel);
log.setLevel(logLevel, logFormat);
} else {
log.setLevel(logLevel, (levelStr, logs) => {
// Here we force the log format to "standard" as the full formatting will be
// performed on main thread.
log.setLevel(logLevel, "standard", (levelStr, logs) => {
const sentLogs = logs.map((e) => {
if (e instanceof Error) {
return formatErrorForSender(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const mediaCapabilitiesProber = {
* @param {string} level
*/
set LogLevel(level: string) {
log.setLevel(level);
log.setLevel(level, log.getFormat());
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ Player.addFeatures([
HTML_SRT_PARSER,
]);
if (isDebugModeEnabled()) {
logger.setLevel("DEBUG");
logger.setLevel("DEBUG", "standard");
} else if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL);
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL, "standard");
}
export default Player;

Expand Down
22 changes: 19 additions & 3 deletions src/main_thread/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,21 @@ class Player extends EventEmitter<IPublicAPIEvent> {
return log.getLevel();
}
static set LogLevel(logLevel: string) {
log.setLevel(logLevel);
log.setLevel(logLevel, log.getFormat());
}

/**
* Current log format.
* Should be either (by verbosity ascending):
* - "standard": Regular log messages.
* - "full": More verbose format, including a timestamp and a namespace.
* Any other value will be translated to "standard".
*/
static get LogFormat(): string {
return log.getFormat();
}
static set LogFormat(format: string) {
log.setLevel(log.getLevel(), format);
}

/**
Expand Down Expand Up @@ -531,6 +545,7 @@ class Player extends EventEmitter<IPublicAPIEvent> {
value: {
dashWasmUrl: workerSettings.dashWasmUrl,
logLevel: log.getLevel(),
logFormat: log.getFormat(),
sendBackLogs: isDebugModeEnabled(),
date: Date.now(),
timestamp: getMonotonicTimeStamp(),
Expand All @@ -540,15 +555,16 @@ class Player extends EventEmitter<IPublicAPIEvent> {
});
log.addEventListener(
"onLogLevelChange",
(logLevel) => {
(logInfo) => {
if (this._priv_worker === null) {
return;
}
log.debug("---> Sending To Worker:", MainThreadMessageType.LogLevelUpdate);
this._priv_worker.postMessage({
type: MainThreadMessageType.LogLevelUpdate,
value: {
logLevel,
logLevel: logInfo.level,
logFormat: logInfo.format,
sendBackLogs: isDebugModeEnabled(),
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ patchWebkitSourceBuffer();
features.codecSupportProber = MainCodecSupportProber;

if (isDebugModeEnabled()) {
logger.setLevel("DEBUG");
logger.setLevel("DEBUG", "standard");
} else if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL);
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL, "standard");
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/multithread_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type {
import type { IFreezingStatus, IRebufferingStatus } from "./playback_observer";
import type { ITrackType } from "./public_types";
import type { ITransportOptions } from "./transports";
import type { ILoggerLevel } from "./utils/logger";
import type { ILogFormat, ILoggerLevel } from "./utils/logger";
import type { IRange } from "./utils/ranges";

/**
Expand Down Expand Up @@ -65,6 +65,12 @@ export interface IInitMessage {
hasMseInWorker: boolean;
/** Initial logging level that should be set. */
logLevel: ILoggerLevel;
/** Intitial logger's log format that should be set. */
logFormat: ILogFormat;
/**
* If `true`, logs should be sent back to the main thread, through a
* `ILogMessageWorkerMessage` message.
*/
sendBackLogs: boolean;
/**
* Value of `Date.now()` at the time the `timestamp` property was generated.
Expand Down Expand Up @@ -138,6 +144,12 @@ export interface ILogLevelUpdateMessage {
value: {
/** The new logger level that should be set. */
logLevel: ILoggerLevel;
/** Intitial logger's log format that should be set. */
logFormat: ILogFormat;
/**
* If `true`, logs should be sent back to the main thread, through a
* `ILogMessageWorkerMessage` message.
*/
sendBackLogs: boolean;
};
}
Expand Down
Loading
Loading