Skip to content

Commit

Permalink
fix: Fix colored output being applied to file logging
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Mar 21, 2024
1 parent 2f51e7a commit 6c27406
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ COPY --from=base /usr/local/lib /usr/local/lib

ENV NODE_ENV=production
ENV IS_DOCKER=true
ENV COLORED_CONSOLE=true
ENV COLORED_STD=true
#
#RUN yarn global add patch-package \
# && yarn install --production=true \
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@foxxmd/chromecast-client": "^1.0.4",
"@foxxmd/logging": "^0.1.12",
"@foxxmd/logging": "^0.1.13",
"@foxxmd/string-sameness": "^0.4.0",
"@kenyip/backoff-strategies": "^1.0.4",
"@react-nano/use-event-source": "^0.13.0",
Expand Down
19 changes: 2 additions & 17 deletions src/backend/common/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,11 @@ if (typeof process.env.CONFIG_DIR === 'string') {
logPath = path.resolve(process.env.CONFIG_DIR, './logs');
}

// docker stdout only colorizes if run with `-it` flag or `tty: true` in docker-compose
// but most common outputs and web log viewers (portainer, dozzle) support colors and using those flags/options is not common for most users
// so
// set COLORED_CONSOLE=true in Dockerfile to coerce colorizing output when running in our docker container.
// and using this instead of FORCE_COLOR (used by colorette) so that we only affect console output instead of all streams
const coloredEnv = process.env.COLORED_CONSOLE;
const coloredConsole = (coloredEnv === undefined || coloredEnv === '') ? undefined : parseBool(process.env.COLORED_CONSOLE);
const prettyDefaults: PrettyOptionsExtra = {};
// colorette only does autodetection if `colorize` prop is not present *at all*, rather than just being undefined
// so need to use default object and only add if we detect there is a non-empty value
if(coloredConsole !== undefined) {
prettyDefaults.colorize = coloredConsole;
}

export const initLogger = (): [FoxLogger, Transform] => {
const opts = parseLogOptions({file: false, console: 'debug'})
const stream = new PassThrough({objectMode: true});
const logger = buildLogger('debug', [
buildDestinationStdout(opts.console, prettyDefaults),
buildDestinationStdout(opts.console),
buildDestinationJsonPrettyStream(opts.console, {destination: stream, object: true, colorize: true})
]);
return [logger, stream];
Expand All @@ -54,8 +40,7 @@ export const appLogger = async (config: FoxLogOptions = {}): Promise<[FoxLogger,
logBaseDir: typeof process.env.CONFIG_DIR === 'string' ? process.env.CONFIG_DIR : undefined,
destinations: [
buildDestinationJsonPrettyStream(opts.console, {destination: stream, object: true, colorize: true})
],
pretty: prettyDefaults
]
});
return [logger, stream];
}
Expand Down

0 comments on commit 6c27406

Please sign in to comment.