import { isNode } from '@app-config/utils' ; export enum LogLevel { Verbose = 'verbose' , Info = 'info' , Warn = 'warn' , Error = 'error' , None = 'none' } let isTest = isNode && process . env . NODE_ENV === 'test' ; export function isTestEnvAndShouldNotPrompt ( newValue ? ) { if ( newValue !== undefined ) { isTest = newValue ; } return isTest ; } export function checkTTY ( ) { return isNode && process . stdin . isTTY && process . stdout . isTTY && ! isTestEnvAndShouldNotPrompt ( ) ; } export function getInitialLogLevel ( ) { if ( ! isNode ) return LogLevel . Warn ; if ( process . env . APP_CONFIG_LOG_LEVEL ) { return process . env . APP_CONFIG_LOG_LEVEL as LogLevel ; } if ( process . env . NODE_ENV === 'test' ) { return LogLevel . None ; } if ( checkTTY ( ) ) { return LogLevel . Info ; } return LogLevel . Warn ; } type Writer = ( message : string ) => void ; interface Logger { setWriter ( write ) : void ; setLevel ( level ) : void ; verbose ( message ) : void ; info ( message ) : void ; warn ( message ) : void ; error ( message ) : void ; } let logLevel = getInitialLogLevel ( ) ; let writeMsg = isNode ? process . stderr . write . bind ( process . stderr ) : console . info ; export const logger = { setWriter ( write ) { writeMsg = write ; } , setLevel ( level ) { logLevel = level ; } , verbose ( message ) { switch ( logLevel ) { case LogLevel . Verbose : writeMsg ( `[app-config][VERBOSE] ${ message }\n` ) ; break ; default : break ; } } , info ( message ) { switch ( logLevel ) { case LogLevel . Verbose : case LogLevel . Info : writeMsg ( `[app-config][INFO] ${ message }\n` ) ; break ; default : break ; } } , warn ( message ) { switch ( logLevel ) { case LogLevel . Verbose : case LogLevel . Info : case LogLevel . Warn : writeMsg ( `[app-config][WARN] ${ message }\n` ) ; break ; default : break ; } } , error ( message ) { switch ( logLevel ) { case LogLevel . Verbose : case LogLevel . Info : case LogLevel . Warn : case LogLevel . Error : writeMsg ( `[app-config][ERROR] ${ message }\n` ) ; break ; default : break ; } } , } ; export const setLogLevel = logger . setLevel ; export const setLogWriter = logger . setWriter ;