@@ -82,13 +82,24 @@ function valueToAttribute(key: string, value: unknown): LogAttribute {
82
82
}
83
83
}
84
84
85
+ let hasRegisteredFlushHook = false ;
86
+
85
87
function addToLogBuffer ( client : Client , log : Log , scope : Scope ) : void {
86
88
function sendLogs ( flushedLogs : Log [ ] ) : void {
87
89
const envelope = createLogEnvelope ( flushedLogs , client , scope ) ;
88
90
// eslint-disable-next-line @typescript-eslint/no-floating-promises
89
91
void client . sendEnvelope ( envelope ) ;
90
92
}
91
93
94
+ // Only register the hook once
95
+ if ( ! hasRegisteredFlushHook ) {
96
+ client . on ( 'flush' , ( ) => {
97
+ sendLogs ( GLOBAL_LOG_BUFFER ) ;
98
+ GLOBAL_LOG_BUFFER = [ ] ;
99
+ } ) ;
100
+ hasRegisteredFlushHook = true ;
101
+ }
102
+
92
103
if ( GLOBAL_LOG_BUFFER . length >= LOG_BUFFER_MAX_LENGTH ) {
93
104
sendLogs ( GLOBAL_LOG_BUFFER ) ;
94
105
GLOBAL_LOG_BUFFER = [ ] ;
@@ -99,12 +110,17 @@ function addToLogBuffer(client: Client, log: Log, scope: Scope): void {
99
110
// this is the first time logs have been enabled, let's kick off an interval to flush them
100
111
// we should only do this once.
101
112
if ( ! isFlushingLogs ) {
102
- setInterval ( ( ) => {
113
+ const flushTimer = setInterval ( ( ) => {
103
114
if ( GLOBAL_LOG_BUFFER . length > 0 ) {
104
115
sendLogs ( GLOBAL_LOG_BUFFER ) ;
105
116
GLOBAL_LOG_BUFFER = [ ] ;
106
117
}
107
118
} , 5000 ) ;
119
+
120
+ // We need to unref the timer in node.js, otherwise the node process never exit.
121
+ if ( typeof flushTimer !== 'number' && flushTimer . unref ) {
122
+ flushTimer . unref ( ) ;
123
+ }
108
124
}
109
125
isFlushingLogs = true ;
110
126
}
0 commit comments