@@ -38,7 +38,8 @@ function isEmptyObject(value: any): boolean {
3838}
3939
4040function setLogPath ( p : string ) : void {
41- if ( p === logPath ) return // nothing to do
41+ // Recreate logger if the target changed or was removed between runs
42+ if ( p === logPath && existsSync ( p ) ) return // nothing to do
4243
4344 logPath = p
4445 mkdirSync ( dirname ( p ) , { recursive : true } )
@@ -94,59 +95,71 @@ function sendAnalyticsAndLog(
9495 msg ?: string ,
9596 ...args : any [ ]
9697) : void {
97- if (
98- process . env . CODEBUFF_GITHUB_ACTIONS !== 'true' &&
99- env . NEXT_PUBLIC_CB_ENVIRONMENT !== 'test'
100- ) {
101- const projectRoot = getProjectRoot ( )
102-
103- const logTarget =
104- env . NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
105- ? path . join ( projectRoot , 'debug' , 'cli.log' )
106- : path . join ( getCurrentChatDir ( ) , 'log.jsonl' )
107-
108- setLogPath ( logTarget )
98+ if ( process . env . CODEBUFF_DISABLE_FILE_LOGS === 'true' ) {
99+ return
109100 }
101+ try {
102+ if (
103+ process . env . CODEBUFF_GITHUB_ACTIONS !== 'true' &&
104+ env . NEXT_PUBLIC_CB_ENVIRONMENT !== 'test'
105+ ) {
106+ const projectRoot = getProjectRoot ( )
107+
108+ const logTarget =
109+ env . NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
110+ ? path . join ( projectRoot , 'debug' , 'cli.log' )
111+ : path . join ( getCurrentChatDir ( ) , 'log.jsonl' )
112+
113+ setLogPath ( logTarget )
114+ }
110115
111- const isStringOnly = typeof data === 'string' && msg === undefined
112- const normalizedData = isStringOnly ? undefined : data
113- const normalizedMsg = isStringOnly ? ( data as string ) : msg
114- const includeData = normalizedData != null && ! isEmptyObject ( normalizedData )
115-
116- const toTrack = {
117- ...( includeData ? { data : normalizedData } : { } ) ,
118- level,
119- loggerContext,
120- msg : stringFormat ( normalizedMsg , ...args ) ,
121- }
116+ const isStringOnly = typeof data === 'string' && msg === undefined
117+ const normalizedData = isStringOnly ? undefined : data
118+ const normalizedMsg = isStringOnly ? ( data as string ) : msg
119+ const includeData =
120+ normalizedData != null && ! isEmptyObject ( normalizedData )
122121
123- logAsErrorIfNeeded ( toTrack )
124-
125- logOrStore: if (
126- env . NEXT_PUBLIC_CB_ENVIRONMENT !== 'dev' &&
127- normalizedData &&
128- typeof normalizedData === 'object' &&
129- 'eventId' in normalizedData &&
130- Object . values ( AnalyticsEvent ) . includes ( ( normalizedData as any ) . eventId )
131- ) {
132- const analyticsEventId = data . eventId as AnalyticsEvent
133- // Not accurate for anonymous users
134- if ( ! loggerContext . userId ) {
135- analyticsBuffer . push ( { analyticsEventId, toTrack } )
136- break logOrStore
122+ const toTrack = {
123+ ...( includeData ? { data : normalizedData } : { } ) ,
124+ level,
125+ loggerContext,
126+ msg : stringFormat ( normalizedMsg , ...args ) ,
137127 }
138128
139- for ( const item of analyticsBuffer ) {
140- trackEvent ( item . analyticsEventId , item . toTrack )
129+ logAsErrorIfNeeded ( toTrack )
130+
131+ logOrStore: if (
132+ env . NEXT_PUBLIC_CB_ENVIRONMENT !== 'dev' &&
133+ normalizedData &&
134+ typeof normalizedData === 'object' &&
135+ 'eventId' in normalizedData &&
136+ Object . values ( AnalyticsEvent ) . includes ( ( normalizedData as any ) . eventId )
137+ ) {
138+ const analyticsEventId = data . eventId as AnalyticsEvent
139+ // Not accurate for anonymous users
140+ if ( ! loggerContext . userId ) {
141+ analyticsBuffer . push ( { analyticsEventId, toTrack } )
142+ break logOrStore
143+ }
144+
145+ for ( const item of analyticsBuffer ) {
146+ trackEvent ( item . analyticsEventId , item . toTrack )
147+ }
148+ analyticsBuffer . length = 0
149+ trackEvent ( analyticsEventId , toTrack )
141150 }
142- analyticsBuffer . length = 0
143- trackEvent ( analyticsEventId , toTrack )
144- }
145151
146- if ( pinoLogger !== undefined ) {
147- const base = { ...loggerContext }
148- const obj = includeData ? { ...base , data : normalizedData } : base
149- pinoLogger [ level ] ( obj , normalizedMsg as any , ...args )
152+ if ( pinoLogger !== undefined ) {
153+ try {
154+ const base = { ...loggerContext }
155+ const obj = includeData ? { ...base , data : normalizedData } : base
156+ pinoLogger [ level ] ( obj , normalizedMsg as any , ...args )
157+ } catch {
158+ // Ignore logging errors so they never interrupt CLI flow/tests
159+ }
160+ }
161+ } catch {
162+ // Swallow all logging errors in tests to avoid noisy failures
150163 }
151164}
152165
0 commit comments