Skip to content

Commit

Permalink
refactor: Use updated logger to reduce log file path logic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Mar 28, 2024
1 parent eb62c2c commit abe6638
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
49 changes: 45 additions & 4 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.13",
"@foxxmd/logging": "^0.1.14",
"@foxxmd/string-sameness": "^0.4.0",
"@kenyip/backoff-strategies": "^1.0.4",
"@react-nano/use-event-source": "^0.13.0",
Expand Down
24 changes: 6 additions & 18 deletions src/backend/common/logging.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
childLogger,
Logger as FoxLogger,
loggerAppRolling,
LogLevel,
LogOptions as FoxLogOptions,
parseLogOptions,
} from '@foxxmd/logging';
import { childLogger, Logger, loggerAppRolling, LogOptions, parseLogOptions, } from '@foxxmd/logging';
import { buildDestinationJsonPrettyStream, buildDestinationStdout, buildLogger } from "@foxxmd/logging/factory";
import { PassThrough, Transform } from "node:stream";
import path from "path";
Expand All @@ -17,7 +10,7 @@ if (typeof process.env.CONFIG_DIR === 'string') {
logPath = path.resolve(process.env.CONFIG_DIR, './logs');
}

export const initLogger = (): [FoxLogger, Transform] => {
export const initLogger = (): [Logger, Transform] => {
const opts = parseLogOptions({file: false, console: 'debug'})
const stream = new PassThrough({objectMode: true});
const logger = buildLogger('debug', [
Expand All @@ -27,27 +20,22 @@ export const initLogger = (): [FoxLogger, Transform] => {
return [logger, stream];
}

export const appLogger = async (config: FoxLogOptions = {}): Promise<[FoxLogger, PassThrough]> => {
export const appLogger = async (config: LogOptions = {}): Promise<[Logger, PassThrough]> => {
const stream = new PassThrough({objectMode: true});
if(process.env.LOG_PATH === undefined && (config.file === undefined || config.file !== false) && (typeof config.file !== 'object' || config.file?.path === undefined)) {
config.file = {
level: config.file as LogLevel,
path: 'logs/scrobble.log'
}
}
const opts = parseLogOptions(config)
const logger = await loggerAppRolling(config, {
logBaseDir: typeof process.env.CONFIG_DIR === 'string' ? process.env.CONFIG_DIR : undefined,
logDefaultPath: './logs/scrobble.log',
destinations: [
buildDestinationJsonPrettyStream(opts.console, {destination: stream, object: true, colorize: true})
]
});
return [logger, stream];
}
export class MaybeLogger {
logger?: FoxLogger
logger?: Logger

constructor(logger?: FoxLogger, label?: string) {
constructor(logger?: Logger, label?: string) {
if (logger !== undefined && label !== undefined) {
this.logger = childLogger(logger, label);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/backend/ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { createContainer } from "iti";
import normalizeUrl from 'normalize-url';
import path from "path";
import { projectDir } from "./common/index.js";
import { logPath } from "./common/logging.js";
import { WildcardEmitter } from "./common/WildcardEmitter.js";
import { Notifiers } from "./notifier/Notifiers.js";
import ScrobbleClients from "./scrobblers/ScrobbleClients.js";
Expand Down Expand Up @@ -59,7 +58,6 @@ const createRoot = (options?: RootOptions) => {
return createContainer().add({
version,
configDir: configDir,
logDir: logPath,
isProd: process.env.NODE_ENV !== undefined && (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'prod'),
port: process.env.PORT ?? port,
clientEmitter: () => new WildcardEmitter(),
Expand Down

0 comments on commit abe6638

Please sign in to comment.