1
1
const os = require ( 'os' )
2
2
const { join, dirname, basename } = require ( 'path' )
3
- const { Minipass } = require ( 'minipass' )
4
3
const fsMiniPass = require ( 'fs-minipass' )
5
4
const fs = require ( 'fs/promises' )
6
5
const { log } = require ( 'proc-log' )
@@ -9,9 +8,9 @@ const { formatWithOptions } = require('./format')
9
8
const padZero = ( n , length ) => n . toString ( ) . padStart ( length . toString ( ) . length , '0' )
10
9
11
10
class LogFiles {
12
- // Default to a plain minipass stream so we can buffer
11
+ // Default to an array so we can buffer
13
12
// initial writes before we know the cache location
14
- #logStream = null
13
+ #logStream = [ ]
15
14
16
15
// We cap log files at a certain number of log events per file.
17
16
// Note that each log event can write more than one line to the
@@ -29,6 +28,7 @@ class LogFiles {
29
28
#path = null
30
29
#logsMax = null
31
30
#files = [ ]
31
+ #timing = false
32
32
33
33
constructor ( {
34
34
maxLogsPerFile = 50_000 ,
@@ -40,7 +40,6 @@ class LogFiles {
40
40
}
41
41
42
42
on ( ) {
43
- this . #logStream = new Minipass ( )
44
43
process . on ( 'log' , this . #logHandler)
45
44
}
46
45
@@ -49,11 +48,12 @@ class LogFiles {
49
48
this . #endStream( )
50
49
}
51
50
52
- load ( { path, logsMax = Infinity } = { } ) {
51
+ load ( { path, logsMax = Infinity , timing } = { } ) {
53
52
// dir is user configurable and is required to exist so
54
53
// this can error if the dir is missing or not configured correctly
55
54
this . #path = path
56
55
this . #logsMax = logsMax
56
+ this . #timing = timing
57
57
58
58
// Log stream has already ended
59
59
if ( ! this . #logStream) {
@@ -62,13 +62,19 @@ class LogFiles {
62
62
63
63
log . verbose ( 'logfile' , `logs-max:${ logsMax } dir:${ this . #path} ` )
64
64
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
66
66
// set that as the new log logstream for future writes
67
67
// if logs max is 0 then the user does not want a log file
68
68
if ( this . #logsMax > 0 ) {
69
69
const initialFile = this . #openLogFile( )
70
70
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
72
78
}
73
79
}
74
80
@@ -80,20 +86,16 @@ class LogFiles {
80
86
return this . #cleanLogs( )
81
87
}
82
88
83
- log ( ...args ) {
84
- this . #logHandler( ...args )
85
- }
86
-
87
89
get files ( ) {
88
90
return this . #files
89
91
}
90
92
91
93
get #isBuffered ( ) {
92
- return this . #logStream instanceof Minipass
94
+ return Array . isArray ( this . #logStream)
93
95
}
94
96
95
97
#endStream ( output ) {
96
- if ( this . #logStream) {
98
+ if ( this . #logStream && ! this . #isBuffered ) {
97
99
this . #logStream. end ( output )
98
100
this . #logStream = null
99
101
}
@@ -111,12 +113,15 @@ class LogFiles {
111
113
return
112
114
}
113
115
114
- const logOutput = this . #formatLogItem( level , ...args )
115
-
116
116
if ( this . #isBuffered) {
117
117
// Cant do anything but buffer the output if we dont
118
118
// 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 ) {
120
125
return
121
126
}
122
127
@@ -137,6 +142,11 @@ class LogFiles {
137
142
}
138
143
139
144
#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
+
140
150
this . #fileLogCount += 1
141
151
const prefix = [ this . #totalLogCount++ , level , title || null ]
142
152
return formatWithOptions ( { prefix, eol : os . EOL , colors : false } , ...args )
0 commit comments