Skip to content

πŸŽ₯ Make videos programmatically with React

log levels

Jonny Burgerd73e3c013fff
diff --git a/packages/cli/src/chalk/index.ts b/packages/cli/src/chalk/index.ts
index aafbfb00d0..8f0506f1f0 100644
--- a/packages/cli/src/chalk/index.ts
+++ b/packages/cli/src/chalk/index.ts
@@ -1,208 +1,3 @@
-const hasColor = () => {
- if (typeof process !== 'undefined') {
- return process.env.FORCE_COLOR !== '0';
- }
+import {RenderInternals} from '@remotion/renderer';
- return false;
-};
-
-type Style = {
- codes: [number, number];
- name: string;
- wrap?: (input: string, newline: boolean) => string;
-};
-
-type Method = (str: string) => string;
-
-type Colors = {
- enabled: boolean;
- visible: boolean;
- styles: Record<string, Style>;
- keys: Record<string, string[]>;
- alias?: (name: string, col: string) => void;
-};
-
-type ColorsWithMethods = Colors & {
- reset: Method;
- bold: Method;
- dim: Method;
- italic: Method;
- underline: Method;
- inverse: Method;
- hidden: Method;
- strikethrough: Method;
- black: Method;
- red: Method;
- green: Method;
- yellow: Method;
- blue: Method;
- magenta: Method;
- cyan: Method;
- white: Method;
- gray: Method;
- bgBlack: Method;
- bgRed: Method;
- bgGreen: Method;
- bgYellow: Method;
- bgBlue: Method;
- bgMagenta: Method;
- bgCyan: Method;
- bgWhite: Method;
- blackBright: Method;
- redBright: Method;
- greenBright: Method;
- yellowBright: Method;
- blueBright: Method;
- magentaBright: Method;
- cyanBright: Method;
- whiteBright: Method;
- bgBlackBright: Method;
- bgRedBright: Method;
- bgGreenBright: Method;
- bgYellowBright: Method;
- bgBlueBright: Method;
- bgMagentaBright: Method;
- bgCyanBright: Method;
- bgWhiteBright: Method;
-};
-
-export const chalk = (() => {
- const colors: Colors = {
- enabled: hasColor(),
- visible: true,
- styles: {},
- keys: {},
- };
-
- const ansi = (st: Style) => {
- const open = `\u001b[${st.codes[0]}m`;
- const close = `\u001b[${st.codes[1]}m`;
- const regex = new RegExp(`\\u001b\\[${st.codes[1]}m`, 'g');
- st.wrap = (input: string, newline: boolean) => {
- if (input.includes(close)) input = input.replace(regex, close + open);
- const output = open + input + close;
- // see https://github.com/chalk/chalk/pull/92, thanks to the
- // chalk contributors for this fix. However, we've confirmed that
- // this issue is also present in Windows terminals
- return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
- };
-
- return st;
- };
-
- const wrap = (sty: Style, input: string, newline: boolean) => {
- return sty.wrap?.(input, newline) as string;
- };
-
- const style = (input: string | null | undefined, stack: string[]) => {
- if (input === '' || input === null || input === undefined) return '';
- if (colors.enabled === false) return input;
- if (colors.visible === false) return '';
- let str = String(input);
- const nl = str.includes('\n');
- let n = stack.length;
-
- while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
- return str;
- };
-
- const define = (name: string, codes: [number, number], type: string) => {
- colors.styles[name] = ansi({name, codes});
- const keys = colors.keys[type] || (colors.keys[type] = []);
- keys.push(name);
-
- Reflect.defineProperty(colors, name, {
- configurable: true,
- enumerable: true,
- set(value) {
- colors.alias?.(name, value);
- },
- get() {
- const color = (input: string) => style(input, color.stack);
- Reflect.setPrototypeOf(color, colors);
- color.stack = this.stack ? this.stack.concat(name) : [name];
- return color;
- },
- });
- };
-
- define('reset', [0, 0], 'modifier');
- define('bold', [1, 22], 'modifier');
- define('dim', [2, 22], 'modifier');
- define('italic', [3, 23], 'modifier');
- define('underline', [4, 24], 'modifier');
- define('inverse', [7, 27], 'modifier');
- define('hidden', [8, 28], 'modifier');
- define('strikethrough', [9, 29], 'modifier');
-
- define('black', [30, 39], 'color');
- define('red', [31, 39], 'color');
- define('green', [32, 39], 'color');
- define('yellow', [33, 39], 'color');
- define('blue', [34, 39], 'color');
- define('magenta', [35, 39], 'color');
- define('cyan', [36, 39], 'color');
- define('white', [37, 39], 'color');
- define('gray', [90, 39], 'color');
- define('grey', [90, 39], 'color');
-
- define('bgBlack', [40, 49], 'bg');
- define('bgRed', [41, 49], 'bg');
- define('bgGreen', [42, 49], 'bg');
- define('bgYellow', [43, 49], 'bg');
- define('bgBlue', [44, 49], 'bg');
- define('bgMagenta', [45, 49], 'bg');
- define('bgCyan', [46, 49], 'bg');
- define('bgWhite', [47, 49], 'bg');
-
- define('blackBright', [90, 39], 'bright');
- define('redBright', [91, 39], 'bright');
- define('greenBright', [92, 39], 'bright');
- define('yellowBright', [93, 39], 'bright');
- define('blueBright', [94, 39], 'bright');
- define('magentaBright', [95, 39], 'bright');
- define('cyanBright', [96, 39], 'bright');
- define('whiteBright', [97, 39], 'bright');
-
- define('bgBlackBright', [100, 49], 'bgBright');
- define('bgRedBright', [101, 49], 'bgBright');
- define('bgGreenBright', [102, 49], 'bgBright');
- define('bgYellowBright', [103, 49], 'bgBright');
- define('bgBlueBright', [104, 49], 'bgBright');
- define('bgMagentaBright', [105, 49], 'bgBright');
- define('bgCyanBright', [106, 49], 'bgBright');
- define('bgWhiteBright', [107, 49], 'bgBright');
-
- colors.alias = (name: string, color: string) => {
- // @ts-expect-error
- const fn = colors[color];
-
- if (typeof fn !== 'function') {
- throw new TypeError(
- 'Expected alias to be the name of an existing color (string) or a function'
- );
- }
-
- if (!fn.stack) {
- Reflect.defineProperty(fn, 'name', {value: name});
- colors.styles[name] = fn;
- fn.stack = [name];
- }
-
- Reflect.defineProperty(colors, name, {
- configurable: true,
- enumerable: true,
- set(value) {
- colors.alias?.(name, value);
- },
- get() {
- const col = (input: string) => style(input, col.stack);
- Reflect.setPrototypeOf(col, colors);
- col.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
- return col;
- },
- });
- };
-
- return colors as ColorsWithMethods;
-})();
+export const {chalk} = RenderInternals;
diff --git a/packages/cli/src/log.ts b/packages/cli/src/log.ts
index d2a45937a0..2c33801225 100644
--- a/packages/cli/src/log.ts
+++ b/packages/cli/src/log.ts
@@ -1,75 +1,7 @@
/* eslint-disable no-console */
-import type {LogLevel} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {chalk} from './chalk';
-import {ConfigInternals} from './config';
-import {truthy} from './truthy';
export const INDENT_TOKEN = chalk.gray('β”‚');
-export const Log = {
- verbose: (...args: Parameters<typeof console.log>) => {
- Log.verboseAdvanced(
- {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
- ...args
- );
- },
- verboseAdvanced: (
- options: {indent: boolean; logLevel: LogLevel},
- ...args: Parameters<typeof console.log>
- ) => {
- if (RenderInternals.isEqualOrBelowLogLevel(options.logLevel, 'verbose')) {
- return console.log(
- ...[
- options.indent ? INDENT_TOKEN : null,
- ...args.map((a) => chalk.blueBright(a)),
- ].filter(truthy)
- );
- }
- },
- info: (...args: Parameters<typeof console.log>) => {
- Log.infoAdvanced(
- {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
- ...args
- );
- },
- infoAdvanced: (
- options: {indent: boolean; logLevel: LogLevel},
- ...args: Parameters<typeof console.log>
- ) => {
- if (RenderInternals.isEqualOrBelowLogLevel(options.logLevel, 'info')) {
- return console.log(
- ...[options.indent ? INDENT_TOKEN : null, ...args].filter(truthy)
- );
- }
- },
- warn: (...args: Parameters<typeof console.log>) => {
- Log.warnAdvanced(
- {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
- ...args
- );
- },
- warnAdvanced: (
- options: {indent: boolean; logLevel: LogLevel},
- ...args: Parameters<typeof console.log>
- ) => {
- if (RenderInternals.isEqualOrBelowLogLevel(options.logLevel, 'warn')) {
- return console.warn(
- ...[
- options.indent ? chalk.yellow(INDENT_TOKEN) : null,
- ...args.map((a) => chalk.yellow(a)),
- ].filter(truthy)
- );
- }
- },
- error: (...args: Parameters<typeof console.log>) => {
- if (
- RenderInternals.isEqualOrBelowLogLevel(
- ConfigInternals.Logging.getLogLevel(),
- 'error'
- )
- ) {
- return console.error(...args.map((a) => chalk.red(a)));
- }
- },
-};
+export const {Log} = RenderInternals;
diff --git a/packages/renderer/src/chalk/index.ts b/packages/renderer/src/chalk/index.ts
new file mode 100644
index 0000000000..aafbfb00d0
--- /dev/null
+++ b/packages/renderer/src/chalk/index.ts
@@ -0,0 +1,208 @@
+const hasColor = () => {
+ if (typeof process !== 'undefined') {
+ return process.env.FORCE_COLOR !== '0';
+ }
+
+ return false;
+};
+
+type Style = {
+ codes: [number, number];
+ name: string;
+ wrap?: (input: string, newline: boolean) => string;
+};
+
+type Method = (str: string) => string;
+
+type Colors = {
+ enabled: boolean;
+ visible: boolean;
+ styles: Record<string, Style>;
+ keys: Record<string, string[]>;
+ alias?: (name: string, col: string) => void;
+};
+
+type ColorsWithMethods = Colors & {
+ reset: Method;
+ bold: Method;
+ dim: Method;
+ italic: Method;
+ underline: Method;
+ inverse: Method;
+ hidden: Method;
+ strikethrough: Method;
+ black: Method;
+ red: Method;
+ green: Method;
+ yellow: Method;
+ blue: Method;
+ magenta: Method;
+ cyan: Method;
+ white: Method;
+ gray: Method;
+ bgBlack: Method;
+ bgRed: Method;
+ bgGreen: Method;
+ bgYellow: Method;
+ bgBlue: Method;
+ bgMagenta: Method;
+ bgCyan: Method;
+ bgWhite: Method;
+ blackBright: Method;
+ redBright: Method;
+ greenBright: Method;
+ yellowBright: Method;
+ blueBright: Method;
+ magentaBright: Method;
+ cyanBright: Method;
+ whiteBright: Method;
+ bgBlackBright: Method;
+ bgRedBright: Method;
+ bgGreenBright: Method;
+ bgYellowBright: Method;
+ bgBlueBright: Method;
+ bgMagentaBright: Method;
+ bgCyanBright: Method;
+ bgWhiteBright: Method;
+};
+
+export const chalk = (() => {
+ const colors: Colors = {
+ enabled: hasColor(),
+ visible: true,
+ styles: {},
+ keys: {},
+ };
+
+ const ansi = (st: Style) => {
+ const open = `\u001b[${st.codes[0]}m`;
+ const close = `\u001b[${st.codes[1]}m`;
+ const regex = new RegExp(`\\u001b\\[${st.codes[1]}m`, 'g');
+ st.wrap = (input: string, newline: boolean) => {
+ if (input.includes(close)) input = input.replace(regex, close + open);
+ const output = open + input + close;
+ // see https://github.com/chalk/chalk/pull/92, thanks to the
+ // chalk contributors for this fix. However, we've confirmed that
+ // this issue is also present in Windows terminals
+ return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
+ };
+
+ return st;
+ };
+
+ const wrap = (sty: Style, input: string, newline: boolean) => {
+ return sty.wrap?.(input, newline) as string;
+ };
+
+ const style = (input: string | null | undefined, stack: string[]) => {
+ if (input === '' || input === null || input === undefined) return '';
+ if (colors.enabled === false) return input;
+ if (colors.visible === false) return '';
+ let str = String(input);
+ const nl = str.includes('\n');
+ let n = stack.length;
+
+ while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
+ return str;
+ };
+
+ const define = (name: string, codes: [number, number], type: string) => {
+ colors.styles[name] = ansi({name, codes});
+ const keys = colors.keys[type] || (colors.keys[type] = []);
+ keys.push(name);
+
+ Reflect.defineProperty(colors, name, {
+ configurable: true,
+ enumerable: true,
+ set(value) {
+ colors.alias?.(name, value);
+ },
+ get() {
+ const color = (input: string) => style(input, color.stack);
+ Reflect.setPrototypeOf(color, colors);
+ color.stack = this.stack ? this.stack.concat(name) : [name];
+ return color;
+ },
+ });
+ };
+
+ define('reset', [0, 0], 'modifier');
+ define('bold', [1, 22], 'modifier');
+ define('dim', [2, 22], 'modifier');
+ define('italic', [3, 23], 'modifier');
+ define('underline', [4, 24], 'modifier');
+ define('inverse', [7, 27], 'modifier');
+ define('hidden', [8, 28], 'modifier');
+ define('strikethrough', [9, 29], 'modifier');
+
+ define('black', [30, 39], 'color');
+ define('red', [31, 39], 'color');
+ define('green', [32, 39], 'color');
+ define('yellow', [33, 39], 'color');
+ define('blue', [34, 39], 'color');
+ define('magenta', [35, 39], 'color');
+ define('cyan', [36, 39], 'color');
+ define('white', [37, 39], 'color');
+ define('gray', [90, 39], 'color');
+ define('grey', [90, 39], 'color');
+
+ define('bgBlack', [40, 49], 'bg');
+ define('bgRed', [41, 49], 'bg');
+ define('bgGreen', [42, 49], 'bg');
+ define('bgYellow', [43, 49], 'bg');
+ define('bgBlue', [44, 49], 'bg');
+ define('bgMagenta', [45, 49], 'bg');
+ define('bgCyan', [46, 49], 'bg');
+ define('bgWhite', [47, 49], 'bg');
+
+ define('blackBright', [90, 39], 'bright');
+ define('redBright', [91, 39], 'bright');
+ define('greenBright', [92, 39], 'bright');
+ define('yellowBright', [93, 39], 'bright');
+ define('blueBright', [94, 39], 'bright');
+ define('magentaBright', [95, 39], 'bright');
+ define('cyanBright', [96, 39], 'bright');
+ define('whiteBright', [97, 39], 'bright');
+
+ define('bgBlackBright', [100, 49], 'bgBright');
+ define('bgRedBright', [101, 49], 'bgBright');
+ define('bgGreenBright', [102, 49], 'bgBright');
+ define('bgYellowBright', [103, 49], 'bgBright');
+ define('bgBlueBright', [104, 49], 'bgBright');
+ define('bgMagentaBright', [105, 49], 'bgBright');
+ define('bgCyanBright', [106, 49], 'bgBright');
+ define('bgWhiteBright', [107, 49], 'bgBright');
+
+ colors.alias = (name: string, color: string) => {
+ // @ts-expect-error
+ const fn = colors[color];
+
+ if (typeof fn !== 'function') {
+ throw new TypeError(
+ 'Expected alias to be the name of an existing color (string) or a function'
+ );
+ }
+
+ if (!fn.stack) {
+ Reflect.defineProperty(fn, 'name', {value: name});
+ colors.styles[name] = fn;
+ fn.stack = [name];
+ }
+
+ Reflect.defineProperty(colors, name, {
+ configurable: true,
+ enumerable: true,
+ set(value) {
+ colors.alias?.(name, value);
+ },
+ get() {
+ const col = (input: string) => style(input, col.stack);
+ Reflect.setPrototypeOf(col, colors);
+ col.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
+ return col;
+ },
+ });
+ };
+
+ return colors as ColorsWithMethods;
+})();
diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.ts
index 1a32d85cd4..eba01126d7 100644
--- a/packages/renderer/src/compositor/compositor.ts
+++ b/packages/renderer/src/compositor/compositor.ts
@@ -3,6 +3,7 @@ import {chmodSync} from 'node:fs';
import os from 'node:os';
import {dynamicLibraryPathOptions} from '../call-ffmpeg';
import {getActualConcurrency} from '../get-concurrency';
+import {log} from '../logger';
import {serializeCommand} from './compose';
import {getExecutablePath} from './get-executable-path';
import {makeNonce} from './make-nonce';
@@ -79,7 +80,7 @@ export const startCompositor = <T extends keyof CompositorCommand>(
data: Buffer
) => {
if (nonce === '0') {
- console.log('[Compositor]', data.toString('utf8'));
+ log('Compositor', data.toString('utf8'));
}
if (waiters.has(nonce)) {
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index db5dc96ba2..939d3c7cc9 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -10,6 +10,7 @@ import {DEFAULT_BROWSER} from './browser';
import {DEFAULT_TIMEOUT} from './browser/TimeoutSettings';
import {callFf, dynamicLibraryPathOptions} from './call-ffmpeg';
import {canUseParallelEncoding} from './can-use-parallel-encoding';
+import {chalk} from './chalk';
import {checkNodeVersionAndWarnAboutRosetta} from './check-apple-silicon';
import {DEFAULT_CODEC, validCodecs} from './codec';
import {combineVideos} from './combine-videos';
@@ -43,6 +44,7 @@ import {isAudioCodec} from './is-audio-codec';
import {isServeUrl} from './is-serve-url';
import {validateJpegQuality} from './jpeg-quality';
import {isEqualOrBelowLogLevel, isValidLogLevel, logLevels} from './log-level';
+import {Log} from './logger';
import {mimeContentType, mimeLookup} from './mime-types';
import {killAllBrowsers} from './open-browser';
import {parseStack} from './parse-browser-error-stack';
@@ -171,6 +173,8 @@ export const RenderInternals = {
validVideoImageFormats,
DEFAULT_STILL_IMAGE_FORMAT,
DEFAULT_VIDEO_IMAGE_FORMAT,
+ chalk,
+ Log,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)
diff --git a/packages/renderer/src/logger.ts b/packages/renderer/src/logger.ts
new file mode 100644
index 0000000000..dee86b1dcb
--- /dev/null
+++ b/packages/renderer/src/logger.ts
@@ -0,0 +1,74 @@
+export const log = (tag: string, message: string) => {
+ console.log(message);
+};
+
+import {chalk} from './chalk';
+import type {LogLevel} from './log-level';
+import {isEqualOrBelowLogLevel} from './log-level';
+import {truthy} from './truthy';
+
+export const INDENT_TOKEN = chalk.gray('β”‚');
+
+export const Log = {
+ verbose: (...args: Parameters<typeof console.log>) => {
+ Log.verboseAdvanced(
+ {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
+ ...args
+ );
+ },
+ verboseAdvanced: (
+ options: {indent: boolean; logLevel: LogLevel},
+ ...args: Parameters<typeof console.log>
+ ) => {
+ if (isEqualOrBelowLogLevel(options.logLevel, 'verbose')) {
+ return console.log(
+ ...[
+ options.indent ? INDENT_TOKEN : null,
+ ...args.map((a) => chalk.blueBright(a)),
+ ].filter(truthy)
+ );
+ }
+ },
+ info: (...args: Parameters<typeof console.log>) => {
+ Log.infoAdvanced(
+ {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
+ ...args
+ );
+ },
+ infoAdvanced: (
+ options: {indent: boolean; logLevel: LogLevel},
+ ...args: Parameters<typeof console.log>
+ ) => {
+ if (isEqualOrBelowLogLevel(options.logLevel, 'info')) {
+ return console.log(
+ ...[options.indent ? INDENT_TOKEN : null, ...args].filter(truthy)
+ );
+ }
+ },
+ warn: (...args: Parameters<typeof console.log>) => {
+ Log.warnAdvanced(
+ {indent: false, logLevel: ConfigInternals.Logging.getLogLevel()},
+ ...args
+ );
+ },
+ warnAdvanced: (
+ options: {indent: boolean; logLevel: LogLevel},
+ ...args: Parameters<typeof console.log>
+ ) => {
+ if (isEqualOrBelowLogLevel(options.logLevel, 'warn')) {
+ return console.warn(
+ ...[
+ options.indent ? chalk.yellow(INDENT_TOKEN) : null,
+ ...args.map((a) => chalk.yellow(a)),
+ ].filter(truthy)
+ );
+ }
+ },
+ error: (...args: Parameters<typeof console.log>) => {
+ if (
+ isEqualOrBelowLogLevel(ConfigInternals.Logging.getLogLevel(), 'error')
+ ) {
+ return console.error(...args.map((a) => chalk.red(a)));
+ }
+ },
+};