Skip to content

Commit 2538438

Browse files
committed
fix: dont write timing logs to file unless requested
1 parent 7e04417 commit 2538438

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

lib/npm.js

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class Npm {
204204
this.#logFile.load({
205205
path: this.logPath,
206206
logsMax: this.config.get('logs-max'),
207+
timing: this.config.get('timing'),
207208
})
208209

209210
this.#timers.load({

lib/utils/log-file.js

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const os = require('os')
22
const { join, dirname, basename } = require('path')
3-
const { Minipass } = require('minipass')
43
const fsMiniPass = require('fs-minipass')
54
const fs = require('fs/promises')
65
const { log } = require('proc-log')
@@ -9,9 +8,9 @@ const { formatWithOptions } = require('./format')
98
const padZero = (n, length) => n.toString().padStart(length.toString().length, '0')
109

1110
class LogFiles {
12-
// Default to a plain minipass stream so we can buffer
11+
// Default to an array so we can buffer
1312
// initial writes before we know the cache location
14-
#logStream = null
13+
#logStream = []
1514

1615
// We cap log files at a certain number of log events per file.
1716
// Note that each log event can write more than one line to the
@@ -29,6 +28,7 @@ class LogFiles {
2928
#path = null
3029
#logsMax = null
3130
#files = []
31+
#timing = false
3232

3333
constructor ({
3434
maxLogsPerFile = 50_000,
@@ -40,7 +40,6 @@ class LogFiles {
4040
}
4141

4242
on () {
43-
this.#logStream = new Minipass()
4443
process.on('log', this.#logHandler)
4544
}
4645

@@ -49,11 +48,12 @@ class LogFiles {
4948
this.#endStream()
5049
}
5150

52-
load ({ path, logsMax = Infinity } = {}) {
51+
load ({ path, logsMax = Infinity, timing } = {}) {
5352
// dir is user configurable and is required to exist so
5453
// this can error if the dir is missing or not configured correctly
5554
this.#path = path
5655
this.#logsMax = logsMax
56+
this.#timing = timing
5757

5858
// Log stream has already ended
5959
if (!this.#logStream) {
@@ -62,13 +62,19 @@ class LogFiles {
6262

6363
log.verbose('logfile', `logs-max:${logsMax} dir:${this.#path}`)
6464

65-
// Pipe our initial stream to our new file stream and
65+
// Write the contents of our array buffer to our new file stream and
6666
// set that as the new log logstream for future writes
6767
// if logs max is 0 then the user does not want a log file
6868
if (this.#logsMax > 0) {
6969
const initialFile = this.#openLogFile()
7070
if (initialFile) {
71-
this.#logStream = this.#logStream.pipe(initialFile)
71+
for (const item of this.#logStream) {
72+
const formatted = this.#formatLogItem(...item)
73+
if (formatted !== null) {
74+
initialFile.write(formatted)
75+
}
76+
}
77+
this.#logStream = initialFile
7278
}
7379
}
7480

@@ -80,20 +86,16 @@ class LogFiles {
8086
return this.#cleanLogs()
8187
}
8288

83-
log (...args) {
84-
this.#logHandler(...args)
85-
}
86-
8789
get files () {
8890
return this.#files
8991
}
9092

9193
get #isBuffered () {
92-
return this.#logStream instanceof Minipass
94+
return Array.isArray(this.#logStream)
9395
}
9496

9597
#endStream (output) {
96-
if (this.#logStream) {
98+
if (this.#logStream && !this.#isBuffered) {
9799
this.#logStream.end(output)
98100
this.#logStream = null
99101
}
@@ -111,12 +113,15 @@ class LogFiles {
111113
return
112114
}
113115

114-
const logOutput = this.#formatLogItem(level, ...args)
115-
116116
if (this.#isBuffered) {
117117
// Cant do anything but buffer the output if we dont
118118
// have a file stream yet
119-
this.#logStream.write(logOutput)
119+
this.#logStream.push([level, ...args])
120+
return
121+
}
122+
123+
const logOutput = this.#formatLogItem(level, ...args)
124+
if (logOutput === null) {
120125
return
121126
}
122127

@@ -137,6 +142,11 @@ class LogFiles {
137142
}
138143

139144
#formatLogItem (level, title, ...args) {
145+
// Only right timing logs to logfile if explicitly requests
146+
if (level === log.KEYS.timing && !this.#timing) {
147+
return null
148+
}
149+
140150
this.#fileLogCount += 1
141151
const prefix = [this.#totalLogCount++, level, title || null]
142152
return formatWithOptions({ prefix, eol: os.EOL, colors: false }, ...args)

test/lib/utils/log-file.js

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const loadLogFile = async (t, { buffer = [], mocks, testdir = {}, ...options } =
4646
const MockLogFile = tmock(t, '{LIB}/utils/log-file.js', mocks)
4747
const logFile = new MockLogFile(Object.keys(options).length ? options : undefined)
4848

49+
// Create a fake public method since there is not one on logFile anymore
50+
logFile.log = (...b) => process.emit('log', ...b)
4951
buffer.forEach((b) => logFile.log(...b))
5052

5153
const id = getId()

0 commit comments

Comments
 (0)