Skip to content

Commit

Permalink
removed use of env vars and mvoed to options arg in logger constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Jul 8, 2024
1 parent c0f1889 commit cd29b42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import envPaths from 'env-paths'
import { join } from 'path'
import * as winston from 'winston'
import { maskPayload } from './masking'
import { EntropyLoggerOptions } from 'src/types'

/**
* Winston Base Log Levels for NPM
Expand All @@ -20,8 +21,8 @@ export class EntropyLogger {
protected context: string
protected endpoint: string
private winstonLogger: winston.Logger

constructor (context: string, endpoint: string) {
// TO-DO: update commander with debug, testing, and level options for both programmatic and textual cli
constructor (context: string, endpoint: string, { debug, isTesting, level }: EntropyLoggerOptions = {}) {
this.context = context
this.endpoint = endpoint

Expand All @@ -40,7 +41,7 @@ export class EntropyLogger {
winston.format.json(),
);

if (process.env.NODE_ENV === 'test') {
if (isTesting) {
format = winston.format.combine(
format,
winston.format.colorize({ level: true }),
Expand All @@ -59,7 +60,7 @@ export class EntropyLogger {
const INFO_PATH = join(paths.log, 'entropy-cli.info.log')

this.winstonLogger = winston.createLogger({
level: process.env.LOG_LEVEL,
level: level || 'info',
format,
defaultMeta: { service: 'Entropy CLI' },
transports: [
Expand All @@ -79,7 +80,7 @@ export class EntropyLogger {
})

// If env var is set then stream logs to console as well as a file
if (process.env.DEBUG) {
if (debug) {
this.winstonLogger.add(new winston.transports.Console({
format: winston.format.cli()
}))
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ export interface EntropyTuiOptions {
endpoint: string
}

type EntropyLoggerLogLevel = 'error' | 'warn' | 'info' | 'debug'

export interface EntropyLoggerOptions {
debug?: boolean
level?: EntropyLoggerLogLevel
isTesting?: boolean
}

0 comments on commit cd29b42

Please sign in to comment.