import getLogLevel , { rfcLogLevels , LevelInfo , LevelsMap } from 'rfc-log-levels' ; import getCurrentLine , { Offset , Location } from 'get-current-line' ; import { Transform } from './transform.js' ; /** The log entry that Caterpillar creates and forwards to its transforms */ export interface LogEntry extends LevelInfo , Location { /** the iso string of when the log occured */ date : string ; /** all the arguments that were after the log level */ args : any [ ] ; } /** Configuration for the Caterpillar Logger */ export interface LoggerOptions { /** Use to override the default value of  Logger . lineOffset lineOffset ? : Offset ; /** Use to override the default value of  Logger . levels levels ? : LevelsMap ; /** Use to override the default value of  Logger . defaultLevelInfo defaultLevel ? : number | string ; /** Use to override the default value of  Logger . lineLevel lineLevel ? : number ; } example export class Logger extends Transform { /**
     * The configuration to use for the line offset.
     * This defaults to any file path that includes `logger`, and any method that includes the word `log`.
     */ public lineOffset : Offset = { file : /logger/i , method : /log/i , } ; /**
     * The mapping of log level names to log level numbers.
     * Defaults to the RFC Log Level configuration.
     */ public levels : LevelsMap = rfcLogLevels ; /**
     * The log level information to use when the log level was unable to be determined.
     * Defaults to the info log level.
     */ public defaultLevelInfo ! : LevelInfo ; /** Set the default level info via a level number or name. */ public set defaultLevel ( value ) { const levelInfo = this . getLogLevel ( value ) ; if ( levelInfo == null ) { throw new Error ( `caterpillar: the intended value of ${ value } for the default log level not found in the configured levels` ) ; } this . defaultLevelInfo = levelInfo ; } /**
     * Only fetch line information for entries that have a log level equal to, or below this number.
     * You should only specify this if you need it, as fFetching line information for thousands of log entries, which is typical in large applications, will slow your application down dramatically.
     * If not specified, defaults to `-Infinity` which effect is to ignore gathering line information for all log levels.
     */ public lineLevel : number = - Infinity ; /** Create our instance and apply our configuraiton options. */ constructor ( opts ? ) { super ( ) ; if ( opts ?. lineOffset != null ) this . lineOffset = opts . lineOffset ; if ( opts ?. levels != null ) this . levels = opts . levels ; if ( opts ?. lineLevel != null ) this . lineLevel = opts . lineLevel ; this . defaultLevel = opts ?. defaultLevel ?? 'info' ; this . levels = Object . assign ( { } , this . levels ) ; } /** Alias for  getLogLevel using the configured logger levels as reference.  getLogLevel ( value ) { return getLogLevel ( value , this . levels ) ; } /** Takes an arguments array and tranforms it into a log entry. */ format ( args ) { const level = args . shift ( ) ; let levelInfo = level === 'default' ? this . defaultLevelInfo : this . getLogLevel ( level ) ; if ( levelInfo == null ) { levelInfo = this . defaultLevelInfo ; args . unshift ( level ) ; } const date = new Date ( ) . toISOString ( ) ; const lineInfo = levelInfo . levelNumber <= this . lineLevel ? getCurrentLine ( this . lineOffset ) : { line : - 1 , char : - 1 , method : '' , file : '' , } ; return Object . assign ( { date , args } , levelInfo , lineInfo ) ; } example example log ( ... args ) { this . write ( args ) ; } /** Alias for log which prefixes the error log level */ error ( ... args ) { this . write ( [ 'error' , ... args ] ) ; } /** Alias for log which prefixes the warn log level */ warn ( ... args ) { this . write ( [ 'warn' , ... args ] ) ; } /** Alias for log which prefixes the info log level */ info ( ... args ) { this . write ( [ 'info' , ... args ] ) ; } /** Alias for log which prefixes the debug log level */ debug ( ... args ) { this . write ( [ 'debug' , ... args ] ) ; } } export default Logger ;