import { isMaster , isWorker } from 'cluster' ; import { LRUCache } from '../../caches' ; import { LogLevel } from './logger' ; export interface LogMessage { cmd : typeof LOG_ONCE ; message : string ; level : LogLevel ; } const history = new LRUCache < string , boolean > ( { max : 42 , } ) ; export const LOG_ONCE = 'logOnce' ; export const isLog = ( message ) : message is LogMessage => message ?. cmd === LOG_ONCE ; export const log = ( message , level ) => { const logger = console [ level ] ; if ( typeof logger === 'function' ) { logger ( message ) ; } } ; export const logOnceToDevConsole = ( message , level ) : void => { const strigified = JSON . stringify ( message ) ; if ( ! history . has ( strigified ) ) { history . set ( strigified , true ) ; if ( isMaster ) { log ( message , level ) ; } else if ( isWorker && process . send ) { const logMessage = { cmd : LOG_ONCE , level , message , } ; process . send ( logMessage ) ; } } } ;