Add Logging singleton to manually control Mediabunny's console output (fixes #415)

This commit is contained in:
Vanilagy
2026-06-18 19:32:18 +02:00
parent 92a384f418
commit cdc2ef63dd
28 changed files with 324 additions and 65 deletions
+34
View File
@@ -279,6 +279,40 @@ return new VideoSample(pixels, {
});
```
## Controlling logging
Especially in command-line applications you usually don't want Mediabunny interfering with your stdout and stderr output. Mediabunny provides ways to control its console output:
```ts
import { Logging, LogLevel } from 'mediabunny';
// The default: Mediabunny can log errors, warnings, and information messages.
Logging.level = LogLevel.Info;
// Only log warnings and errors.
Logging.level = LogLevel.Warnings;
// Only log errors.
Logging.level = LogLevel.Errors;
// Don't log anything at all.
Logging.level = LogLevel.None;
```
You can also hook into log events:
```ts
Logging.on('error', (args: unknown[]) => {
// Handle error message
});
Logging.on('warn', (args: unknown[]) => {
// Handle warning message
});
Logging.on('info', (args: unknown[]) => {
// Handle info message
});
```
## Implementation details
`@mediabunny/server` uses [NodeAV](https://github.com/seydx/node-av) under the hood which provides N-API C bindings to FFmpeg's C API. Using NodeAV, this package implements [custom decoders and encoders](https://mediabunny.dev/guide/supported-formats-and-codecs#custom-coders) by directly using the APIs provided by `libavcodec`.
+2 -1
View File
@@ -8,6 +8,7 @@
import {
AudioSample,
Logging,
MaybePromise,
registerDecoder,
registerEncoder,
@@ -24,7 +25,7 @@ import { copyAudioSampleToAvFrame, AvFrameAudioSampleResource } from './audio-sa
const SERVER_LOADED_SYMBOL = Symbol.for('@mediabunny/server loaded');
if ((globalThis as Record<symbol, unknown>)[SERVER_LOADED_SYMBOL]) {
console.error(
Logging._error(
'[WARNING]\n@mediabunny/server was loaded twice.'
+ ' This will likely cause the package not to work correctly.'
+ ' Check if multiple dependencies are importing different versions of @mediabunny/server,'