11import { APP } from '../../constants'
2- import { IUserLandTracer } from '../../tracing'
32import { cleanError } from '../../utils/error'
43import { cleanLog } from '../../utils/log'
5- import { IOContext } from '../worker/runtime/typings'
6- import { logOnceToDevConsole } from './console'
4+ import { LogClient } from '@vtex/diagnostics-nodejs/dist/types' ;
5+ import { LoggerContext , LogLevel , TracingState } from './loggerTypes'
6+ import { getLogClient } from './client'
77
8- const linked = ! ! process . env . VTEX_APP_LINK
98const app = APP . ID
109const EMPTY_MESSAGE = 'Logger.log was called with null or undefined message'
1110
12- export interface LoggerTracingContext {
13- requestTracer : IUserLandTracer
14- }
15-
16- export enum LogLevel {
17- Debug = 'debug' ,
18- Info = 'info' ,
19- Warn = 'warn' ,
20- Error = 'error' ,
21- }
22-
23- interface LoggerContext extends Pick < IOContext , 'account' | 'workspace' | 'requestId' | 'operationId' | 'production' > {
24- tracer ?: IOContext [ 'tracer' ]
25- }
26-
27- interface TracingState {
28- isTraceSampled : boolean ,
29- traceId ?: string
30- }
31-
3211export class Logger {
3312 private account : string
3413 private workspace : string
3514 private operationId : string
3615 private requestId : string
3716 private production : boolean
3817 private tracingState ?: TracingState
18+ private logClient : LogClient | undefined = undefined
19+ private clientInitialized : boolean = false
20+ private clientInitializing : boolean = false
3921
4022 constructor ( ctx : LoggerContext ) {
4123 this . account = ctx . account
@@ -50,6 +32,25 @@ export class Logger {
5032 traceId : ctx . tracer . traceId ,
5133 }
5234 }
35+
36+ this . initLogClient ( ) ;
37+ }
38+
39+ private async initLogClient ( ) {
40+ if ( this . clientInitialized || this . clientInitializing ) {
41+ return ;
42+ }
43+
44+ this . clientInitializing = true ;
45+ try {
46+ this . logClient = await getLogClient ( this . account , this . workspace ) ;
47+ this . clientInitialized = true ;
48+ } catch ( error ) {
49+ console . error ( 'Failed to initialize log client:' , error ) ;
50+ throw error ;
51+ } finally {
52+ this . clientInitializing = false ;
53+ }
5354 }
5455
5556 public debug = ( message : any ) =>
@@ -86,11 +87,34 @@ export class Logger {
8687 // Mark third-party apps logs to send to skidder
8788 if ( APP . IS_THIRD_PARTY ( ) ) {
8889 Object . assign ( inflatedLog , {
89- __SKIDDER_TOPIC_1 : `skidder.vendor.${ APP . VENDOR } ` ,
90- __SKIDDER_TOPIC_2 : `skidder.app.${ APP . VENDOR } .${ APP . NAME } ` ,
91- } )
90+ ' __SKIDDER_TOPIC_1' : `skidder.vendor.${ APP . VENDOR } ` ,
91+ ' __SKIDDER_TOPIC_2' : `skidder.app.${ APP . VENDOR } .${ APP . NAME } ` ,
92+ } ) ;
9293 }
9394
94- console . log ( JSON . stringify ( inflatedLog ) )
95+ if ( this . logClient ) {
96+ try {
97+ let logMessage = typeof data === 'string' ? data : JSON . stringify ( data )
98+ switch ( level ) {
99+ case LogLevel . Debug :
100+ this . logClient . debug ( logMessage , inflatedLog ) ;
101+ break ;
102+ case LogLevel . Info :
103+ this . logClient . info ( logMessage , inflatedLog ) ;
104+ break ;
105+ case LogLevel . Warn :
106+ this . logClient . warn ( logMessage , inflatedLog ) ;
107+ break ;
108+ case LogLevel . Error :
109+ this . logClient . error ( logMessage , inflatedLog ) ;
110+ break ;
111+ default :
112+ this . logClient . info ( logMessage , inflatedLog ) ;
113+ }
114+ } catch ( e ) {
115+ console . error ( 'Error using diagnostics client for logging:' , e ) ;
116+ }
117+ }
118+ console . log ( typeof inflatedLog === 'string' ? JSON . stringify ( inflatedLog ) : inflatedLog )
95119 }
96120}
0 commit comments