@@ -7,18 +7,11 @@ const MiniPass = require('minipass')
77const fsMiniPass = require ( 'fs-minipass' )
88const fs = require ( '@npmcli/fs' )
99const log = require ( './log-shim' )
10+ const runId = require ( './run-id' )
1011
1112const padZero = ( n , length ) => n . toString ( ) . padStart ( length . toString ( ) . length , '0' )
1213const globify = pattern => pattern . split ( '\\' ) . join ( '/' )
1314
14- const _logHandler = Symbol ( 'logHandler' )
15- const _formatLogItem = Symbol ( 'formatLogItem' )
16- const _getLogFilePath = Symbol ( 'getLogFilePath' )
17- const _openLogFile = Symbol ( 'openLogFile' )
18- const _cleanLogs = Symbol ( 'cleanlogs' )
19- const _endStream = Symbol ( 'endStream' )
20- const _isBuffered = Symbol ( 'isBuffered' )
21-
2215class LogFiles {
2316 // If we write multiple log files we want them all to have the same
2417 // identifier for sorting and matching purposes
@@ -46,19 +39,16 @@ class LogFiles {
4639 #files = [ ]
4740
4841 constructor ( {
42+ id = runId ( ) ,
4943 maxLogsPerFile = 50_000 ,
5044 maxFilesPerProcess = 5 ,
5145 } = { } ) {
52- this . #logId = LogFiles . logId ( new Date ( ) )
46+ this . #logId = id
5347 this . #MAX_LOGS_PER_FILE = maxLogsPerFile
5448 this . #MAX_FILES_PER_PROCESS = maxFilesPerProcess
5549 this . on ( )
5650 }
5751
58- static logId ( d ) {
59- return d . toISOString ( ) . replace ( / [ . : ] / g, '_' )
60- }
61-
6252 static format ( count , level , title , ...args ) {
6353 let prefix = `${ count } ${ level } `
6454 if ( title ) {
@@ -75,12 +65,12 @@ class LogFiles {
7565
7666 on ( ) {
7767 this . #logStream = new MiniPass ( )
78- process . on ( 'log' , this [ _logHandler ] )
68+ process . on ( 'log' , this . #logHandler )
7969 }
8070
8171 off ( ) {
82- process . off ( 'log' , this [ _logHandler ] )
83- this [ _endStream ] ( )
72+ process . off ( 'log' , this . #logHandler )
73+ this . #endStream ( )
8474 }
8575
8676 load ( { dir, logsMax = Infinity } = { } ) {
@@ -100,7 +90,7 @@ class LogFiles {
10090 // set that as the new log logstream for future writes
10191 // if logs max is 0 then the user does not want a log file
10292 if ( this . #logsMax > 0 ) {
103- const initialFile = this [ _openLogFile ] ( )
93+ const initialFile = this . #openLogFile ( )
10494 if ( initialFile ) {
10595 this . #logStream = this . #logStream. pipe ( initialFile )
10696 }
@@ -109,29 +99,29 @@ class LogFiles {
10999 // Kickoff cleaning process, even if we aren't writing a logfile.
110100 // This is async but it will always ignore the current logfile
111101 // Return the result so it can be awaited in tests
112- return this [ _cleanLogs ] ( )
102+ return this . #cleanLogs ( )
113103 }
114104
115105 log ( ...args ) {
116- this [ _logHandler ] ( ...args )
106+ this . #logHandler ( ...args )
117107 }
118108
119109 get files ( ) {
120110 return this . #files
121111 }
122112
123- get [ _isBuffered ] ( ) {
113+ get #isBuffered ( ) {
124114 return this . #logStream instanceof MiniPass
125115 }
126116
127- [ _endStream ] ( output ) {
117+ #endStream ( output ) {
128118 if ( this . #logStream) {
129119 this . #logStream. end ( output )
130120 this . #logStream = null
131121 }
132122 }
133123
134- [ _logHandler ] = ( level , ...args ) => {
124+ #logHandler = ( level , ...args ) => {
135125 // Ignore pause and resume events since we
136126 // write everything to the log file
137127 if ( level === 'pause' || level === 'resume' ) {
@@ -143,9 +133,9 @@ class LogFiles {
143133 return
144134 }
145135
146- const logOutput = this [ _formatLogItem ] ( level , ...args )
136+ const logOutput = this . #formatLogItem ( level , ...args )
147137
148- if ( this [ _isBuffered ] ) {
138+ if ( this . #isBuffered ) {
149139 // Cant do anything but buffer the output if we dont
150140 // have a file stream yet
151141 this . #logStream. write ( logOutput )
@@ -155,29 +145,29 @@ class LogFiles {
155145 // Open a new log file if we've written too many logs to this one
156146 if ( this . #fileLogCount >= this . #MAX_LOGS_PER_FILE) {
157147 // Write last chunk to the file and close it
158- this [ _endStream ] ( logOutput )
148+ this . #endStream ( logOutput )
159149 if ( this . #files. length >= this . #MAX_FILES_PER_PROCESS) {
160150 // but if its way too many then we just stop listening
161151 this . off ( )
162152 } else {
163153 // otherwise we are ready for a new file for the next event
164- this . #logStream = this [ _openLogFile ] ( )
154+ this . #logStream = this . #openLogFile ( )
165155 }
166156 } else {
167157 this . #logStream. write ( logOutput )
168158 }
169159 }
170160
171- [ _formatLogItem ] ( ...args ) {
161+ #formatLogItem ( ...args ) {
172162 this . #fileLogCount += 1
173163 return LogFiles . format ( this . #totalLogCount++ , ...args )
174164 }
175165
176- [ _getLogFilePath ] ( count = '' ) {
166+ #getLogFilePath ( count = '' ) {
177167 return path . resolve ( this . #dir, `${ this . #logId} -debug-${ count } .log` )
178168 }
179169
180- [ _openLogFile ] ( ) {
170+ #openLogFile ( ) {
181171 // Count in filename will be 0 indexed
182172 const count = this . #files. length
183173
@@ -186,7 +176,7 @@ class LogFiles {
186176 // We never want to write files ending in `-9.log` and `-10.log` because
187177 // log file cleaning is done by deleting the oldest so in this example
188178 // `-10.log` would be deleted next
189- const f = this [ _getLogFilePath ] ( padZero ( count , this . #MAX_FILES_PER_PROCESS) )
179+ const f = this . #getLogFilePath ( padZero ( count , this . #MAX_FILES_PER_PROCESS) )
190180 // Some effort was made to make the async, but we need to write logs
191181 // during process.on('exit') which has to be synchronous. So in order
192182 // to never drop log messages, it is easiest to make it sync all the time
@@ -210,15 +200,15 @@ class LogFiles {
210200 }
211201 }
212202
213- async [ _cleanLogs ] ( ) {
203+ async #cleanLogs ( ) {
214204 // module to clean out the old log files
215205 // this is a best-effort attempt. if a rm fails, we just
216206 // log a message about it and move on. We do return a
217207 // Promise that succeeds when we've tried to delete everything,
218208 // just for the benefit of testing this function properly.
219209
220210 try {
221- const logPath = this [ _getLogFilePath ] ( )
211+ const logPath = this . #getLogFilePath ( )
222212 const logGlob = path . join ( path . dirname ( logPath ) , path . basename ( logPath )
223213 // tell glob to only match digits
224214 . replace ( / \d / g, '[0123456789]' )
0 commit comments